Unzipping a directory within a zip file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eazhar
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 12:35 pm

Unzipping a directory within a zip file

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Unzipping a directory within a zip file

Post by VladSun »

Try not to give the second parameter ...

Code: Select all

$zip->extractTo('/my/destination/dir/');
There are 10 types of people in this world, those who understand binary and those who don't
eazhar
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 12:35 pm

Re: Unzipping a directory within a zip file

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Unzipping a directory within a zip file

Post 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.
eazhar
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 12:35 pm

Re: Unzipping a directory within a zip file

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Unzipping a directory within a zip file

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Unzipping a directory within a zip file

Post 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.
Post Reply