Pclzip: Powerful PHP compression and decompression zip type

Source: Internet
Author: User
Tags create zip unzip archive zip archive file
Pclzip: Powerful PHP compression and decompression zip class

Pclzip Introduction
The Pclzip library is capable of compressing and decompressing compressed files (WINZIP, PKZIP) in zip format, and can process such files, including generating compression files, listing the contents of compressed files, and extracting archives, and so on. Because of the ability to compress and decompress on the server side, it is quite convenient to use.
Pclzip defines a Pclzip class whose category object can be treated as a zip file and method is also provided for processing.

?

How to use Pclzip

?

1. Basic
All functions are provided by pclzip.lib.php this file, Pclzip Library can be downloaded from its homepage (www.phpconcept.net/pclzip/index.en.php). All pkzip files are actually a pclzip category object. When a pclzip file (ie, Pclzip Category object) is generated, a compressed file is created and the file name is specified, but the contents of this archive do not exist:

 
  

This object provides some public method that can be used to process this file.

?

2. Parameters
Each method has its own parameters that can be used, including parameters that must and must not be required:

 
  Add (' Dev/file.txt ',         pclzip_opt_remove_path, ' dev ');? >

The ' Dev/file.txt ' in the above example is a required parameter; ' Pclzip_opt_remove_path ' is a non-mandatory parameter. Of course, some method can also contain only non-mandatory parameters:

 
  Extract (Pclzip_opt_path, "folder",                          Pclzip_opt_remove_path, "Data",                                pclzip_cb_pre_extract, "callback_pre_ Extract ",);? >
?

In the previous example, the file stored in the original compressed file path is/data, but you can specify the decompression to/folder. In addition, before extracting, the callback function (' Callback_pre_extract () ') is called, which allows the user to change the file storage path and file name during the decompression process, or select some files to be uncompressed.
All available non-essential parameters can be referenced in the URL (www.phpconcept.net/pclzip/man/en/index.php).

?

3. Callback Value
The value returned by each method may be different and will be described in each method. But most of the method callbacks 0, error or array.

?

4. Error handling
Since version 1.3, error handling has been integrated into the Pclzip category, and when a method callback error code, you can learn some additional messages to facilitate error handling:
* ErrorName (): Callback Error name
* ErrorCode (): Callback error code
* ERRORINFO (): Description of callback Error

Here are a few examples to illustrate how to use Pclzip.

?

Pclzip Example 1, generating a zip archive
Pclzip ($zipname): For Pclzip constructor, $zipname the file name of the PKZIP compression file.
The main thing is to produce a Pclzip object, that is, a pkzip compressed file; At this point, only the compression file is generated, and do some checking (for example, if there is an open zlib extension ... And so on), in addition, did not do other actions.

Create ($filelist, [optional Arguments list]): Adds the parameter $filelist the specified file or directory (including all of the files and subdirectories) to the resulting compression file.
Instead of the necessary parameters, you can modify the file storage path within the compressed file.
The parameters available for this method can refer to the log (www.phpconcept.net/pclzip/man/en/index.php).

The following example shows how to generate a pkzip compressed file (file name Archive.zip) and add the File.txt, Data/text.txt, and Directory folder (containing the files and subdirectories) to the newly generated archive.zip:

Usage One:

 
  Create (' File.txt,data/text.txt,folder '), if ($v _list = = 0) {die ("Error:". $archive->errorinfo (True));}? >
?

Usage Two:

The following example shows that Archive.zip is essentially the same as in the previous example, but when file.txt and text.txt are compressed, the path is changed from data/to install/, so the path to the two files in Archive.zip is install/ File.txt and Install/text.txt

 
  Create (' Data/file.txt,data/text.txt ', Pclzip_opt_remove_path, ' data ', Pclzip_opt_add_path, ' Install '); if ($v _list = = 0) {die ("Error:".) $archive->errorinfo (True));}? >
?

Pclzip instance 2, List compressed file contents

Listcontent (): Lists the contents of the archive, including the properties and directories of the file:

 
  Listcontent ()) = = 0) {die ("Error:". $zip->errorinfo (True));} for ($i = 0; $i < sizeof ($list); $i + +) {for (Reset ($list [$i]); $key = key ($list [$i]); next ($list [$i]) {E Cho "File $i/[$key] =". $list [$i] [$key]. "
";} echo "
";}? >

?

The previous example will return the result:
File 0/[FileName] = Data/file1.txt
File 0/[Stored_filename] = Data/file1.txt
File 0/[Size] = 53
File 0/[compressed_size] = 36
File 0/[Mtime] = 1010440428
File 0/[Comment] =
File 0/[Folder] = 0
File 0/[index] = 0
File 0/[Status] = OK

File 1/[FileName] = Data/file2.txt
File 1/[stored_filename] = Data/file2.txt
File 1/[Size] = 54
File 1/[compressed_size] = 53
File 1/[mtime] = 1011197724
File 1/[Comment] =
File 1/[Folder] = 0
File 1/[Index] = 1
File 1/[Status] = OK

?

Pclzip Example 3. Unzip the archive
Extract ([Options list]): Unzip the file or directory in the PKZIP.
The available parameters for the [options list] are referenced in the URL (www.phpconcept.net/pclzip/man/en/index.php). These parameters allow the user to extract more options when extracting, such as specifying the path to the extracted archive, specifying that only some files can be decompressed or not being uncompressed, or extracting the files into a string output (which can be used in a Readme file).

The following example is an example of a simple unzip archive that compresses files in the archive archive.zip to the current directory:

 
  Extract () = = 0) {die            ("Error:". $archive->errorinfo (True));        }? >

The following example is an advanced decompression file used, all files in the Archive.zip are decompressed in data/, and all files in the Install/release are also dropped directly in data/, not data/install/release:

 
  Extract (Pclzip_opt_path, ' data ',                  pclzip_opt_remove_path, ' install/release ') = = 0) {die                                ("Error:". $archive- >errorinfo (True));        }? >

If you see the parameters of the Create method, and then look at the method prototype, you know how to do it. At least I haven't used that yet.

Related documents:
Pclzip official address : http://www.phpconcept.net/pclzip/index.php
Pclzip Manual Address : http://www.phpconcept.net/pclzip/man/en/index.php
Pear class Create zip archive file : http://www.ccvita.com/10.html
Pclzip Introduction and use : http://www.ccvita.com/59.html
Pclzip: Powerful PHP compression and decompression Zip class : http://www.ccvita.com/330.html

  • Related Article

    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.