Skip to content

Instantly share code, notes, and snippets.

@svandragt
Last active March 10, 2020 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svandragt/06c3988048feae71cac17ff13ce943c8 to your computer and use it in GitHub Desktop.
Save svandragt/06c3988048feae71cac17ff13ce943c8 to your computer and use it in GitHub Desktop.
Zip files in a streamable way, with a 2MB memory footprint.
<?php
// composer init; composer require maennchen/zipstream-php
error_reporting(-1);
require_once __DIR__ .'/vendor/autoload.php';
function stream_zip( array $files, string $out_file ) {
$out = fopen( 'file://' . $out_file, 'w' );
$options = new ZipStream\Option\Archive();
$options->setOutputStream( $out );
$zip = new ZipStream\ZipStream( 'zipstream.zip', $options );
foreach ( $files as $file ) {
$in = fopen( 'file://' . __DIR__ . "/{$file}", 'rb' );
$zip->addFileFromStream( $file, $in );
fclose( $in );
}
$zip->finish();
fclose($out);
echo ( memory_get_peak_usage( false ) / 1024 / 1024 ) . " MiB\n";
}
stream_zip( [ 'box-disk001.vmdk' ], __DIR__ . '/sander.zip' );
@svandragt
Copy link
Author

svandragt commented Mar 10, 2020

for a 5GB file: 2.9 MiB Memory used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment