In memory download and extract zip archive
Asked Answered
V

5

13

I would like to download a zip archive and unzip it in memory using PHP.

This is what I have today (and it's just too much file-handling for me :) ):

// download the data file from the real page
copy("http://www.curriculummagic.com/AdvancedBalloons.kmz", "./data/zip.kmz");

// unzip it
$zip = new ZipArchive;
$res = $zip->open('./data/zip.kmz');
if ($res === TRUE) {
    $zip->extractTo('./data');
    $zip->close();
}

// use the unzipped files...
Voluntarism answered 12/9, 2011 at 18:5 Comment(0)
T
19

Warning: This cannot be done in memory — ZipArchive cannot work with "memory mapped files".

You can obtain the data of a file inside a zip-file into a variable (memory) with file_get_contentsDocs as it supports the zip:// Stream wrapper Docs:

$zipFile = './data/zip.kmz';     # path of zip-file
$fileInZip = 'test.txt';         # name the file to obtain

# read the file's data:
$path = sprintf('zip://%s#%s', $zipFile, $fileInZip);
$fileData = file_get_contents($path);

You can only access local files with zip:// or via ZipArchive. For that you can first copy the contents to a temporary file and work with it:

$zip = 'http://www.curriculummagic.com/AdvancedBalloons.kmz';
$file = 'doc.kml';

$ext = pathinfo($zip, PATHINFO_EXTENSION);
$temp = tempnam(sys_get_temp_dir(), $ext);
copy($zip, $temp);
$data = file_get_contents("zip://$temp#$file");
unlink($temp);
Tabitha answered 12/9, 2011 at 18:19 Comment(4)
No, zip:// supports only local files so far (as well as ZipArchive). You have to have it accessible via your standard file-system. Will update the answer to make this visible for your http url.Tabitha
No, stacked streams are not supported, I guess even file:// won't work. It needs to be the path to the filesystem. Same for the ZipArchive class, I assume this relates to each other. Streams are not that well supported within the zip PHP extension, which is a bit pity in my eyes. Go for the tempfile to work around that, the second code example. You can encapsulate that into a class of it's own extending/decorating ZipArchive and provide a file-system wrapper that is capable of to have this more fluent.Tabitha
@dacwe: If your file-system mounts a ramdisk, there is no problem at all to have ZipArchive work in memory - for what it matters :)Tabitha
Yeah, that was actually the way I solved it, but it's not a php solution.. :)Voluntarism
H
5

As easy as:

$zipFile = "test.zip";
$fileInsideZip = "somefile.txt";
$content = file_get_contents("zip://$zipFile#$fileInsideZip");
Haema answered 8/5, 2016 at 15:50 Comment(0)
T
5

Old subject but still relevant since I asked myself the same question, without finding an answer.

I ended up writing this function which returns an array containing the name of each file contained in the archive, as well as the decompressed contents of that file:

function GetZipContent(String $body_containing_zip_file) {

    $sectors = explode("\x50\x4b\x01\x02", $body_containing_zip_file);
    array_pop($sectors);
    $files = explode("\x50\x4b\x03\x04", implode("\x50\x4b\x01\x02", $sectors));
    array_shift($files);

    $result = array();
    foreach($files as $file) {
        $header = unpack("vversion/vflag/vmethod/vmodification_time/vmodification_date/Vcrc/Vcompressed_size/Vuncompressed_size/vfilename_length/vextrafield_length", $file);
        array_push($result, [
            'filename' => substr($file, 26, $header['filename_length']),
            'content' => gzinflate(substr($file, 26 + $header['filename_length'], -12))
        ]);
    }
    
    return $result;
}
Trevino answered 27/5, 2021 at 12:31 Comment(2)
FYI - this is just a stub. $data is undefined on the first linePlated
This is like UBER useful! Thank you! I used this to grab a zipped file from CURL and process it without having to hit the file system! You totally rock!Bolus
S
0

You can get a stream to a file inside the zip and extract it into a variable:

$fp = $zip->getStream('test.txt');
if(!$fp) exit("failed\n");

while (!feof($fp)) {
    $contents .= fread($fp, 1024);
}

fclose($fp);
Slater answered 12/9, 2011 at 18:9 Comment(1)
How can I download the file and "pipe" it to the $zip?Voluntarism
D
-1

If you can use system calls, the simplest way should look like this (bzip2 case). You just use stdout.

$out=shell_exec('bzip2 -dkc '.$zip);
Dev answered 11/5, 2015 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.