Skip to content

Instantly share code, notes, and snippets.

@svandragt
Last active Mar 10, 2020
Embed
What would you like to do?
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