1

I have a zip file with sub directories inside it. I use the php extractTo() function to unzip it, but I only get the files from the root, I can't get the files inside the subdirectories.

My Zip file has this structure:

-Json
  - 01.json
  - 02.json
  - 03.json
  - URL
     - url1.json
     - url2.json
  - seeker
     - seek1.json
     - seek.json

And my code is:

$zip = new ZipArchive();
$zip->open($nombrezip);
$zip->extractTo($destino);
$zip->close();

So I need to Unzip the files inside "URL" and "seeker".

ManelPNavarro
  • 579
  • 7
  • 21
  • possible duplicate of [Extract sub folders of ZIP file in PHP](http://stackoverflow.com/questions/10968359/extract-sub-folders-of-zip-file-in-php) – TiMESPLiNTER Apr 20 '15 at 09:13

1 Answers1

1

So let's say you have a ZipFile: test.zip

Structure:

- file1.txt
- file1.txt
- - dir1/
- - - file3.txt
- - - file4.txt
- - dir2/
- - - file5.txt
- - - file6.txt

To keep ZIP structure you could do:

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->extractTo('./test', array('file1.txt', 'file2.txt'));
    $zip->extractTo('./test', array('dir1/file3.txt', 'dir1/file4.txt'));
    $zip->extractTo('./test', array('dir2/file5.txt', 'dir2/file6.txt'));
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

That's the same as doing

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->extractTo('./test');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

But if you have a test.zip file with this Structure:

- file1.txt
- file1.txt
- file3.txt
- file4.txt
- file5.txt
- file6.txt

And you want to get output structured as before, you could do this:

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->extractTo('./test/', array('file1.txt', 'file2.txt'));
    $zip->extractTo('./test/dir1', array('file3.txt', 'file4.txt'));
    $zip->extractTo('./test/dir2', array('file5.txt', 'file6.txt'));
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>
lalengua
  • 509
  • 3
  • 15