Php calls the mod_zip module of nginx to package the ZIP file.

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 you want to package and output files from the network, you must first save the temporary files. When the number of files is large or the file size is large. In addition, the output of large packaging files by php takes a lot of time for the php process and affects the concurrency capability.

Nginx has a third-party module, mod_zip. You can also output a zip package. Similar to X-Accel-Redirect, you only need to output the path and other information of the corresponding file in php, and then give a special response header.

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

For example:
Copy codeThe Code is as follows:
Printf ("% s % d % s \ n", $ crc32, $ size, $ url, $ path );
Output the files to be packaged one by one.

$ Crc32 is a hexadecimal file crc32 value. It can also be left blank and replaced. However, in this way, the Range cannot be used for multipart download and resumable upload.
$ Size is a 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, you cannot create an empty directory. On the one hand, no empty directory is defined at the beginning of the zip format, and later standards and software are implemented by adding a 0-size file at the end. In this case, you must first create an internal 0-size file in nginx, for example, at/_ 0. Then output
Copy codeThe Code is as follows:
Printf ("% s % d % s \ n", '000000', 0, '/_ 0', $ path .'/');
To support Chinese paths, you can use Response Headers such as X-Archive-Charset: utf8 to encode the output content. The nginx zip module is converted to the standard utf8 format according to the standard. However, each software has different support for this zip file. For example, the windows zip directory is not supported and can only be output in gbk encoding. The effects of other software on coding vary. Winrar, 7zip, and windows zip directories that have been tested can all be well supported. 7zip may change some empty Chinese directories to 0 files. Therefore, you need to consider this.

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.