我想使用ZipArchive(或本地PHP类)在内存中创建一个zip文件,并将文件内容读取回客户端。是否可能?如果是,怎么做?
我想在此应用程序中压缩的文件最大为15 MB,从内存方面来看应该没问题。
我想使用ZipArchive(或本地PHP类)在内存中创建一个zip文件,并将文件内容读取回客户端。是否可能?如果是,怎么做?
我想在此应用程序中压缩的文件最大为15 MB,从内存方面来看应该没问题。
$zip = new \ZipStream\ZipStream('bulk-export.zip');
$zip->addFile('users.csv', '(csv data generated by another method here)');
$zip->finish();
- Hans<?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
?>
有另一个线程在讨论这个问题: 使用PHP在内存中操作归档文件(无需在磁盘上创建临时文件)
nettle建议使用phpmyadmin的zip.lib.php。我认为这是一个相当可靠的解决方案。
顺便提一下,zip.lib.php已经不存在了,它已经被同一libraries/文件夹中的ZipFile.php所取代。