A PHP compression and decompression class PclZip is recommended.

Source: Internet
Author: User
Tags decompress zip

PclZip is a powerful PHP class for compressing and decompressing zip files. The PclZip library can compress and decompress Zip files (WinZip and PKZIP ); this type of files can be processed, including generating compressed files, listing compressed files, and extracting files. You can also add or delete an existing ZIP package.

Http://www.phpconcept.net/pclzip/

Recently, I was developing my Wordpress plug-in ShareLink. During this process, I found the PHP class of the zip file operated by PclZip, which I have to recommend. It is simple, easy to use, and powerful.

Another reason for the recommendation is that the source code of the PHP function allows me to find the loose usage of a PHP function parameter. The following is an example.

Generate a zip file

Usage 1:

<?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));}?>

Usage 2:

<?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));}?>

If you see no parameters for the create method, you will know how to deal with it by looking at the method prototype. At least I have never used it.

The following is a simple code for compressing and backing up the entire site:

<? Php require_once ('pclzip. lib. php '); $ zip = new PclZip ("archive.zip"); $ v_list = $ zip-> create ($ _ SERVER ['document _ root'], PCLZIP_OPT_REMOVE_PATH, $ _ SERVER ['document _ root']); if ($ v_list = 0) {echo 'exception :'. $ z-> errorInfo (true);} else {echo 'backup successful ';}?>

Other usage methods:

<? Php // extract to the extract/folder/directory $ list = $ archive-> extract (PCLZIP_OPT_PATH, "extract/folder /"); // Add this directory to the compressed file. After the file is compressed, there will be the backup directory in the file, and the two files will be saved in the backup file $ list = $ archive-> create ("file.txt,image.gif ", PCLZIP_OPT_ADD_PATH, "backup"); // remove some paths, after this is done, it will become test/file.txt $ list = $ archive-> add ("/usr/local/user/test/file.txt", PCLZIP_OPT_REMOVE_PATH, "/usr/local/user"); // if all the shards are lost, after this archive is created, only file.txtand image.gif are displayed, no $ List = $ archive-> create ("data/file.txt images/image.gif", PCLZIP_OPT_REMOVE_ALL_PATH ); // set the CHMOD of the extracted archive to 0777 $ list = $ archive-> extract (PCLZIP_OPT_SET_CHMOD, 0777); // decompress the archive, this parameter uses the file name to identify/reference. You can use the following array $ rule_list [0] = 'test/aaa.txt '; $ rule_list [1] = 'test/ddd.txt '; // or the following: in a string, use commas to separate each file to be extracted. $ rule_list = "test/aaa.txt, test/ddd.txt "; $ list = $ archive-> extract (PCLZIP_OPT_BY_NAME, $ rule_li St); // decompress some of the files. Using the php ereg () function, all files whose names are successfully compared will be decompressed $ list = $ archive-> extract (PCLZIP_OPT_BY_EREG, "aa"); // decompress some files. Using the php preg_match () function, all files whose names are successfully compared will be decompressed $ list = $ archive-> extract (PCLZIP_OPT_BY_PREG, "/^ bb/"); // if you do not understand the preceding two functions, study the Regular Expression (Regular Expression) first. // extract the Regular Expression based on the index of the elements in the array, however, I do not know much about index = a $ list = $ archive-> extract (PCLZIP_OPT_BY_INDEX, array ('0-1', '6-7 ')); // decompress the file content into a string $ list = $ archive-> extract (PC LZIP_OPT_BY_NAME, "data/readme.txt", PCLZIP_OPT_EXTRACT_AS_STRING); // extract the file content and output it directly (echo) $ list = $ archive-> extract (PCLZIP_OPT_BY_NAME, "data/readme.txt", PCLZIP_OPT_EXTRACT_IN_OUTPUT); // add an archive to a compressed archive, but the archive is not compressed. $ list = $ archive-> add ("data/file.txt ", PCLZIP_OPT_NO_COMPRESSION); // Add an annotation to this compression file. If there is an annotation, it will overwrite $ list = $ archive-> create ("data", PCLZIP_OPT_COMMENT, "Add a comment"); // Add an annotation to the compression file. If there is an annotation, it will be followed by $ list = $ archive-> add ("data", PCLZIP_OPT_ADD_COMMENT, "Add a comment after the existing one "); // add an annotation to the compression file. If there is an annotation, It will be placed before the original annotation $ list = $ archive-> add ("data", PCLZIP_OPT_PREPEND_COMMENT, "Add a comment before the existing one");?>

Class Library download: pclzip-2-8-2.zip

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.