36

I would like to create a zip file in memory using a ZipArchive (or a native PHP class) and read the content of the file back to the client. Is this possible? If so, how?

The files that I want to zip in this application are a maximum of 15 MB total. I think we should be in good shape memory-wise.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
CLJ
  • 1,907
  • 5
  • 22
  • 36
  • 3
    Duplicate: http://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly – pestaa Nov 12 '10 at 13:55
  • and how will you handle zip archives that exceed the amount of memory available to PHP? – bcosca Nov 12 '10 at 13:55
  • please let this question stay open, http://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly was asked in a way that it could receive useful answers. – CLJ Nov 12 '10 at 14:13
  • sorry, your comment has confused me. Please explain. – Oddthinking Nov 12 '10 at 14:20
  • @Oddthinking, sorry about that. The other question and answers skip over the part where the zip file is opened for writing (http://www.php.net/manual/en/function.ziparchive-open.php), which is the crux of the entire problem. If that is not addressed, the problem can not be solved. – CLJ Nov 12 '10 at 14:30
  • Does this answer your question? [Manipulate an Archive in memory with PHP (without creating a temporary file on disk)](https://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-d) – A.L Nov 08 '19 at 15:13

4 Answers4

6

Try ZipStream (link to GitHub repo, also supports install via Composer).

From original author's website (now dead):

ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • 1
    I found this to be a good replacement for the dead link: https://github.com/maennchen/ZipStream-PHP `$zip = new \ZipStream\ZipStream('bulk-export.zip'); $zip->addFile('users.csv', '(csv data generated by another method here)'); $zip->finish();` – Hans Jan 01 '17 at 20:24
5

Thanks to Frosty Z for the great library ZipStream-PHP. We have a use case to upload some large zip files in quantity and size to S3. The official documentation does not mention how to upload to S3.

So we've had an idea to stream the zip created by ZipStream output directly to S3 without creating the zip file on the server.

Here is a working sample code that we came up with:

<?php
# Autoload the dependencies
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use ZipStream\Option\Archive as ArchiveOptions;

//s3client service
$s3Client = new S3Client([
    'region' => 'ap-southeast-2',
    'version' => 'latest',
    'credentials' => [
        'key' => '<AWS_KEY>',
        'secret' => '<AWS_SECRET_KEY>',
    ]
]);
$s3Client->registerStreamWrapper(); //required

$opt = new ArchiveOptions();
$opt->setContentType('application/octet-stream');
$opt->setEnableZip64(false); //optional - for MacOs to open archives

$bucket = 'your_bucket_path';
$zipName = 'target.zip';

$zip = new ZipStream\ZipStream($zipName, $opt);

$path = "s3://{$bucket}/{$zipName}";
$s3Stream = fopen($path, 'w');

$zip->opt->setOutputStream($s3Stream); // set ZipStream's output stream to the open S3 stream

$filePath1 = './local_files/document1.zip';
$filePath2 = './local_files/document2.zip';
$filePath3 = './local_files/document3.zip';

$zip->addFileFromPath(basename($filePath1), $filePath1);
$zip->addFileFromPath(basename($filePath2), $filePath2);
$zip->addFileFromPath(basename($filePath3), $filePath3);

$zip->finish(); // sends the stream to S3
?>
Terry Truong
  • 291
  • 3
  • 4
3

Take a look at the following library, it allows creating zip files and returning them as a stream: PHPClasses.org.

Cpt. eMco
  • 565
  • 4
  • 13
  • 8
    I would like something that is native to the standard php library. – CLJ Nov 12 '10 at 14:22
  • If I may ask, is there a reason for that? – Cpt. eMco Nov 12 '10 at 15:02
  • 6
    So I do not have to drag in a dependency and maintain it over time. If it is included in the standard library, I will not have to worry about incompatibly when I upgrade php versions. – CLJ Nov 12 '10 at 15:08
  • 3
    Actually, it's a PHP class. And it requires PHP5. Upgrade concerns should be worrying you when PHP6 arrives (not anytime soon). – Joel A. Villarreal Bertoldi Nov 12 '10 at 17:22
  • 1
    @Chris: You may have a look too at http://pablotron.org/software/zipstream-php/ (suggested here: http://stackoverflow.com/questions/990462/generating-zip-files-with-php-apache-on-the-fly-in-high-speed) – Maxime Pacary Nov 16 '11 at 10:51
  • @FrostyZ This should have been an answer. – jahu Jan 26 '15 at 11:56
0

There is another thread talking about this: Manipulate an Archive in memory with PHP (without creating a temporary file on disk)

nettle has suggested to use zip.lib.php from phpmyadmin. I think this is a rather solid solution.

FYI zip.lib.php does not exist anymore, it has been replace by ZipFile.php in the same libraries/ folder.

Community
  • 1
  • 1
Shunzhe
  • 1
  • 2