maennchen/ZipStream-PHP

How to load zip stream to s3 bucket.

za4me opened this issue · 1 comments

za4me commented

Please add this to wiki. I spent lot of time and i think this little example will be helpful for new users. Current issue which i found #120 don't have clear and simple example how i can write zip stream to s3 bucket.

use Aws\S3\S3Client;
use Aws\Credentials\CredentialProvider;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;

$bucket = 'your bucket name';
$client = new S3Client([
    'region' => 'your region',
    'version' => 'latest',
    'bucketName' => $bucket,
    'credentials' => CredentialProvider::defaultProvider(),
]);
$client->registerStreamWrapper();

$zipFile = fopen("s3://$bucket/example.zip", 'w');

$options = new Archive();
$options->setEnableZip64(false);
$options->setOutputStream($zipFile);

$zip = new ZipStream(null, $options);
$zip->addFile('file1.txt', 'File1 data');
$zip->addFile('file2.txt', 'File2 data');
$zip->finish();

fclose($zipFile);