Last active
March 10, 2020 17:31
-
-
Save svandragt/06c3988048feae71cac17ff13ce943c8 to your computer and use it in GitHub Desktop.
Zip files in a streamable way, with a 2MB memory footprint.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for a 5GB file: 2.9 MiB Memory used.