Zip

目录

简介

一个用 Zip 压缩的文件存档。

类摘要

ZipArchive

class ZipArchive {

/* 属性 */

/* 方法 */

bool addEmptyDir ( string $dirname [, int $flags = 0 ] )

bool addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 [, int $flags = ZipArchive::FL_OVERWRITE ]]]] )

bool addFromString ( string $localname , string $contents [, int $flags = ZipArchive::FL_OVERWRITE ] )

array addGlob ( string $pattern [, int $flags = 0 [, array $options = array() ]] )

array addPattern ( string $pattern [, string $path = "." [, array $options = array() ]] )

bool close ( void )

public int count ( void )

bool deleteIndex ( int $index )

bool deleteName ( string $name )

bool extractTo ( string $destination [, mixed $entries ] )

string getArchiveComment ([ int $flags ] )

string getCommentIndex ( int $index [, int $flags ] )

string getCommentName ( string $name [, int $flags ] )

bool GetExternalAttributesIndex ( int $index , int &$opsys , int &$attr [, int $flags ] )

bool getExternalAttributesName ( string $name , int &$opsys , int &$attr [, int $flags ] )

string getFromIndex ( int $index [, int $length = 0 [, int $flags ]] )

string getFromName ( string $name [, int $length = 0 [, int $flags ]] )

string getNameIndex ( int $index [, int $flags ] )

string getStatusString ( void )

resource getStream ( string $name )

int locateName ( string $name [, int $flags ] )

mixed open ( string $filename [, int $flags ] )

bool registerCancelCallback ( callable $callback )

bool registerProgressCallback ( float $rate , callable $callback )

bool renameIndex ( int $index , string $newname )

bool renameName ( string $name , string $newname )

bool replaceFile ( string $filename , int $index [, int $start = 0 [, int $length = 0 [, int $flags = 0 ]]] )

bool setArchiveComment ( string $comment )

bool setCommentIndex ( int $index , string $comment )

bool setCommentName ( string $name , string $comment )

bool setCompressionIndex ( int $index , int $comp_method [, int $comp_flags = 0 ] )

bool setCompressionName ( string $name , int $comp_method [, int $comp_flags = 0 ] )

bool setEncryptionIndex ( int $index , string $method [, string $password ] )

bool setEncryptionName ( string $name , int $method [, string $password ] )

bool setExternalAttributesIndex ( int $index , int $opsys , int $attr [, int $flags ] )

bool setExternalAttributesName ( string $name , int $opsys , int $attr [, int $flags ] )

bool setMtimeIndex ( int $index , int $timestamp [, int $flags ] )

bool setMtimeName ( string $name , int $timestamp [, int $flags ] )

public bool setPassword ( string $password )

array statIndex ( int $index [, int $flags ] )

array statName ( string $name [, int $flags ] )

bool unchangeAll ( void )

bool unchangeArchive ( void )

bool unchangeIndex ( int $index )

bool unchangeName ( string $name )

}

属性

status
Zip Archive 的状态

statusSys
Zip Archive 的系统状态

numFiles
压缩包里的文件数

filename
在文件系统里的文件名

comment
压缩包的注释

ZipArchive::addEmptyDir

Add a new directory

说明

bool ZipArchive::addEmptyDir ( string $dirname [, int $flags = 0 ] )

Adds an empty directory in the archive.

参数

dirname
The directory to add.

flags
Bitmask consisting of ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437. The behaviour of these constants is described on the ZIP constants page.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

更新日志

版本说明
8.0.0 / 1.18.0flags was added.

范例

示例 #1 Create a new directory in an archive

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    if($zip->addEmptyDir('newDirectory')) {
        echo 'Created a new root directory';
    } else {
        echo 'Could not create the directory';
    }
    $zip->close();
} else {
    echo 'failed';
}
?>

ZipArchive::addFile

Adds a file to a ZIP archive from the given path

说明

bool ZipArchive::addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 [, int $flags = ZipArchive::FL_OVERWRITE ]]]] )

Adds a file to a ZIP archive from a given path.

Note: For maximum portability, it is recommended to always use forward slashes (/) as directory separator in ZIP filenames.

参数

filename
The path to the file to add.

localname
If supplied, this is the local name inside the ZIP archive that will override the filename.

start
For partial copy, start position.

length
For partial copy, length to be copied, if 0 or -1 the whole file (starting from start) is used.

flags
Bitmask consisting of ZipArchive::FL_OVERWRITE, ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437. The behaviour of these constants is described on the ZIP constants page.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

更新日志

版本说明
8.0.0 / 1.18.0flags was added.

范例

This example opens a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt.

示例 #1 Open and add

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->addFile('/path/to/index.txt', 'newname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

注释

Note:

When a file is set to be added to the archive, PHP will lock the file. The lock is only released once the class="classname">ZipArchive object has been closed, either via ZipArchive::close or the class="classname">ZipArchive object being destroyed. This may prevent you from being able to delete the file being added until after the lock has been released.

参见

  • ZipArchive::replaceFile

ZipArchive::addFromString

Add a file to a ZIP archive using its contents

说明

bool ZipArchive::addFromString ( string $localname , string $contents [, int $flags = ZipArchive::FL_OVERWRITE ] )

Add a file to a ZIP archive using its contents.

Note: For maximum portability, it is recommended to always use forward slashes (/) as directory separator in ZIP filenames.

参数

localname
The name of the entry to create.

contents
The contents to use to create the entry. It is used in a binary safe mode.

flags
Bitmask consisting of ZipArchive::FL_OVERWRITE, ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437. The behaviour of these constants is described on the ZIP constants page.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

更新日志

版本说明
8.0.0 / 1.18.0flags was added.

范例

示例 #1 Add an entry to a new archive

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('test.txt', 'file content goes here');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

示例 #2 Add file to a directory inside an archive

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->addFromString('dir/test.txt', 'file content goes here');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::addGlob

Add files from a directory by glob pattern

说明

array ZipArchive::addGlob ( string $pattern [, int $flags = 0 [, array $options = array() ]] )

Add files from a directory which match the glob pattern.

Note: For maximum portability, it is recommended to always use forward slashes (/) as directory separator in ZIP filenames.

参数

pattern
A glob pattern against which files will be matched.

flags
A bit mask of glob() flags.

options
An associative array of options. Available options are:

  • "add_path"

    Prefix to prepend when translating to the local path of the file within the archive. This is applied after any remove operations defined by the "remove_path" or "remove_all_path" options.

  • "remove_path"

    Prefix to remove from matching file paths before adding to the archive.

  • "remove_all_path"

    TRUE to use the file name only and add to the root of the archive.

  • "flags"

    Bitmask consisting of ZipArchive::FL_OVERWRITE, ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437. The behaviour of these constants is described on the ZIP constants page.

  • "comp_method"

    Compression method, one of the ZipArchive::CM_* constants, see ZIP constants page.

  • "comp_flags"

    Compression level.

  • "enc_method"

    Encryption method, one of the ZipArchive::EM_* constants, see ZIP constants page.

  • "enc_password"

    Password used for encryption.

返回值

An array of added files on success 或者在失败时返回 FALSE

更新日志

版本说明
8.0.0 / 1.18.0"flags" in options was added.
8.0.0 / 1.18.1"comp_method", "comp_flags", "enc_method" and "enc_password" in options were added.

范例

示例 #1 ZipArchive::addGlob example

Add all php scripts and text files from current working directory

<?php
$zip = new ZipArchive();
$ret = $zip->open('application.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($ret !== TRUE) {
    printf('Failed with code %d', $ret);
} else {
    $options = array('add_path' => 'sources/', 'remove_all_path' => TRUE);
    $zip->addGlob('*.{php,txt}', GLOB_BRACE, $options);
    $zip->close();
}
?>

参见

  • ZipArchive::addFile
  • ZipArchive::addPattern

ZipArchive::addPattern

Add files from a directory by PCRE pattern

说明

array ZipArchive::addPattern ( string $pattern [, string $path = "." [, array $options = array() ]] )

Add files from a directory which match the regular expression pattern. The operation is not recursive. The pattern will be matched against the file name only.

参数

pattern
A PCRE pattern against which files will be matched.

path
The directory that will be scanned. Defaults to the current working directory.

options
An associative array of options accepted by ZipArchive::addGlob.

返回值

An array of added files on success 或者在失败时返回 FALSE

范例

示例 #1 ZipArchive::addPattern example

Add all php scripts and text files from current directory

<?php
$zip = new ZipArchive();
$ret = $zip->open('application.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($ret !== TRUE) {
    printf('Failed with code %d', $ret);
} else {
    $directory = realpath('.');
    $options = array('add_path' => 'sources/', 'remove_path' => $directory);
    $zip->addPattern('/\.(?:php|txt)$/', $directory, $options);
    $zip->close();
}
?>

参见

  • ZipArchive::addFile
  • ZipArchive::addGlob

ZipArchive::close

Close the active archive (opened or newly created)

说明

bool ZipArchive::close ( void )

Close opened or created archive and save changes. This method is automatically called at the end of the script.

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::count

Counts the number of files in the archive

说明

public int ZipArchive::count ( void )

参数

此函数没有参数。

返回值

Returns the number of files in the archive.

ZipArchive::deleteIndex

Delete an entry in the archive using its index

说明

bool ZipArchive::deleteIndex ( int $index )

Delete an entry in the archive using its index.

参数

index
Index of the entry to delete.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Delete file from archive using its index

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->deleteIndex(2);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::deleteName

Delete an entry in the archive using its name

说明

bool ZipArchive::deleteName ( string $name )

Delete an entry in the archive using its name.

参数

name
Name of the entry to delete.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Deleting a file and directory from an archive, using names

<?php
$zip = new ZipArchive;
if ($zip->open('test1.zip') === TRUE) {
    $zip->deleteName('testfromfile.php');
    $zip->deleteName('testDir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::extractTo

解压缩文件

说明

bool ZipArchive::extractTo ( string $destination [, mixed $entries ] )

将压缩文件解压缩到指定的目录

参数

destination
解压缩的本地目标路径

entries
解压缩的文件。它接受一个单独的文件名或一个文件名数组。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Extract all entries

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

示例 #2 Extract two entries

<?php
$zip = new ZipArchive;
$res = $zip->open('test_im.zip');
if ($res === TRUE) {
    $zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::getArchiveComment

Returns the Zip archive comment

说明

string ZipArchive::getArchiveComment ([ int $flags ] )

Returns the Zip archive comment.

参数

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged comment is returned.

返回值

Returns the Zip archive comment 或者在失败时返回 FALSE.

范例

示例 #1 Dump an archive comment

<?php
$zip = new ZipArchive;
$res = $zip->open('test_with_comment.zip');
if ($res === TRUE) {
    var_dump($zip->getArchiveComment());
    /* Or using the archive property */
    var_dump($zip->comment);
} else {
    echo 'failed, code:' . $res;
}
?>

ZipArchive::getCommentIndex

Returns the comment of an entry using the entry index

说明

string ZipArchive::getCommentIndex ( int $index [, int $flags ] )

Returns the comment of an entry using the entry index.

参数

index
Index of the entry

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged comment is returned.

返回值

Returns the comment on success 或者在失败时返回 FALSE.

范例

示例 #1 Dump an entry comment

<?php
$zip = new ZipArchive;
$res = $zip->open('test1.zip');
if ($res === TRUE) {
    var_dump($zip->getCommentIndex(1));
} else {
    echo 'failed, code:' . $res;
}
?>

ZipArchive::getCommentName

Returns the comment of an entry using the entry name

说明

string ZipArchive::getCommentName ( string $name [, int $flags ] )

Returns the comment of an entry using the entry name.

参数

name
Name of the entry

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged comment is returned.

返回值

Returns the comment on success 或者在失败时返回 FALSE.

范例

示例 #1 Dump an entry comment

<?php
$zip = new ZipArchive;
$res = $zip->open('test1.zip');
if ($res === TRUE) {
    var_dump($zip->getCommentName('test/entry1.txt'));
} else {
    echo 'failed, code:' . $res;
}
?>

ZipArchive::getExternalAttributesIndex

Retrieve the external attributes of an entry defined by its index

说明

bool ZipArchive::GetExternalAttributesIndex ( int $index , int &$opsys , int &$attr [, int $flags ] )

Retrieve the external attributes of an entry defined by its index.

参数

index
Index of the entry.

opsys
On success, receive the operating system code defined by one of the ZipArchive::OPSYS_ constants.

attr
On success, receive the external attributes. Value depends on operating system.

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged attributes are returned.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example extract all the entries of a ZIP archive test.zip and set the Unix rights from external attributes.

示例 #1 Extract all entries with Unix rights

<?php
$zip = new ZipArchive();
if ($zip->open('test.zip') === TRUE) {
    for ($idx=0 ; $s = $zip->statIndex($idx) ; $idx++) {
        if ($zip->extractTo('.', $s['name'])) {
            if ($zip->getExternalAttributesIndex($idx, $opsys, $attr) 
                && $opsys==ZipArchive::OPSYS_UNIX) {
               chmod($s['name'], ($attr >> 16) & 0777);
            }
        }
    }
    $zip->close();
    echo "Ok\n";
} else {
    echo "KO\n";
}
?>

ZipArchive::getExternalAttributesName

Retrieve the external attributes of an entry defined by its name

说明

bool ZipArchive::getExternalAttributesName ( string $name , int &$opsys , int &$attr [, int $flags ] )

Retrieve the external attributes of an entry defined by its name.

参数

name
Name of the entry.

opsys
On success, receive the operating system code defined by one of the ZipArchive::OPSYS_ constants.

attr
On success, receive the external attributes. Value depends on operating system.

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged attributes are returned.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::getFromIndex

Returns the entry contents using its index

说明

string ZipArchive::getFromIndex ( int $index [, int $length = 0 [, int $flags ]] )

Returns the entry contents using its index.

参数

index
Index of the entry

length
The length to be read from the entry. If 0, then the entire entry is read.

flags
The flags to use to open the archive. the following values may be ORed to it.

  • ZipArchive::FL_UNCHANGED

  • ZipArchive::FL_COMPRESSED

返回值

Returns the contents of the entry on success 或者在失败时返回 FALSE.

范例

示例 #1 Get the file contents

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    echo $zip->getFromIndex(2);
    $zip->close();
} else {
    echo 'failed';
}
?>

参见

  • ZipArchive::getFromName

ZipArchive::getFromName

Returns the entry contents using its name

说明

string ZipArchive::getFromName ( string $name [, int $length = 0 [, int $flags ]] )

Returns the entry contents using its name.

参数

name
Name of the entry

length
The length to be read from the entry. If 0, then the entire entry is read.

flags
The flags to use to find the entry. The following values may be ORed.

  • ZipArchive::FL_UNCHANGED

  • ZipArchive::FL_COMPRESSED

  • ZipArchive::FL_NOCASE

返回值

Returns the contents of the entry on success 或者在失败时返回 FALSE.

范例

示例 #1 Get the file contents

<?php
$zip = new ZipArchive;
if ($zip->open('test1.zip') === TRUE) {
    echo $zip->getFromName('testfromfile.php');
    $zip->close();
} else {
    echo 'failed';
}
?>

示例 #2 Convert an image from a zip entry

<?php
$z = new ZipArchive();
if ($z->open(dirname(__FILE__) . '/test_im.zip')) {
    $im_string = $z->getFromName("pear_item.gif");
    $im = imagecreatefromstring($im_string);
    imagepng($im, 'b.png');
}
?>

参见

  • ZipArchive::getFromIndex

ZipArchive::getNameIndex

Returns the name of an entry using its index

说明

string ZipArchive::getNameIndex ( int $index [, int $flags ] )

Returns the name of an entry using its index.

参数

index
Index of the entry.

flags
If flags is set to ZipArchive::FL_UNCHANGED, the original unchanged name is returned.

返回值

Returns the name on success 或者在失败时返回 FALSE.

范例

示例 #1 ZipArchive::getNameIndex example

<?php
if ($zip->open('test.zip') == TRUE) {
 for ($i = 0; $i < $zip->numFiles; $i++) {
     $filename = $zip->getNameIndex($i);
     // ...
 }
}
?>

ZipArchive::getStatusString

Returns the status error message, system and/or zip messages

说明

string ZipArchive::getStatusString ( void )

Returns the status error message, system and/or zip messages.

参数

此函数没有参数。

返回值

Returns a string with the status message on success 或者在失败时返回 FALSE.

更新日志

版本说明
8.0.0 / 1.18.0This method can be called on closed archive.

ZipArchive::getStream

Get a file handler to the entry defined by its name (read only)

说明

resource ZipArchive::getStream ( string $name )

Get a file handler to the entry defined by its name. For now it only supports read operations.

参数

name
The name of the entry to use.

返回值

Returns a file pointer (resource) on success 或者在失败时返回 FALSE.

范例

示例 #1 Get the entry contents with fread and store it

<?php
$contents = '';
$z = new ZipArchive();
if ($z->open('test.zip')) {
    $fp = $z->getStream('test');
    if(!$fp) exit("failed\n");

    while (!feof($fp)) {
        $contents .= fread($fp, 2);
    }

    fclose($fp);
    file_put_contents('t',$contents);
    echo "done.\n";
}
?>

示例 #2 Same as the previous example but with fopen and the zip stream wrapper

<?php
$contents = '';
$fp = fopen('zip://' . dirname(__FILE__) . '/test.zip#test', 'r');
if (!$fp) {
    exit("cannot open\n");
}
while (!feof($fp)) {
    $contents .= fread($fp, 2);
}
echo "$contents\n";
fclose($fp);
echo "done.\n";
?>

示例 #3 Stream wrapper and image, can be used with the xml function as well

<?php
$im = imagecreatefromgif('zip://' . dirname(__FILE__) . '/test_im.zip#pear_item.gif');
imagepng($im, 'a.png');
?>

ZipArchive::locateName

Returns the index of the entry in the archive

说明

int ZipArchive::locateName ( string $name [, int $flags ] )

Locates an entry using its name.

参数

name
The name of the entry to look up

flags
The flags are specified by ORing the following values, or 0 for none of them.

  • ZipArchive::FL_NOCASE

  • ZipArchive::FL_NODIR

返回值

Returns the index of the entry on success 或者在失败时返回 FALSE.

范例

示例 #1 Create an archive and then use it with ZipArchive::locateName

<?php
$file = 'testlocate.zip';

$zip = new ZipArchive;
if ($zip->open($file, ZipArchive::CREATE) !== TRUE) {
    exit('failed');
}

$zip->addFromString('entry1.txt', 'entry #1');
$zip->addFromString('entry2.txt', 'entry #2');
$zip->addFromString('dir/entry2d.txt', 'entry #2');

if (!$zip->status == ZipArchive::ER_OK) {
    echo "failed to write zip\n";
}
$zip->close();

if ($zip->open($file) !== TRUE) {
    exit('failed');
}

echo $zip->locateName('entry1.txt') . "\n";
echo $zip->locateName('eNtry2.txt') . "\n";
echo $zip->locateName('eNtry2.txt', ZipArchive::FL_NOCASE) . "\n";
echo $zip->locateName('enTRy2d.txt', ZipArchive::FL_NOCASE|ZipArchive::FL_NODIR) . "\n";
$zip->close();

?>

以上例程会输出:

0

1
2

ZipArchive::open

Open a ZIP file archive

说明

mixed ZipArchive::open ( string $filename [, int $flags ] )

Opens a new or existing zip archive for reading, writing or modifying.

Since libzip 1.6.0, a empty file is not a valid archive any longer.

参数

filename
The file name of the ZIP archive to open.

flags
The mode to use to open the archive.

  • ZipArchive::OVERWRITE

  • ZipArchive::CREATE

  • ZipArchive::RDONLY

  • ZipArchive::EXCL

  • ZipArchive::CHECKCONS

返回值

Error codes
Returns TRUE on success or the error code.

  • ZipArchive::ER_EXISTS

    File already exists.

  • ZipArchive::ER_INCONS

    Zip archive inconsistent.

  • ZipArchive::ER_INVAL

    Invalid argument.

  • ZipArchive::ER_MEMORY

    Malloc failure.

  • ZipArchive::ER_NOENT

    No such file.

  • ZipArchive::ER_NOZIP

    Not a zip archive.

  • ZipArchive::ER_OPEN

    Can't open file.

  • ZipArchive::ER_READ

    Read error.

  • ZipArchive::ER_SEEK

    Seek error.

范例

示例 #1 Open and extract

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    echo 'ok';
    $zip->extractTo('test');
    $zip->close();
} else {
    echo 'failed, code:' . $res;
}
?>

示例 #2 Create an archive

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('test.txt', 'file content goes here');
    $zip->addFile('data.txt', 'entryname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

示例 #3 Create an temporary archive

<?php
$name = tempnam(sys_get_temp_dir(), "FOO");
$zip = new ZipArchive;
$res = $zip->open($name, ZipArchive::OVERWRITE); /* truncate as empty file is not valid */
if ($res === TRUE) {
    $zip->addFile('data.txt', 'entryname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::registerCancelCallback

Register a callback to allow cancellation during archive close.

说明

bool ZipArchive::registerCancelCallback ( callable $callback )

Register a callback function to allow cancellation during archive close.

参数

callback
If this function return 0 operation will continue, other value it will be cancelled.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

This function is only available if built against libzip ≥ 1.6.0.

范例

This example creates a ZIP file archive php.zip and cancel operation on some run condition.

示例 #1 Archive a file

<?php
$zip = new ZipArchive();
if ($zip->open('php.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
    $zip->addFile(PHP_BINARY, 'php');
    $zip->registerCancelCallback(function () {
        return ($someruncondition ? -1 : 0);
    });
    $zip->close();
}

参见

  • ZipArchive::registerProgressCallback

ZipArchive::registerProgressCallback

Register a callback to provide updates during archive close.

说明

bool ZipArchive::registerProgressCallback ( float $rate , callable $callback )

Register a callback function to provide updates during archive close.

参数

rate
Change between each call of the callback (from 0.0 to 1.0).

callback
This function will receive the current state as a float (from 0.0 to 1.0).

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

This function is only available if built against libzip ≥ 1.3.0.

范例

This example creates a ZIP file archive php.zip and show progression.

示例 #1 Archive a file

$zip = new ZipArchive();
if ($zip->open('php.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
    $zip->addFile(PHP_BINARY, 'php');
    $zip->registerProgressCallback(0.05, function ($r) {
        printf("%d%%\n", $r * 100);
    });
    $zip->close();
}

参见

  • ZipArchive::registerCancelCallback

ZipArchive::renameIndex

Renames an entry defined by its index

说明

bool ZipArchive::renameIndex ( int $index , string $newname )

Renames an entry defined by its index.

参数

index
Index of the entry to rename.

newname
New name.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Rename one entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->renameIndex(2,'newname.txt');
    $zip->close();
} else {
    echo 'failed, code:' . $res;
}
?>

ZipArchive::renameName

Renames an entry defined by its name

说明

bool ZipArchive::renameName ( string $name , string $newname )

Renames an entry defined by its name.

参数

name
Name of the entry to rename.

newname
New name.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Rename one entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->renameName('currentname.txt','newname.txt');
    $zip->close();
} else {
    echo 'failed, code:' . $res;
}
?>

ZipArchive::replaceFile

Replace file in ZIP archive with a given path

说明

bool ZipArchive::replaceFile ( string $filename , int $index [, int $start = 0 [, int $length = 0 [, int $flags = 0 ]]] )

Replace file in ZIP archive with a given path.

Note: For maximum portability, it is recommended to always use forward slashes (/) as directory separator in ZIP filenames.

参数

filename
The path to the file to add.

index
The index of the file to be replaced, its name is unchanged.

start
For partial copy, start position.

length
For partial copy, length to be copied, if 0 or -1 the whole file (starting from start) is used.

flags
Bitmask consisting of ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437. The behaviour of these constants is described on the ZIP constants page.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example opens a ZIP file archive test.zip and replaces index 1 entry with /path/to/index.txt.

示例 #1 Open and replace

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->replaceFile('/path/to/index.txt', 1);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

参见

  • ZipArchive::addFile

ZipArchive::setArchiveComment

Set the comment of a ZIP archive

说明

bool ZipArchive::setArchiveComment ( string $comment )

Set the comment of a ZIP archive.

参数

comment
The contents of the comment.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Create an archive and set a comment

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('test.txt', 'file content goes here');
    $zip->setArchiveComment('new archive comment');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::setCommentIndex

Set the comment of an entry defined by its index

说明

bool ZipArchive::setCommentIndex ( int $index , string $comment )

Set the comment of an entry defined by its index.

参数

index
Index of the entry.

comment
The contents of the comment.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Open an archive and set a comment for an entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->setCommentIndex(2, 'new entry comment');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::setCommentName

Set the comment of an entry defined by its name

说明

bool ZipArchive::setCommentName ( string $name , string $comment )

Set the comment of an entry defined by its name.

参数

name
Name of the entry.

comment
The contents of the comment.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Open an archive and set a comment for an entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    $zip->setCommentName('entry1.txt', 'new entry comment');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::setCompressionIndex

Set the compression method of an entry defined by its index

说明

bool ZipArchive::setCompressionIndex ( int $index , int $comp_method [, int $comp_flags = 0 ] )

Set the compression method of an entry defined by its index.

参数

index
Index of the entry.

comp_method
The compression method, one of the ZipArchive::CM_* constants.

comp_flags
Compression level.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Add files with different compression methods to an archive

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('foo', 'Some text');
    $zip->addFromString('bar', 'Some other text');
    $zip->setCompressionIndex(0, ZipArchive::CM_STORE);
    $zip->setCompressionIndex(1, ZipArchive::CM_DEFLATE);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::setCompressionName

Set the compression method of an entry defined by its name

说明

bool ZipArchive::setCompressionName ( string $name , int $comp_method [, int $comp_flags = 0 ] )

Set the compression method of an entry defined by its name.

参数

name
Name of the entry.

comp_method
The compression method, one of the ZipArchive::CM_* constants.

comp_flags
Compression level.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Add files with different compression methods to an archive

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('foo', 'Some text');
    $zip->addFromString('bar', 'Some other text');
    $zip->setCompressionName('foo', ZipArchive::CM_STORE);
    $zip->setCompressionName('bar', ZipArchive::CM_DEFLATE);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

示例 #2 Add file and set compression method

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile('foo.jpg', 'bar.jpg');
    $zip->setCompressionName('bar.jpg', ZipArchive::CM_XZ);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

ZipArchive::setEncryptionIndex

Set the encryption method of an entry defined by its index

说明

bool ZipArchive::setEncryptionIndex ( int $index , string $method [, string $password ] )

Set the encryption method of an entry defined by its index.

参数

index
Index of the entry.

method
The encryption method defined by one of the ZipArchive::EM_ constants.

password
Optional password, default used when missing.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

This function is only available if built against libzip ≥ 1.2.0.

参见

  • ZipArchive::setPassword
  • ZipArchive::setEncryptionName

ZipArchive::setEncryptionName

Set the encryption method of an entry defined by its name

说明

bool ZipArchive::setEncryptionName ( string $name , int $method [, string $password ] )

Set the encryption method of an entry defined by its name.

参数

name
Name of the entry.

method
The encryption method defined by one of the ZipArchive::EM_ constants.

password
Optional password, default used when missing.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example creates a ZIP file archive test.zip and add the file test.txt encrypted using the AES 256 method.

示例 #1 Archive and encrypt a file

<?php
$zip = new ZipArchive();
if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
    $zip->setPassword('secret');
    $zip->addFile('text.txt');
    $zip->setEncryptionName('text.txt', ZipArchive::EM_AES_256);
    $zip->close();
    echo "Ok\n";
} else {
    echo "KO\n";
}
?>

注释

Note:

This function is only available if built against libzip ≥ 1.2.0.

参见

  • ZipArchive::setPassword
  • ZipArchive::setEncryptionIndex

ZipArchive::setExternalAttributesIndex

Set the external attributes of an entry defined by its index

说明

bool ZipArchive::setExternalAttributesIndex ( int $index , int $opsys , int $attr [, int $flags ] )

Set the external attributes of an entry defined by its index.

参数

index
Index of the entry.

opsys
The operating system code defined by one of the ZipArchive::OPSYS_ constants.

attr
The external attributes. Value depends on operating system.

flags
Optional flags. Currently unused.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::setExternalAttributesName

Set the external attributes of an entry defined by its name

说明

bool ZipArchive::setExternalAttributesName ( string $name , int $opsys , int $attr [, int $flags ] )

Set the external attributes of an entry defined by its name.

参数

name
Name of the entry.

opsys
The operating system code defined by one of the ZipArchive::OPSYS_ constants.

attr
The external attributes. Value depends on operating system.

flags
Optional flags. Currently unused.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example opens a ZIP file archive test.zip and add the file test.txt with its Unix rights as external attributes.

示例 #1 Archive a file, with its Unix rights

<?php
$zip = new ZipArchive();
$stat = stat($filename='test.txt');
if (is_array($stat) && $zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
    $zip->addFile($filename);
    $zip->setExternalAttributesName($filename, ZipArchive::OPSYS_UNIX, $stat['mode'] << 16);
    $zip->close();
    echo "Ok\n";
} else {
    echo "KO\n";
}
?>

ZipArchive::setMtimeIndex

Set the modification time of an entry defined by its index

说明

bool ZipArchive::setMtimeIndex ( int $index , int $timestamp [, int $flags ] )

Set the modification time of an entry defined by its index.

参数

index
Index of the entry.

timestamp
The modification time (unix timestamp) of the file.

flags
Optional flags, unused for now.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example creates a ZIP file archive test.zip and add the file test.txt with its modification date.

示例 #1 Archive a file

<?php
$zip = new ZipArchive();
if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
    $zip->addFile('text.txt');
    $zip->setMtimIndex(0, mktime(0,0,0,12,25,2019));
    $zip->close();
    echo "Ok\n";
} else {
    echo "KO\n";
}
?>

注释

Note:

This function is only available if built against libzip ≥ 1.0.0.

参见

  • ZipArchive::setMtimeName

ZipArchive::setMtimeName

Set the modification time of an entry defined by its name

说明

bool ZipArchive::setMtimeName ( string $name , int $timestamp [, int $flags ] )

Set the modification time of an entry defined by its name.

参数

name
Name of the entry.

timestamp
The modification time (unix timestamp) of the file.

flags
Optional flags, unused for now.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

This example creates a ZIP file archive test.zip and add the file test.txt with its modification date.

示例 #1 Archive a file

<?php
$zip = new ZipArchive();
if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
    $zip->addFile('text.txt');
    $zip->setMtimeName('text.txt', mktime(0,0,0,12,25,2019));
    $zip->close();
    echo "Ok\n";
} else {
    echo "KO\n";
}
?>

注释

Note:

This function is only available if built against libzip ≥ 1.0.0.

参见

  • ZipArchive::setMtimeIndex

ZipArchive::setPassword

Set the password for the active archive

说明

public bool ZipArchive::setPassword ( string $password )

Sets the password for the active archive.

参数

password
The password to be used for the archive.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Note:

As of PHP 7.2.0 and libzip 1.2.0 the password is used to decompress the archive, and is also the default password for class="methodname">ZipArchive::setEncryptionName and class="methodname">ZipArchive::setEncryptionIndex. Formerly, this function only set the password to be used to decompress the archive; it did not turn a non-password-protected class="classname">ZipArchive into a password-protected class="classname">ZipArchive.

参见

  • ZipArchive::setEncryptionIndex
  • ZipArchive::setEncryptionName

ZipArchive::statIndex

Get the details of an entry defined by its index

说明

array ZipArchive::statIndex ( int $index [, int $flags ] )

The function obtains information about the entry defined by its index.

参数

index
Index of the entry

flags
ZipArchive::FL_UNCHANGED may be ORed to it to request information about the original file in the archive, ignoring any changes made.

返回值

Returns an array containing the entry details 或者在失败时返回 FALSE.

范例

示例 #1 Dump the stat info of an entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    print_r($zip->statIndex(3));
    $zip->close();
} else {
    echo 'failed, code:' . $res;
}
?>

以上例程的输出类似于:

Array
(
    [name] => foobar/baz
    [index] => 3
    [crc] => 499465816
    [size] => 27
    [mtime] => 1123164748
    [comp_size] => 24
    [comp_method] => 8
)

ZipArchive::statName

Get the details of an entry defined by its name

说明

array ZipArchive::statName ( string $name [, int $flags ] )

The function obtains information about the entry defined by its name.

参数

name
Name of the entry

flags
The flags argument specifies how the name lookup should be done. Also, ZipArchive::FL_UNCHANGED may be ORed to it to request information about the original file in the archive, ignoring any changes made.

  • ZipArchive::FL_NOCASE

  • ZipArchive::FL_NODIR

  • ZipArchive::FL_UNCHANGED

返回值

Returns an array containing the entry details 或者在失败时返回 FALSE.

范例

示例 #1 Dump the stat info of an entry

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
    print_r($zip->statName('foobar/baz'));
    $zip->close();
} else {
    echo 'failed, code:' . $res;
}
?>

以上例程的输出类似于:

Array
(
    [name] => foobar/baz
    [index] => 3
    [crc] => 499465816
    [size] => 27
    [mtime] => 1123164748
    [comp_size] => 24
    [comp_method] => 8
)

ZipArchive::unchangeAll

Undo all changes done in the archive

说明

bool ZipArchive::unchangeAll ( void )

Undo all changes done in the archive.

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::unchangeArchive

Revert all global changes done in the archive

说明

bool ZipArchive::unchangeArchive ( void )

Revert all global changes to the archive. For now, this only reverts archive comment changes.

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::unchangeIndex

Revert all changes done to an entry at the given index

说明

bool ZipArchive::unchangeIndex ( int $index )

Revert all changes done to an entry at the given index.

参数

index
Index of the entry.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ZipArchive::unchangeName

Revert all changes done to an entry with the given name

说明

bool ZipArchive::unchangeName ( string $name )

Revert all changes done to an entry.

参数

name
Name of the entry.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE