


Recommend a PHP compression and decompression class PclZip_PHP tutorial
PclZip is a very powerful PHP class for compressing and decompressing zip files. The PclZip library can compress and decompress Zip format compressed files (WinZip, PKZIP); and can process such files. Including generating compressed files, listing the contents of compressed files, decompressing files, etc. At the same time, you can also add or delete files to existing ZIP packages.
Official website: http://www.phpconcept.net/pclzip/
Recently I am developing my WordPress plug-in ShareLink. In the process, I discovered PclZip, a PHP class for operating zip files. I have to recommend it. Simple, easy to use, and powerful are my evaluations of it.
Another reason for recommendation is that I discovered a lewd usage of PHP function parameters in its source code. An example will be given below.
Generate zip file
Usage 1:
<?php include_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:
<?php include_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)); } ?>
See that there are no parameters in the create method, and then look at the method prototype and you will know how slutty it is. At least I haven't used it that way.
The following is a simple code for compressing the entire site and backing it up:
<?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 '异常:'.$z->errorInfo(true); } else { echo '备份成功'; } ?>
Other ways to use:
<?php //解压缩到extract/folder/这个目录中 $list = $archive->extract(PCLZIP_OPT_PATH, "extract/folder/"); //增加这个目录在压缩档中,完成以后压缩档里面会有backup这个目录,backup里面会有这两个档案 $list = $archive->create("file.txt,image.gif",PCLZIP_OPT_ADD_PATH, "backup"); //去掉部份的路径,这里完成后会变成test/file.txt $list = $archive->add("/usr/local/user/test/file.txt",PCLZIP_OPT_REMOVE_PATH, "/usr/local/user"); //把所有路径都去掉,这个压缩档建立完后,里面就只会有file.txt跟image.gif,不会有目录了 $list = $archive->create("data/file.txt images/image.gif",PCLZIP_OPT_REMOVE_ALL_PATH); //把解压缩出来的档案的CHMOD设成0777 $list = $archive->extract(PCLZIP_OPT_SET_CHMOD, 0777); //解压缩部份的档案,这个参数是使用档案名称判别 //引数可以用下面这样的阵列 $rule_list[0] = 'test/aaa.txt'; $rule_list[1] = 'test/ddd.txt'; //或是下面这样,一个字串中,用逗号分隔每个要解压缩的档案 $rule_list = "test/aaa.txt,test/ddd.txt"; $list = $archive->extract(PCLZIP_OPT_BY_NAME,$rule_list); //解压缩部份的档案,使用php的ereg()函式,档案名称有比对成功的都会被解压缩 $list = $archive->extract(PCLZIP_OPT_BY_EREG, "aa"); //解压缩部份的档案,使用php的preg_match()函式,档案名称有比对成功的都会被解压缩 $list = $archive->extract(PCLZIP_OPT_BY_PREG, "/^bb/"); //上面这两个函式如果不懂的话,请先研究正规表示法(Regular Expression) //依照阵列中元素的索引解压缩,可是我不太懂index啥 = =a $list = $archive->extract(PCLZIP_OPT_BY_INDEX, array('0-1','6-7')); //将一个档案内容解压缩成一个字串 $list = $archive->extract(PCLZIP_OPT_BY_NAME, "data/readme.txt",PCLZIP_OPT_EXTRACT_AS_STRING); //将一个档案内容解压缩完后直接输出(echo) $list = $archive->extract(PCLZIP_OPT_BY_NAME, "data/readme.txt",PCLZIP_OPT_EXTRACT_IN_OUTPUT); //将一个档案加入一个压缩档中,但不会对此档案压缩 $list = $archive->add("data/file.txt", PCLZIP_OPT_NO_COMPRESSION); //对此压缩档增加一个注解,如果原本就有注解的话会被覆盖掉 $list = $archive->create("data", PCLZIP_OPT_COMMENT, "Add a comment"); //对此压缩档增加一个注解,如果原本就有注解的话会接在后面 $list = $archive->add("data", PCLZIP_OPT_ADD_COMMENT, "Add a comment after the existing one"); //对此压缩档增加一个注解,如果原本就有注解的话会放在原本的注解前面 $list = $archive->add("data", PCLZIP_OPT_PREPEND_COMMENT, "Add a comment before the existing one"); ?>
Class library download: pclzip-2-8-2.zip

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
