0

I have a zip file with some files and folders inside, and I want to extract the contents of the folder "/files" from the zip file to the a specified path (the root path of my application).

If there is a non existing folder it should just be created.

So for example if the path inside the zip is: "/files/includes/test.class.php" it should be extracted to

$path . "/includes/test.class.php"

How can I do this?

The only function i found to switch inside the zip file should be

http://www.php.net/manual/en/ziparchive.getstream.php

but i actually don't know how i can do that with this function.

TheNiceGuy
  • 3,462
  • 8
  • 34
  • 64
  • Step 1) Read the [documentation](http://php.net/manual/en/class.ziparchive.php) FYI `ZipArchive` will preserve directory structure so you don't have to worry. – Emissary Apr 02 '13 at 18:44
  • Yes but i dont want to extract the root of the zip, to the root of my app. I want to extract "/files" from the zip to my root. – TheNiceGuy Apr 02 '13 at 18:48
  • Ye but to reiterate by last point, someone has also covered that in the... (you guessed it) documentation [here](http://www.php.net/manual/en/ziparchive.extractto.php#100802) and [here](http://www.php.net/manual/en/ziparchive.extractto.php#94921) - I also distinctly remember seeing that question on Stackoverflow before! – Emissary Apr 02 '13 at 18:51

2 Answers2

0

I think you need zziplib extension for this to work

$zip = new ZipArchive;

if ($zip->open('your zip file') === TRUE) {
  //create folder if does not exist
  if (!is_dir('path/to/directory')) {
      mkdir('path/to/directory');
  }

  //then extract the zip
  $zip->extractTo('destination to which zip is to be extracted');
  $zip->close();
  echo 'Zip successfully extracted.';
} else {
  echo 'An error occured while extracting.';
}

Read this link for more info http://www.php.net/manual/en/ziparchive.extractto.php

Hope this helps :)

Sabari
  • 6,205
  • 1
  • 27
  • 36
  • Thanks but that won't answer the question. I dont want to extract the root of the zip, to the root of my app. I want to extract "/files" from the zip to my root. – TheNiceGuy Apr 02 '13 at 18:50
  • May be this can help you then http://stackoverflow.com/questions/10968359/extract-sub-folders-of-zip-file-in-php – Sabari Apr 02 '13 at 18:59
0

Try this:

$zip = new ZipArchive;
$archiveName = 'test.zip';
$destination = $path . '/includes/';
$pattern = '#^files/includes/(.)+#';
$patternReplace = '#^files/includes/#';

function makeStructure($entry, $destination, $patternReplace)
{
    $entry = preg_replace($patternReplace, '', $entry);
    $parts = explode(DIRECTORY_SEPARATOR, $entry);
    $dirArray = array_slice($parts, 0, sizeof($parts) - 1);
    $dir = $destination . join(DIRECTORY_SEPARATOR, $dirArray);
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }
    if ($dir !== $destination) {
        $dir .= DIRECTORY_SEPARATOR;
    }
    $fileExtension = pathinfo($entry, PATHINFO_EXTENSION);
    if (!empty($fileExtension)) {
        $fileName = $dir . pathinfo($entry, PATHINFO_BASENAME);
        return $fileName;
    }
    return null;
}

if ($zip->open($archiveName) === true) {
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $entry = $zip->getNameIndex($i);
        if (preg_match($pattern, $entry)) {
            $file = makeStructure($entry, $destination, $patternReplace);
            if ($file === null) {
                continue;
            }
            copy('zip://' . $archiveName . '#' . $entry, $file);
        }
    }
    $zip->close();
}
mkjasinski
  • 3,115
  • 2
  • 22
  • 21
  • Thanks, but that would just work if i only have this specific file, but i want to extract the whole content of "/files" to "/". – TheNiceGuy Apr 02 '13 at 18:55
  • Thank you this works really good. Just one little problem. If there are subfolders inside the zip archive, they currently wont get copied. Is there a way to copy them too, to keep the structure? thank you – TheNiceGuy Apr 03 '13 at 10:54
  • If there are subfolders inside the zip, they get created as a file. – TheNiceGuy Apr 03 '13 at 11:23
  • @Michael you want to copy the folders? or just the files? – mkjasinski Apr 03 '13 at 11:27
  • All content inside the the given zip folder "/files". – TheNiceGuy Apr 03 '13 at 11:35
  • Hey, thanks for your work. Well, it won't create folders. It just copy all the files into the given path without keeping the structure from the archive. – TheNiceGuy Apr 03 '13 at 16:46
  • I tested this and structure of dirs was maked. I don't know, why dosn't work for you. – mkjasinski Apr 03 '13 at 17:55
  • Yeah the problem was xampp. Could you please just explain me what the makeStructure function does? Cause i rly don't understand her, and also don't want to just copy and paste. Thanks – TheNiceGuy Apr 04 '13 at 13:21
  • `makeStructure` function checks if entry is a file or directory, is created directory tree, and if entry is a file, it returns the full path of file to copy. – mkjasinski Apr 04 '13 at 13:41