PHP Zip压缩

2024-03-15 26

PHP提供了扩展来实现对ZIP压缩档案和其中文件的读取和写入操作。通过这个扩展,用户可以轻松地对ZIP压缩文件进行操作,包括创建、提取和修改ZIP档案中的文件。让我们一起深入探讨如何在PHP中使用这个扩展来处理ZIP压缩文件。

一、安装

1、Linux 系统

  • 自PHP 7.4.0版本开始,必须在编译PHP时使用–with-zip配置选项来启用zip支持。在此之前的PHP版本中,需使用–enable-zip选项。自PHP 7.4.0版本起,不再捆绑libzip。
  • 从PHP 7.3.0版本开始,不建议使用捆绑的libzip进行构建,但仍可通过在配置中添加–without-libzip参数来实现。
  • 新增了–with-libzip=DIR配置选项,以便使用系统安装的libzip。需要libzip版本0.11,建议使用0.11.2或更高版本。

2、Windows系统

自PHP 8.2.0版本开始,需在php.ini中启用php_zip.dll动态链接库。之前该扩展是内置的。

二、预定义常量

下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。

ZipArchive 使用类常量。有各种类型的常量,主要有:Flag(以 FL_ 为前缀)、全局 Flag(以 AFL_ 为前缀)、错误(以 ER_ 为前缀)以及模式(没有前缀)。

1、归档打开模式

  • ZIPARCHIVE::CREATE (int):如果不存在则创建归档。
  • ZIPARCHIVE::OVERWRITE (int):如果归档存在,则忽略其当前内容。换句话说,处理方式与空归档相同。
  • ZIPARCHIVE::EXCL (int):如果归档已经存在,则出错。
  • ZipArchive::RDONLY (int):只读模式打开归档。 在 PHP 7.4.3 和 PECL zip 1.17.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.0.0。
  • ZIPARCHIVE::CHECKCONS (int):对归档执行额外的一致性检查,如果失败则显示错误。

2、归档全局flag

  • ZipArchive::AFL_RDONLY (int):归档只读,无法清除。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。
  • ZipArchive::AFL_IS_TORRENTZIP (int):当前归档是 torrentzipped(使用 BitTorrent 压缩)。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。
  • ZipArchive::AFL_WANT_TORRENTZIP (int):使用 torrentzip 格式写入归档。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。
  • ZipArchive::AFL_CREATE_OR_KEEP_FILE_FOR_EMPTY_ARCHIVE (int):归档为空时不要删除文件。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。

3、归档flag

  • ZIPARCHIVE::FL_NOCASE (int):名称查找时忽略大小写。
  • ZIPARCHIVE::FL_NODIR (int):忽略目录部分。
  • ZIPARCHIVE::FL_COMPRESSED (int):读取压缩数据。
  • ZIPARCHIVE::FL_UNCHANGED (int):使用原始数据,忽略更改。
  • ZipArchive::FL_RECOMPRESS (int):强制重新压缩数据。 PHP 8.0.0 和 PECL zip 1.18.0 起可以使用。 从 PHP 8.3.0 和 PECL zip 1.22.1 起已被弃用, 将在 libzip 的未来版本中被删除。
  • ZipArchive::FL_ENCRYPTED (int):读取加密数据(意味着 FL_COMPRESSED)。 PHP 8.0.0 和 PECL zip 1.18.0 起可以使用。
  • ZipArchive::FL_OVERWRITE (int):如果文件名称存在,重写(覆盖)它。 PHP 8.0.0 和 PECL zip 1.18.0 起可以使用。
  • ZipArchive::FL_LOCAL (int):在本地 header 中。 PHP 8.0.0 和 PECL zip 1.18.0 起可以使用。
  • ZipArchive::FL_CENTRAL (int):In central directory. PHP 8.0.0 和 PECL zip 1.18.0 起可以使用。
  • ZipArchive::FL_ENC_GUESS (int):猜测字符串编码(默认情况)。从 PHP 7.0.8 起可用。
  • ZipArchive::FL_ENC_RAW (int):获取未修改的字符串。从 PHP 7.0.8 起可用。
  • ZipArchive::FL_ENC_STRICT (int):严格遵循规范要求。从 PHP 7.0.8 起可用。
  • ZipArchive::FL_ENC_UTF_8 (int):字符串是 UTF-8 编码。从 PHP 7.0.8 起可用。
  • ZipArchive::FL_ENC_CP437 (int):字符串是 CP437 编码。从 PHP 7.0.8 起可用。
  • ZipArchive::FL_OPEN_FILE_NOW (int):在添加文件,而不是等待归档关闭时打开文件。需要注意文件描述符消耗的情况。 从 PHP 8.3.0 和 PECL zip 1.22.1 起可用。

4、压缩模式

  • ZIPARCHIVE::CM_DEFAULT (int):更好的压缩或存储。
  • ZIPARCHIVE::CM_STORE (int):存储(不压缩)。
  • ZIPARCHIVE::CM_SHRINK (int):收缩。
  • ZIPARCHIVE::CM_REDUCE_1 (int):用因子 1 换算。
  • ZIPARCHIVE::CM_REDUCE_2 (int):用因子 2 换算。
  • ZIPARCHIVE::CM_REDUCE_3 (int):用因子 3 换算。
  • ZIPARCHIVE::CM_REDUCE_4 (int):用因子 4 换算。
  • ZIPARCHIVE::CM_IMPLODE (int):聚爆。
  • ZIPARCHIVE::CM_DEFLATE (int):deflated。
  • ZIPARCHIVE::CM_DEFLATE64 (int):deflate64。
  • ZIPARCHIVE::CM_PKWARE_IMPLODE (int):PKWARE imploding。
  • ZIPARCHIVE::CM_BZIP2 (int):BZIP2 算法。
  • ZipArchive::CM_LZMA (int):LZMA 算法。
  • ZipArchive::CM_LZMA2 (int):LZMA2 algorithm. Available as of PHP 7.4.3 and PECL zip 1.16.0, respectively, if built against libzip ≥ 1.6.0.。
  • ZipArchive::CM_ZSTD (int):Zstandard(zstd)压缩算法。如果针对 libzip ≥ 1.8.0 编译,则分别从 PHP 8.0.0 和 PECL zip 1.19.1 开始可用。
  • ZipArchive::CM_XZ (int):XZ 算法。 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.6.0。
  • ZipArchive::CM_TERSE (int)
  • ZipArchive::CM_LZ77 (int)
  • ZipArchive::CM_WAVPACK (int)
  • ZipArchive::CM_PPMD (int)

5、错误

  • ZIPARCHIVE::ER_OK (int):没有错误。
  • ZIPARCHIVE::ER_MULTIDISK (int):不支持多磁盘 zip 归档。
  • ZIPARCHIVE::ER_RENAME (int):重命名临时文件失败。
  • ZIPARCHIVE::ER_CLOSE (int):关闭 zip 归档失败。
  • ZIPARCHIVE::ER_SEEK (int):寻址错误。
  • ZIPARCHIVE::ER_READ (int):读取错误。
  • ZIPARCHIVE::ER_WRITE (int):写入错误。
  • ZIPARCHIVE::ER_CRC (int):CRC校验失败。
  • ZIPARCHIVE::ER_ZIPCLOSED (int):ip 归档已关闭。
  • ZIPARCHIVE::ER_NOENT (int):没有文件。
  • ZIPARCHIVE::ER_EXISTS (int):文件已经存在。
  • ZIPARCHIVE::ER_OPEN (int):不能打开文件。
  • ZIPARCHIVE::ER_TMPOPEN (int):创建临时文件失败。
  • ZIPARCHIVE::ER_ZLIB (int):Zlib错误。
  • ZIPARCHIVE::ER_MEMORY (int):内存分配失败。
  • ZIPARCHIVE::ER_CHANGED (int):条目已被改变。
  • ZIPARCHIVE::ER_COMPNOTSUPP (int):不支持的压缩方式。
  • ZIPARCHIVE::ER_EOF (int):过早的EOF。
  • ZIPARCHIVE::ER_INVAL (int):无效的参数。
  • ZIPARCHIVE::ER_NOZIP (int):不是 zip 归档。
  • ZIPARCHIVE::ER_INTERNAL (int):内部错误。
  • ZIPARCHIVE::ER_INCONS (int):Zip 归档不一致。
  • ZIPARCHIVE::ER_REMOVE (int):不能移除文件。
  • ZIPARCHIVE::ER_DELETED (int):条目已删除。
  • ZipArchive::ER_ENCRNOTSUPP (int):不支持的压缩方式。 PHP 7.4.3 和 PECL zip 1.16.1 起可用。
  • ZipArchive::ER_RDONLY (int):归档只读。 分别于 PHP 7.4.3 和 PECL zip 1.16.1 起可用。
  • ZipArchive::ER_NOPASSWD (int):没有提供密码。 分别于 PHP 7.4.3 和 PECL zip 1.16.1 起可用。
  • ZipArchive::ER_WRONGPASSWD (int):提供的密码错误。 分别于 PHP 7.4.3 和 PECL zip 1.16.1 起可用。
  • ZipArchive::ER_OPNOTSUPP (int):操作不支持。 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.0.0。
  • ZipArchive::ER_INUSE (int):资源仍在使用。 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.0.0。
  • ZipArchive::ER_TELL (int):Tell error. 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.0.0。
  • ZipArchive::ER_COMPRESSED_DATA (int):压缩数据无效。 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.6.0。
  • ZipArchive::ER_CANCELLED (int):操作已取消。 在 PHP 7.4.3 和 PECL zip 1.16.1 中可用,前提是编译时使用的 libzip 版本大于等于 1.6.0。
  • ZipArchive::ER_DATA_LENGTH (int):数据长度异常。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。
  • ZipArchive::ER_NOT_ALLOWED (int):在 torrentzip 中不允许。如果构建时使用的是 libzip ≥ 1.10.0,分别在 PHP 8.3.0 和 PECL zip 1.22.0 中可用。

6、加密模式

  • ZipArchive::EM_NONE (int):没有加密。分别从 PHP 7.2.0 和 PECL zip 1.14.0 开始可用。
  • ZipArchive::EM_TRAD_PKWARE (int):传统 PKWARE 加密。分别从 PHP 8.0.0 和 PECL zip 1.19.0 开始可用。
  • ZipArchive::EM_AES_128 (int):AES 128 encryption, since PHP 7.2.0, PECL zip 1.14.0
  • ZipArchive::EM_AES_192 (int):AES 1192 encryption, since PHP 7.2.0, PECL zip 1.14.0
  • ZipArchive::EM_AES_256 (int):AES 256 encryption, since PHP 7.2.0, PECL zip 1.14.0
  • ZipArchive::EM_UNKNOWN (int):未知的加密算法。分别从 PHP 8.0.0 和 PECL zip 1.19.0 开始可用。

7、length参数常量

ZipArchive::LENGTH_TO_END (int):使用的文件大小,如果文件增大,将会忽略附加数据,如果文件变小,则会引发错误(ZipArchive::ER_DATA_LENGTH)。 自 PHP 8.3.0 和 PECL zip 1.22.2 起可用。

ZipArchive::LENGTH_UNCHECKED (int):使用所有可用的数据。如果针对 libzip ≥ 1.10.1 编译,自 PHP 8.3.0 和 PECL zip 1.22.2 起可用。

8、其它常量

ZipArchive::LIBZIP_VERSION (string):Zip 库版本。从 PHP 7.4.3 和 PECL zip 1.16.0 开始起可用。

9、外部属性的操作系统常量

  • ZipArchive::OPSYS_DOS (int)
  • ZipArchive::OPSYS_AMIGA (int)
  • ZipArchive::OPSYS_OPENVMS (int)
  • ZipArchive::OPSYS_UNIX (int)
  • ZipArchive::OPSYS_VM_CMS (int)
  • ZipArchive::OPSYS_ATARI_ST (int)
  • ZipArchive::OPSYS_OS_2 (int)
  • ZipArchive::OPSYS_MACINTOSH (int)
  • ZipArchive::OPSYS_Z_SYSTEM (int)
  • ZipArchive::OPSYS_CPM (int)
  • ZipArchive::OPSYS_WINDOWS_NTFS (int)
  • ZipArchive::OPSYS_MVS (int)
  • ZipArchive::OPSYS_VSE (int)
  • ZipArchive::OPSYS_ACORN_RISC (int)
  • ZipArchive::OPSYS_VFAT (int)
  • ZipArchive::OPSYS_ALTERNATE_MVS (int)
  • ZipArchive::OPSYS_BEOS (int)
  • ZipArchive::OPSYS_TANDEM (int)
  • ZipArchive::OPSYS_OS_400 (int)
  • ZipArchive::OPSYS_OS_X (int)
  • ZipArchive::OPSYS_DEFAULT (int):自 PECL zip 1.12.4 起可用

三、示例

1、创建 Zip 归档

<?php
$zip = new ZipArchive();
$filename = "./test112.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>

2、输出文档细节和列表

<?php
$za = new ZipArchive();
$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
echo "numFiles: " . $za->numFiles . "\n";
echo "status: " . $za->status . "\n";
echo "statusSys: " . $za->statusSys . "\n";
echo "filename: " . $za->filename . "\n";
echo "comment: " . $za->comment . "\n";
for ($i=0; $i<$za->numFiles;$i++) {
echo "index: $i\n";
print_r($za->statIndex($i));
}
echo "numFile:" . $za->numFiles . "\n";
?>

3、Zip 流封装,读取 OpenOffice 文件的元信息

<?php
$reader = new XMLReader();
$reader->open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml');
$odt_meta = array();
while ($reader->read()) {
if ($reader->nodeType == XMLREADER::ELEMENT) {
$elm = $reader->name;
} else {
if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') {
break;
}
if (!trim($reader->value)) {
continue;
}
$odt_meta[$elm] = $reader->value;
}
}
print_r($odt_meta);
?>

此例使用了旧的 API(PHP 4),它打开了 ZIP 文件归档,读取归档里的每个文件,并输出文件内容。此例用到的 test2.zip 文档是 ZZIPlib 源分布里测试文档中的一个。

4、Zip 使用范例

<?php
$zip = zip_open("/tmp/test2.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "File Contents:\n";
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$buf\n";
zip_entry_close($zip_entry);
}
echo "\n";
}
zip_close($zip);
}
?>

四、ZipArchive类

ZipArchive类用 Zip 压缩的文件归档。

1、类摘要

class ZipArchive implements Countable {
/* 常量 */
public const int CREATE;
public const int EXCL;
public const int CHECKCONS;
public const int OVERWRITE;
public const int RDONLY;
public const int FL_NOCASE;
public const int FL_NODIR;
public const int FL_COMPRESSED;
public const int FL_UNCHANGED;
public const int FL_RECOMPRESS;
public const int FL_ENCRYPTED;
public const int FL_OVERWRITE;
public const int FL_LOCAL;
public const int FL_CENTRAL;
public const int FL_ENC_GUESS;
public const int FL_ENC_RAW;
public const int FL_ENC_STRICT;
public const int FL_ENC_UTF_8;
public const int FL_ENC_CP437;
public const int FL_OPEN_FILE_NOW;
public const int CM_DEFAULT;
public const int CM_STORE;
public const int CM_SHRINK;
public const int CM_REDUCE_1;
public const int CM_REDUCE_2;
public const int CM_REDUCE_3;
public const int CM_REDUCE_4;
public const int CM_IMPLODE;
public const int CM_DEFLATE;
public const int CM_DEFLATE64;
public const int CM_PKWARE_IMPLODE;
public const int CM_BZIP2;
public const int CM_LZMA;
public const int CM_LZMA2;
public const int CM_ZSTD;
public const int CM_XZ;
public const int CM_TERSE;
public const int CM_LZ77;
public const int CM_WAVPACK;
public const int CM_PPMD;
public const int ER_OK;
public const int ER_MULTIDISK;
public const int ER_RENAME;
public const int ER_CLOSE;
public const int ER_SEEK;
public const int ER_READ;
public const int ER_WRITE;
public const int ER_CRC;
public const int ER_ZIPCLOSED;
public const int ER_NOENT;
public const int ER_EXISTS;
public const int ER_OPEN;
public const int ER_TMPOPEN;
public const int ER_ZLIB;
public const int ER_MEMORY;
public const int ER_CHANGED;
public const int ER_COMPNOTSUPP;
public const int ER_EOF;
public const int ER_INVAL;
public const int ER_NOZIP;
public const int ER_INTERNAL;
public const int ER_INCONS;
public const int ER_REMOVE;
public const int ER_DELETED;
public const int ER_ENCRNOTSUPP;
public const int ER_RDONLY;
public const int ER_NOPASSWD;
public const int ER_WRONGPASSWD;
public const int ER_OPNOTSUPP;
public const int ER_INUSE;
public const int ER_TELL;
public const int ER_COMPRESSED_DATA;
public const int ER_CANCELLED;
public const int ER_DATA_LENGTH;
public const int ER_NOT_ALLOWED;
public const int AFL_RDONLY;
public const int AFL_IS_TORRENTZIP;
public const int AFL_WANT_TORRENTZIP;
public const int AFL_CREATE_OR_KEEP_FILE_FOR_EMPTY_ARCHIVE;
public const int OPSYS_DOS;
public const int OPSYS_AMIGA;
public const int OPSYS_OPENVMS;
public const int OPSYS_UNIX;
public const int OPSYS_VM_CMS;
public const int OPSYS_ATARI_ST;
public const int OPSYS_OS_2;
public const int OPSYS_MACINTOSH;
public const int OPSYS_Z_SYSTEM;
public const int OPSYS_CPM;
public const int OPSYS_WINDOWS_NTFS;
public const int OPSYS_MVS;
public const int OPSYS_VSE;
public const int OPSYS_ACORN_RISC;
public const int OPSYS_VFAT;
public const int OPSYS_ALTERNATE_MVS;
public const int OPSYS_BEOS;
public const int OPSYS_TANDEM;
public const int OPSYS_OS_400;
public const int OPSYS_OS_X;
public const int OPSYS_DEFAULT;
public const int EM_NONE;
public const int EM_TRAD_PKWARE;
public const int EM_AES_128;
public const int EM_AES_192;
public const int EM_AES_256;
public const int EM_UNKNOWN;
public const string LIBZIP_VERSION;
public const int LENGTH_TO_END;
public const int LENGTH_UNCHECKED;
/* 属性 */
public readonly int $lastId;
public readonly int $status;
public readonly int $statusSys;
public readonly int $numFiles;
public readonly string $filename;
public readonly string $comment;
/* 方法 */
public addEmptyDir(string $dirname, int $flags = 0): bool
public addFile(
string $filepath,
string $entryname = "",
int $start = 0,
int $length = ZipArchive::LENGTH_TO_END,
int $flags = ZipArchive::FL_OVERWRITE
): bool
public addFromString(string $name, string $content, int $flags = ZipArchive::FL_OVERWRITE): bool
public addGlob(string $pattern, int $flags = 0, array $options = []): array|false
public addPattern(string $pattern, string $path = ".", array $options = []): array|false
public clearError(): void
public close(): bool
public count(): int
public deleteIndex(int $index): bool
public deleteName(string $name): bool
public extractTo(string $pathto, array|string|null $files = null): bool
public getArchiveComment(int $flags = 0): string|false
public getArchiveFlag(int $flag = 0, int $flags = 0): int
public getCommentIndex(int $index, int $flags = 0): string|false
public getCommentName(string $name, int $flags = 0): string|false
public getExternalAttributesIndex(
int $index,
int &$opsys,
int &$attr,
int $flags = 0
): bool
public getExternalAttributesName(
string $name,
int &$opsys,
int &$attr,
int $flags = 0
): bool
public getFromIndex(int $index, int $len = 0, int $flags = 0): string|false
public getFromName(string $name, int $len = 0, int $flags = 0): string|false
public getNameIndex(int $index, int $flags = 0): string|false
public getStatusString(): string
public getStream(string $name): resource|false
public getStreamIndex(int $index, int $flags = 0): resource|false
public getStreamName(string $name, int $flags = 0): resource|false
public static isCompressionMethodSupported(int $method, bool $enc = true): bool
public static isEncryptionMethodSupported(int $method, bool $enc = true): bool
public locateName(string $name, int $flags = 0): int|false
public open(string $filename, int $flags = 0): bool|int
public registerCancelCallback(callable $callback): bool
public registerProgressCallback(float $rate, callable $callback): bool
public renameIndex(int $index, string $new_name): bool
public renameName(string $name, string $new_name): bool
public replaceFile(
string $filepath,
int $index,
int $start = 0,
int $length = ZipArchive::LENGTH_TO_END,
int $flags = 0
): bool
public setArchiveComment(string $comment): bool
public setArchiveFlag(int $flag, int $value): bool
public setCommentIndex(int $index, string $comment): bool
public setCommentName(string $name, string $comment): bool
public setCompressionIndex(int $index, int $method, int $compflags = 0): bool
public setCompressionName(string $name, int $method, int $compflags = 0): bool
public setEncryptionIndex(int $index, int $method, ?string $password = null): bool
public setEncryptionName(string $name, int $method, ?string $password = null): bool
public setExternalAttributesIndex(
int $index,
int $opsys,
int $attr,
int $flags = 0
): bool
public setExternalAttributesName(
string $name,
int $opsys,
int $attr,
int $flags = 0
): bool
public setMtimeIndex(int $index, int $timestamp, int $flags = 0): bool
public setMtimeName(string $name, int $timestamp, int $flags = 0): bool
public setPassword(string $password): bool
public statIndex(int $index, int $flags = 0): array|false
public statName(string $name, int $flags = 0): array|false
public unchangeAll(): bool
public unchangeArchive(): bool
public unchangeIndex(int $index): bool
public unchangeName(string $name): bool
}

2、属性

  • lastId:最后新增的条目(文件或目录)索引值。 从 PHP 8.0.0 和 PECL zip 1.18.0 开始可用。
  • status:Zip 归档的状态。从 PHP 8.0.0 和 PECL zip 1.18.0 开始,可用于关闭归档。
  • statusSys:Zip 归档的系统状态。从 PHP 8.0.0 和 PECL zip 1.18.0 开始,可用于关闭归档。
  • numFiles:归档里的文件数。
  • filename:在文件系统里的文件名。
  • comment:归档里的注释。

3、ZipArchive函数

  • ZipArchive::addEmptyDir — 添加新目录。
  • ZipArchive::addFile — 将文件从给定路径添加到ZIP存档。
  • ZipArchive::addFromString — 使用文件内容将文件添加到ZIP存档中。
  • ZipArchive::addGlob — 按 glob 模式从目录添加文件。
  • ZipArchive::addPattern — 按 PCRE 模式从目录添加文件。
  • ZipArchive::clearError — 清除状态错误消息、系统和/或 zip 消息。
  • ZipArchive::close — 关闭活动存档(打开或新创建的)。
  • ZipArchive::count — 计算存档中的文件数。
  • ZipArchive::d eleteIndex — 使用索引删除存档中的条目。
  • ZipArchive::d eleteName — 使用其名称删除存档中的条目。
  • ZipArchive::extractTo — 解压缩文件。
  • ZipArchive::getArchiveComment — 返回 Zip 存档注释。
  • ZipArchive::getArchiveFlag — 返回 Zip 归档全局 flag 的值。
  • ZipArchive::getCommentIndex — 使用条目索引返回条目的注释。
  • ZipArchive::getCommentName — 使用条目名称返回条目的注释。
  • ZipArchive::getExternalAttributesIndex — 检索由其索引定义的条目的外部属性。
  • ZipArchive::getExternalAttributesName — 检索由其名称定义的条目的外部属性。
  • ZipArchive::getFromIndex — 使用其索引返回条目内容。
  • ZipArchive::getFromName — 使用其名称返回条目内容。
  • ZipArchive::getNameIndex — 使用索引返回条目的名称。
  • ZipArchive::getStatusString — 返回状态错误消息、系统和/或 zip 消息。
  • ZipArchive::getStream — 获取由其名称定义的条目的文件处理程序(只读)。
  • ZipArchive::getStreamIndex — 获取由其索引定义的条目的文件处理程序(只读)。
  • ZipArchive::getStreamName — 获取由其名称定义的条目的文件处理程序(只读)。
  • ZipArchive::isCompressionMethodSupported — 检查 libzip 是否支持压缩方法。
  • ZipArchive::isEncryptionMethodSupported — 检查 libzip 是否支持加密方法。
  • ZipArchive::locateName — 返回存档中条目的索引。
  • ZipArchive::open — 打开ZIP文件存档。
  • ZipArchive::registerCancelCallback — 注册回调以允许在存档关闭期间取消。
  • ZipArchive::registerProgressCallback — 注册一个回调以在存档关闭期间提供更新。
  • ZipArchive::renameIndex — 重命名由其索引定义的条目。
  • ZipArchive::renameName — 重命名由其名称定义的条目。
  • ZipArchive::replaceFile — 将ZIP存档中的文件替换为给定路径。
  • ZipArchive::setArchiveComment — 设置ZIP存档的注释。
  • ZipArchive::setArchiveFlag — 设置 ZIP 归档的全局 flag。
  • ZipArchive::setCommentIndex — 设置由其索引定义的条目的注释。
  • ZipArchive::setCommentName — 设置由其名称定义的条目的注释。
  • ZipArchive::setCompressionIndex — 设置由其索引定义的条目的压缩方法。
  • ZipArchive::setCompressionName — 设置由其名称定义的条目的压缩方法。
  • ZipArchive::setEncryptionIndex — 设置由其索引定义的条目的加密方法。
  • ZipArchive::setEncryptionName — 设置由其名称定义的条目的加密方法。
  • ZipArchive::setExternalAttributesIndex — 设置由其索引定义的条目的外部属性。
  • ZipArchive::setExternalAttributesName — 设置由其名称定义的条目的外部属性。
  • ZipArchive::setMtimeIndex — 设置由其索引定义的条目的修改时间。
  • ZipArchive::setMtimeName — 设置由其名称定义的条目的修改时间。
  • ZipArchive::setPassword — 设置活动存档的密码。
  • ZipArchive::statIndex — 获取由其索引定义的条目的详细信息。
  • ZipArchive::statName — 获取由其名称定义的条目的详细信息。
  • ZipArchive::unchangeAll — 撤消存档中所做的所有更改。
  • ZipArchive::unchangeArchive — 还原存档中完成的所有全局更改。
  • ZipArchive::unchangeIndex — 还原对给定索引处的条目所做的所有更改。
  • ZipArchive::unchangeName — 还原对具有给定名称的条目所做的所有更改。

五、Zip函数

1、zip_close — 关闭一个ZIP档案文件。

2、zip_entry_close — 关闭目录项。

3、zip_entry_compressedsize — 检索目录项压缩过后的大小。

4、zip_entry_compressionmethod — 检索目录实体的压缩方法。

5、zip_entry_filesize — 检索目录实体的实际大小。

6、zip_entry_name — 检索目录项的名称。

7、zip_entry_open — 打开用于读取的目录实体。

8、zip_entry_read — 读取一个打开了的压缩目录实体。

9、zip_open — 打开 ZIP 文件归档。

10、zip_read — 读取 ZIP 文件归档中下一项。

  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。
PHP Zip压缩
上一篇: PHP Rar文件
PHP Zip压缩
下一篇: PHP Zlib压缩