PHP calls nginx Mod_zip module packaging zip file _php Tips

Source: Internet
Author: User

PHP itself has a zip module that can produce zip files. However, this zip module can only be packaged using local files. If the file that you want to package output from is a network, you must save the temporary file first. It's a cup of paper when you have a lot of files or a large file. In addition, PHP to output large packaging files will occupy the PHP process a lot of time, affecting concurrency.

Nginx has a third party module, Mod_zip. You can also output a zip package. And x-accel-redirect a bit similar, only need PHP output the path of the corresponding file information, and then give a special response header can be.

The response head used by the Nginx Zip module is x-archive-files:zip. With this response header, the Nginx Zip module processes the response body and completes the packaged output.

Like what:

Copy Code code as follows:

printf ("%s%d%s%s\n", $CRC, $size, $url, $path);

Output the files to be packaged.

$CRC 32 is the CRC32 value of the 16 file. May not be provided, with "-" instead. However, this can not be used to download the Range block, the breakpoint continued to pass.
$size is a decimal integer of the file size.
$url is the source address to package. If you want to package a local file, you can first make a internal path in the Nginx.
$path is the path in the ZIP package.

However, it is impossible to create an empty directory. On the one hand, the zip format does not initially define an empty directory, and later standards and software are implemented by adding a/end 0 size file. At this point, you need to first in the Nginx to do a internal 0 size file, such as located in/_0. and then output

Copy Code code as follows:

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

If you want to support a Chinese path, you can use a response header such as X-archive-charset:utf8 to encode the output. The Nginx Zip module is converted to the standard format of UTF8 as standard. However, different software for the standard support of this zip, such as Windows Zip Directory is not supported, can only be directly output in GBK encoding. Other software has different effects on coding support. Tested winrar,7zip,windows Zip directory, winrar can be very good support. 7zip may turn some of the Chinese empty directories into 0 size files. So it needs to be dealt with at your own discretion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.