Pclzip: Powerful PHP Compression decompression class use tutorial

Source: Internet
Author: User
Tags unzip archive
When you purchase a virtual host that does not support Zip_open (),
Also does not support new ziparchive (),
Don't even think it will support exec (), DL () ...
But Gzopen () will always support it.

PHP compression decompression class Pclzip as long as the server support Gzopen can be compression and decompression.

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:

<? Phprequire_once (' pclzip.lib.php '); $archive = new Pclzip ("Archive.zip"); >


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:

<? Phprequire_once (' pclzip.lib.php '); $archive = new Pclzip (' Archive.zip '); $v _list = $archive->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:

<? Php$list = $archive->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:

<? Phpinclude_once (' pclzip.lib.php '); $archive = new Pclzip (' Archive.zip '); $v _list = $archive->create (' File.txt,data /text.txt,folder '), if ($v _list = = 0) {die ("Error:". $archive->errorinfo (True));}? >


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

<? Phpinclude_once (' pclzip.lib.php '); $archive = new Pclzip (' Archive.zip '); $v _list = $archive->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:

<? Phpinclude_once (' pclzip.lib.php '); $zip = new Pclzip ("Test.zip"), if ($list = $zip->listcontent ()) = = 0) {die ("Error: ". $zip->errorinfo (True));} for ($i =0; $i
 
  
";} 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:

<? Phprequire_once (' pclzip.lib.php '); $archive = new Pclzip (' Archive.zip '); if ($archive->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:

<? Phpinclude (' pclzip.lib.php '); $archive = new Pclzip (' Archive.zip '); if ($archive->extract (pclzip_opt_path, ' data '), Pclzip_opt_remove_path, ' install/release ') = = 0) {die ("Error:". $archive->errorinfo (True));}? >

Official website: http://www.phpconcept.net/pclzip/pclzip-downloads

  • 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.