php 本身有 zip 模块,可以生产 zip 文件。但是这个 zip 模块只能使用本地文件来打包。如果需要打包输出的文件来自网络,就得先保存临时文件。在文件数量多或者文件大的时候就很杯具。另外,由 php 来输出大的打包文件会占用 php 进程大量时间,影响并发能力。
nginx 有一个第三方模块,mod_zip 。同样可以输出 zip 包。和 X-Accel-Redirect 有点类似,只需要 php 输出相应文件的路径等信息,然后给一个特殊的响应头即可。
nginx zip 模块使用的响应头是 X-Archive-Files: zip 。加上这个响应头,nginx zip 模块就会处理响应正文,完成打包输出。
比如:
复制代码 代码如下:
printf("%s %d %s %s\n", $crc32, $size, $url, $path );
逐条输出要打包的文件。
$crc32 是 16 进制的文件 crc32 值。也可以不提供,用 “-” 代替。不过这样就没法用 Range 分块下载,断点续传了。
$size 是文件大小的十进制整数。
$url 是要打包的源地址。如果要打包一个本地文件,可以先在 nginx 中做一个 internal path。
$path 是 zip 包中的路径。
不过这样没法创建空目录。一方面,zip 格式开始就没有定义空目录,后来的标准和软件都是通过加一个 / 结尾的 0 大小文件来实现的。这时,就需要先在 nginx 中做一个 internal 的 0 大小文件,比如位于 /_0 。然后输出
复制代码 代码如下:
printf("%s %d %s %s\n", '00000000', 0, '/_0', $path.'/');
如果要支持中文路径,可以使用 X-Archive-Charset: utf8 这样的响应头,内容为你输出的编码。nginx zip 模块会按标准转换成 utf8 的标准格式。不过各个软件对这个 zip 的标准支持不一,比如 windows 的 zip 目录就不支持,只能以 gbk 编码直接输出。其他软件对编码支持效果也各不相同。测试过的 winrar,7zip,windows zip 目录中,winrar 倒是都可以很好支持。7zip 可能会把部分中文空目录变成 0 大小文件。所以,这点还需要自己斟酌处理。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft