Home >Backend Development >PHP Tutorial >PHP calls nginx's mod_zip module to package ZIP files_PHP tutorial

PHP calls nginx's mod_zip module to package ZIP files_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:381227browse

php itself has a zip module that can produce zip files. But this zip module can only use local files for packaging. If the files that need to be packaged and output come from the network, you must first save the temporary files. This is a problem when there are many files or the files are large. In addition, using PHP to output large packaged files will take up a lot of time in the PHP process and affect concurrency capabilities.

nginx has a third-party module, mod_zip. You can also export zip packages. It is somewhat similar to X-Accel-Redirect. It only needs php to output the path of the corresponding file and other information, and then give a special response header.

The response header used by the nginx zip module is X-Archive-Files: zip . With this response header added, the nginx zip module will process the response body and complete the packaging output.

For example:

Copy code The code is as follows:

printf("%s %d %s %sn", $crc32, $size, $url, $path );

Output the files to be packaged one by one.

$crc32 is the hexadecimal file crc32 value. It can also be omitted and replaced with "-". However, in this way, it is impossible to use Range to download in chunks and resume the download from a breakpoint.
$size is the decimal integer of the file size.
$url is the source address to be packaged. If you want to package a local file, you can first create an internal path in nginx.
$path is the path in the zip package.

However, this cannot create an empty directory. On the one hand, the zip format did not define an empty directory from the beginning. Later standards and software implemented this by adding a 0-size file ending with /. At this time, you need to first create an internal 0-size file in nginx, such as located at /_0. Then output

Copy code The code is as follows:

printf("%s %d %s %sn", '00000000' , 0, '/_0', $path.'/');

If you want to support Chinese paths, you can use a response header like X-Archive-Charset: utf8, and the content is the encoding you output. The nginx zip module will convert the data into the utf8 standard format according to the standard. However, each software has different support for this zip standard. For example, the windows zip directory does not support it and can only be output directly in gbk encoding. Other software also has different encoding support effects. Among the tested winrar, 7zip, and windows zip directories, winrar can support them very well. 7zip may turn some empty Chinese directories into 0-size files. Therefore, this still needs to be considered and dealt with by yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781816.htmlTechArticlephp itself has a zip module that can produce zip files. But this zip module can only use local files for packaging. If the files you need to package and output come from the Internet, you must first save the temporary files...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn