esviko
esviko

Reputation: 358

Adding a file to a specific folder of zip archive

I've got a zip archive (Open Document Spread Sheet - ods) containing this structure:

Configurations2   | File Folder
META-INF          | File Folder
Pictures          | File Folder
Thumbnails        | File Folder
meta.xml          | XML Document
mimetype          | File
settings.xml      | XML Document
styles.xml        | XML Document

What I want to do now is adding two files to the zip archive

  1. one file named `content.xml` to the root of the .ods structure
  2. another file named `image.png` to the ./Puctures Folder

Adding a file to the root of a zip archive is not a problem

// `$zipfile` contains the sructure as desribed above
// `$content_xml` contains a xml string
// `$image` contains the `image.png`
$zip = new ZipArchive();
$zip->open($zipfile, ZIPARCHIVE::CREATE);
$zip->addFromString('content.xml',$content_xml);
$zip->close();

How can I add $image to ./Pictures? Any help would be greatly appreciated!

Upvotes: 1

Views: 2170

Answers (1)

stinnes
stinnes

Reputation: 56

I don't use PHP but can't you simply add files with the full path like this:

$zipFile->addFile($folder . $f, $subfolder . $f);

edit:

Just checked it ... works just fine. Assuming ZIP contains an icons folder following code works (if not, add one with addEmtpyDir('icons')):

<?php

$zip = new ZipArchive();
$zip->open('./test.odt', ZIPARCHIVE::CREATE);
$zip->addFile('./favicon.ico', 'icons/fav.ico');
$zip->close();

?>

Upvotes: 4

Related Questions