PclZip:强大的PHP压缩与解压缩zip种
PclZip:强大的PHP压缩与解压缩zip类
PclZip介绍
PclZip
library能够压缩与解压缩Zip格式的压缩档(WinZip、PKZIP);且能对此类类档案进行处理,包括产生压缩档、列出压缩档的内容以及解压
缩档案等等。由于能够在伺服器端进行压缩与解压缩的动作,所以相当方便使用。
PclZip定义一个PclZip类别,其类别物件可视为一个ZIP档案,亦提供method来进行处理。
?
如何使用PclZip
?
1.基础
所有的功能都由pclzip.lib.php这个档案提供,PclZip
library可于其首页(www.phpconcept.net/pclzip/index.en.php)下载。所有的PKZIP档案其实就是一个
PclZip的类别物件。当产生一个PclZip档案(ie,
PclZip类别物件),就会先产生一个压缩档,且档名已经指定,但此压缩档的内容尚未存在:
<?PHP require_once('pclzip.lib.php'); $archive = new PclZip("archive.zip"); ?>
此物件提供了一些public method可用来处理此档案。
?
2.参数
每一个method有其各自可使用的参数,包括有必须与非必须的参数:
<?PHP require_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); $v_list = $archive->add('dev/file.txt', PCLZIP_OPT_REMOVE_PATH, 'dev'); ?>
上例中的’dev/file.txt’就是必须参数;’PCLZIP_OPT_REMOVE_PATH’则为非必须参数。当然有些method也可以只包 含非必须的参数:
<?PHP $list = $archive->extract(PCLZIP_OPT_PATH, "folder", PCLZIP_OPT_REMOVE_PATH, "data", PCLZIP_CB_PRE_EXTRACT, "callback_pre_extract",); ?>
上例中原本压缩档内档案存放的路径为/data,不过你可以指定解压缩至/folder中。此外,在解压缩之前,会呼叫callback
function(’callback_pre_extract()’),此function可让使用者在解压缩的过程中变更档案存放路径与档名,或是选
择某些档案不解压缩。
所有可用的非必要参数可参考网址(www.phpconcept.net/pclzip/man/en/index.php)。
?
3.回传值
每个method所回传的值可能会不同,将会在每个method中说明。不过大部分的method回传0、error或是阵列。
?
4.错误处理
从版本1.3之后,错误处理已经整合至PclZip类别中,当一个method回传错误码,可以得知一些额外的讯息以方便错误处理:
* errorName():回传错误名称
* errorCode():回传错误码
* errorInfo():回传错误的描述
接下来会举几个例子来说明如何使用PclZip。
?
PclZip实例1、产生ZIP压缩档
PclZip($zipname):为PclZip constructor,$zipname为PKZIP压缩档的档名。
主要是产生一个PclZip物件,即一个PKZIP压缩档;但此时,只有压缩档产生出来,并做一些检查(例如是否有开启zlib
extension…等),除此之外,并没有做其他动作。
create($filelist, [optional arguments
list]):将参数$filelist指定的档案或目录(包含当中所有档案与子目录)加入上述所产生的压缩档中。
而非必要的参数则能够修改压缩档内的档案存放路径。
此method可用的参数可以参考网志(www.phpconcept.net/pclzip/man/en/index.php)。
下面的示例说明如何产生PKZIP压缩档(档名为archive.zip),并将file.txt、data/text.txt以及目录 folder(包含当中的档案与子目录)加入刚刚产生的archive.zip中:
用法一:
<?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 ) ); } ?>
用法二:
下面的示例说明基本上与上例一样产生archive.zip,但在将file.txt与text.txt压缩于其中时,将路径由data/改为 install/ ;因此,在archive.zip中这两个档案的路径会是install/file.txt与install/text.txt
<?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 ) ); } ?>
PclZip实例2、列出压缩档内容
listContent( ) :列出压缩档中的内容,包括档案的属性与目录:
<?PHP include_once ('pclzip.lib.php'); $zip = new PclZip ( "test.zip" ); if (($list = $zip->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] )) { echo "File $i / [$key] = " . $list [$i] [$key] . "<br>"; } echo "<br />"; } ?>
?
上例将会回传结果:
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实例3、解压缩档案
extract([options list]) :解压缩PKZIP中的档案或目录。
[options
list]可用的参数可参考网址(www.phpconcept.net/pclzip/man/en/index.php)。这些参数能让使用者在解压
缩的时候有更多的选项,譬如指定变更解压缩档案的路径、指定只解压缩某些档案或不解压缩某些档案或者是将档案解压缩成字串输出(可用于readme档)。
下例是一个简单的解压缩档案示例,将压缩档archive.zip内的档案解压缩至目前的目录:
<?PHP require_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); if ($archive->extract() == 0) { die("Error : ".$archive->errorInfo(true)); } ?>
下例是进阶的解压缩档案使用,archive.zip中所有档案都解压缩于data/中,而特别指明在install/release中的所有档案也直接 丢于data/中,而非data/install/ release:
<?PHP include('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)); } ?>
看见create方法的参数没有,再看看方法原型你就知道如何做了。至少我还没有这样用过。
相关文档:
PclZip官方地址
:http://www.phpconcept.net/pclzip/index.php
PclZip手册地
址
:http://www.phpconcept.net/pclzip/man/en/index.php
PEAR类创建
ZIP档案文件
:http://www.ccvita.com/10.html
PclZip简介与
使用
:http://www.ccvita.com/59.html
PclZip:强大
的PHP压缩与解压缩zip类
:http://www.ccvita.com/330.html

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,

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

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

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.
