使用 PHP 类 ZipArchive 创建 zip 文件而不将文件写入磁盘?

发布于 2024-10-01 19:12:24 字数 132 浏览 3 评论 0原文

我想使用 ZipArchive (或本机 PHP 类)在内存中创建一个 zip 文件,并将文件的内容读回客户端。这可能吗?如果是这样,怎么办?

我想要在此应用程序中压缩的文件总共最大为 15 MB。我认为我们的记忆力应该保持良好状态。

I would like to create a zip file in memory using a ZipArchive (or a native PHP class) and read the content of the file back to the client. Is this possible? If so, how?

The files that I want to zip in this application are a maximum of 15 MB total. I think we should be in good shape memory-wise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱给你人给你 2024-10-08 19:12:24

尝试 ZipStream (链接到 GitHub 存储库,也支持通过 作曲家)。

来自原作者的网站(现已死亡):

ZipStream 是一个用于动态流式传输动态 zip 文件的库
来自 PHP,根本不写入服务器上的磁盘。

Try ZipStream (link to GitHub repo, also supports install via Composer).

From original author's website (now dead):

ZipStream is a library for dynamically streaming dynamic zip files
from PHP without writing to the disk at all on the server.

裂开嘴轻声笑有多痛 2024-10-08 19:12:24

感谢 Frosty Z 提供的优秀库 ZipStream-PHP。我们有一个用例,用于上传一些数量和大小都较大的 zip 文件到 S3。官方文档没有提到如何上传到S3。

因此,我们有一个想法,将 ZipStream 输出创建的 zip 直接流式传输到 S3,而无需在服务器上创建 zip 文件。

这是我们提出的工作示例代码:

<?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
?>

Thanks to Frosty Z for the great library ZipStream-PHP. We have a use case to upload some large zip files in quantity and size to S3. The official documentation does not mention how to upload to S3.

So we've had an idea to stream the zip created by ZipStream output directly to S3 without creating the zip file on the server.

Here is a working sample code that we came up with:

<?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
?>
被翻牌 2024-10-08 19:12:24

看一下下面的库,它允许创建 zip 文件并将它们作为流返回: PHPClasses.org

Take a look at the following library, it allows creating zip files and returning them as a stream: PHPClasses.org.

铁憨憨 2024-10-08 19:12:24

还有另一个话题讨论这个:
使用 PHP 操作内存中的存档(无需在磁盘上创建临时文件)

nettle 建议使用 phpmyadmin。我认为这是一个相当可靠的解决方案。

仅供参考 zip.lib.php 不再存在,它已被同一库/文件夹中的 ZipFile.php 替换。

There is another thread talking about this:
Manipulate an Archive in memory with PHP (without creating a temporary file on disk)

nettle has suggested to use zip.lib.php from phpmyadmin. I think this is a rather solid solution.

FYI zip.lib.php does not exist anymore, it has been replace by ZipFile.php in the same libraries/ folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文