Page 1 of 1

Unzipping a directory within a zip file

Posted: Thu Jun 12, 2008 1:21 pm
by eazhar
So I have a zip file that has the following directory structure

Fit Y by X - Contextual.zip
>[dir] _rels
>[dir] word
>[dir] media <-- this is inside the word folder
>[dir] docProp

So basically what I'm trying to extract is all the contents inside the word/media/ folder.

I'm using the example provided in the php.net http://us2.php.net/manual/en/function.z ... ractto.php

The example appears to be good for specific files inside the main directory as soon as you open up the zip. But it doesn't specify how to extract folders, files within folders, or folders within folders.

Code: Select all

 
<?php
$zip = new ZipArchive;
$res = $zip->open('Fit Y by X - Contextual.docx');
if ($res === TRUE) {
    $zip->extractTo('extract/', array("word\media\."));  [color=#40FF00]//<---- not sure what goes in the second parameter[/color]
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>
 
I'm not sure what goes inside the second parameter
I've tried all sorts of permutations such as:

array('word\media\.')
array("word\media\")
array('word\media\')
array("word/media/.")
array('word/media/.')
array("word/media/")
array('word/media/')

as well as all of those without the array functions and none seem to work. Any suggestions?

Re: Unzipping a directory within a zip file

Posted: Thu Jun 12, 2008 3:09 pm
by VladSun
Try not to give the second parameter ...

Code: Select all

$zip->extractTo('/my/destination/dir/');

Re: Unzipping a directory within a zip file

Posted: Thu Jun 12, 2008 7:16 pm
by eazhar
Well I understand the first parameter is the destination to which you want the extracted files. My problem is that I dont want to extract the entire zip, just a certain folder within the archive.

Re: Unzipping a directory within a zip file

Posted: Thu Jun 12, 2008 10:15 pm
by Ollie Saunders
You seem to be on the right lines by my judgement eazhar.
You'll almost certainly want to leave the dot off the end and the slash you want to use will be contained within the DIRECTORY_SEPARATOR constant but even then it shouldn't even matter.

Try this:

Code: Select all

$zip->extractTo('extract', '/word/media');
Otherwise I don't know what's going wrong. What is actually happening with each of the things you've tried and what is extractTo() returning? Verify that your zip does actually contain the directories you are trying to extract.

Re: Unzipping a directory within a zip file

Posted: Fri Jun 13, 2008 11:28 am
by eazhar
Thanks ole

Tried that exactly what you wrote and now i get the folders put in place but none of the files get extracted

for example, now i have \extract\word\media

and media is an empty folder (which it wouldn't be if I extracted it with winrar or command line).

i'm guessing you have to make use of the array() function but I'm not sure exactly how this would apply and then the fact that you would need to iterate through to grab every file.

Re: Unzipping a directory within a zip file

Posted: Fri Jun 13, 2008 11:31 am
by RobertGonzalez
The second example on that page shows that you pass an array of file names inside of a directory to get those files. Have you tried that yet to see if it will work?

Re: Unzipping a directory within a zip file

Posted: Fri Jun 13, 2008 11:59 am
by Ollie Saunders
Yeah it seems like it's not recursing or something, more than a little screwed up. Try specifying actual files as Everah suggests and see if you can actually get it to extract anything but even if that works I'm not sure this will be acceptable and I wouldn't know where to go from there.

Otherwise Is there any reason why you couldn't use the rar command or something else instead? You can issue command line stuff pretty easily from PHP.