php zip

其他类别 2025-08-24

PhpZip是用于使用邮政编码的扩展工作的PHP-Library。

俄罗斯文档

版本和依赖项

版本php文档
^4.0(主) ^7.4 |^8.0当前的
^3.0 ^5.5 |^7.0文档v3.3

目录

  • 特征
  • 要求
  • 安装
  • 例子
  • 词汇表
  • 文档
    • PhpZipZipFile的方法概述
    • zip-Archive的创建/开放
    • 从档案中读取条目
    • 迭代条目
    • 获取有关条目的信息
    • 在档案中添加条目
    • 从档案中删除条目
    • 使用条目和档案
    • 使用密码
    • 撤消变化
    • 将文件或输出保存到浏览器
    • 关闭档案
  • 运行测试
  • ChangElog
  • 升级
    • 升级版本3到版本4
    • 将版本2升级到版本3

特征

  • 打开和解压缩zip文件。
  • 创建邮政编码。
  • 修改拉链档案。
  • 纯PHP(不需要扩展php-zip和类ZipArchive )。
  • 它支持将存档保存到文件,将存档输出到浏览器中,或将其作为字符串输出而不将其保存到文件中。
  • 支持档案评论和个人条目的评论。
  • 获取有关存档中每个条目的信息。
  • 仅支持以下压缩方法:
    • 没有压缩(存储)。
    • 放气压缩。
    • BZIP2压缩具有延伸php-bz2
  • ZIP64的支持(文件大小超过4 GB或存档中的条目数量超过65535)。
  • 使用密码

    注意力!

    对于32位系统,当前不支持Traditional PKWARE Encryption (ZipCrypto)加密方法。尽可能使用加密方法WinZIP AES Encryption

    • 设置密码以读取所有条目或仅针对某些条目的存档。
    • 更改存档的密码,包括各个条目。
    • 删除所有或单个条目的存档密码。
    • 为所有人设置密码和/或加密方法,以及存档中的各个条目。
    • 为不同条目设置不同的密码和加密方法。
    • 删除所有或某些条目的密码。
    • 支持Traditional PKWARE Encryption (ZipCrypto)WinZIP AES Encryption加密方法。
    • 为存档中的所有或单个条目设置加密方法。

要求

  • PHP > = 7.4或PHP > = 8.0(最好是64位)。
  • 用于BZIP2压缩的可选php-Extension bzip2
  • WinZip Aes Encryption支持的可选php-Extension openssl

安装

composer require nelexa/zip

最新稳定版本:

例子

 // create new archive
$ zipFile = new  PhpZip  ZipFile ();
try {
    $ zipFile
        -> addFromString ( ' zip/entry/filename ' , ' Is file content ' ) // add an entry from the string
        -> addFile ( ' /path/to/file ' , ' data/tofile ' ) // add an entry from the file
        -> addDir ( __DIR__ , ' to/path/ ' ) // add files from the directory
        -> saveAsFile ( $ outputFilename ) // save the archive to a file
        -> close (); // close archive
            
    // open archive, extract, add files, set password and output to browser.
    $ zipFile
        -> openFile ( $ outputFilename ) // open archive from file
        -> extractTo ( $ outputDirExtract ) // extract files to the specified directory
        -> deleteFromRegex ( ' ~^.~ ' ) // delete all hidden (Unix) files
        -> addFromString ( ' dir/file.txt ' , ' Test file ' ) // add a new entry from the string
        -> setPassword ( ' password ' ) // set password for all entries
        -> outputAsAttachment ( ' library.jar ' ); // output to the browser without saving to a file
}
catch (  PhpZip  Exception  ZipException $ e ){
    // handle exception
}
finally {
    $ zipFile -> close ();
}

其他示例可以在tests/文件夹中找到

词汇表

zip条目- zip -Archive中的文件或文件夹。存档中的每个条目都有某些属性,例如:文件名,压缩方法,加密方法,压缩前的文件大小,压缩后的文件大小,CRC32等。

文档:

PhpZipZipFile的方法概述

  • Zipfile :: __构造 - 初始化Zip存档。
  • zipfile :: addall-添加数组中的所有条目。
  • zipfile :: adddir-将文件从指定路径上的目录中添加到没有子目录的指定路径上。
  • Zipfile :: AddDirrecursive-通过子目录在指定路径上的目录中将文件添加到档案中。
  • Zipfile :: AddEmptydir-添加一个新目录。
  • Zipfile :: AddFile-将文件从给定路径添加到zip档案中。
  • zipfile :: addsplfile-将SplFileInfo添加到zip档案中。
  • Zipfile :: AddFromFinder-将SymfonyComponentFinderFinder中的文件添加到zip档案中。
  • Zipfile :: AddFilesFromIterator-添加目录迭代器中的文件。
  • Zipfile :: AddFilesFromGlob-添加来自Glog模式目录的文件,而无需子目录。
  • zipfile :: addfilesFromGlobRecursive-添加来自Glog模式的目录中的文件,并带有子目录。
  • Zipfile :: AddFilesFromRegex-添加来自PCRE模式目录中的文件,而无需子目录。
  • Zipfile :: AddFilesFromRegeXRecursive-添加来自子目录的PCRE模式目录中的文件。
  • Zipfile :: AddFromStream-将流从流到ZIP存档的条目。
  • Zipfile :: AddFromString-使用其内容将文件添加到zip档案中。
  • Zipfile ::关闭 - 关闭档案。
  • Zipfile :: Count-返回存档中的条目数。
  • Zipfile :: DeleteFromName-使用其名称删除存档中的条目。
  • Zipfile :: DeleteFromGlob-使用Glob模式删除存档中的条目。
  • Zipfile :: DeleteFromregex-使用PCRE模式删除存档中的条目。
  • Zipfile :: DeleteAll-删除Zip存档中的所有条目。
  • Zipfile ::禁用 - 禁用加密,用于已存档中的所有条目。
  • zipfile :: disauseCryptignEntry-禁用由其名称定义的条目的加密。
  • Zipfile :: Deftracto-提取存档内容。
  • Zipfile :: getarchivecomment-返回Zip Archive评论。
  • zipfile :: getEntrycomment-使用条目名称返回条目的评论。
  • zipfile :: getentrycontent-使用其名称返回条目内容。
  • zipfile :: getListFiles-返回存档文件列表。
  • Zipfile :: Hasentry-检查存档中是否有条目。
  • zipfile :: iSdirectory-检查存档中的条目是否是目录。
  • Zipfile :: Matcher-选择存档中的条目以对其进行操作。
  • Zipfile :: OpenFile-从文件打开一个zip -Archive。
  • Zipfile :: OpenFromString-从字符串打开Zip -Archive。
  • Zipfile :: OpenFromStream-从流中打开一个Zip -Archive。
  • zipfile :: outputasattachment-向浏览器输出zip -Archive。
  • Zipfile :: outputaspsr7Response-输出zip-Archive作为PSR-7响应。
  • zipfile :: outputassymfonyresponse-输出zip -archive作为符号响应。
  • zipfile :: outputAsstring-输出zip -archive作为字符串。
  • zipfile ::重命名 - 重命名为由其名称定义的条目。
  • Zipfile ::重写 - 保存更改并重新打开更改的存档。
  • zipfile :: saveasfile-将存档保存到文件中。
  • zipfile :: saveasstream-将存档写入流。
  • Zipfile :: SetArchiveComment-设置Zip存档的评论。
  • zipfile :: setCompressionlevel-设置存档中所有文件的压缩级别。
  • zipfile :: setCompressionlevelentry-设置条目以其名称的压缩级别。
  • zipfile :: setCompressionMethodentry-设置条目以其名称的压缩方法。
  • Zipfile :: SetEntrycomment-设置由其名称定义的条目的评论。
  • Zipfile :: SetReadPassword-设置打开档案的密码。
  • Zipfile :: SetReadPasswordEntry-设置一个密码,用于读取由其名称定义的条目。
  • Zipfile :: SetPassword-为存档中的所有文件设置一个新密码。
  • Zipfile :: SetPasswordEntry-设置由其名称定义的条目的新密码。
  • zipfile :: unchangeall-撤消档案中所有更改。
  • zipfile :: unchangearchearchivecomment-撤消对存档评论的更改。
  • zipfile :: unchangentry-撤消由其名称定义的条目的变更。

zip-Archive的创建/开放

zipfile :: __构造**

初始化拉链档案

 $ zipFile = new  PhpZip  ZipFile ();
Zipfile :: OpenFile

从文件打开zip-Archive。

 $ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> openFile ( ' file.zip ' );
Zipfile :: OpenFromstring

从字符串打开邮政编码。

 $ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> openFromString ( $ stringContents );
Zipfile :: OpenFromStream

从流从流打开邮政编码。

 $ stream = fopen ( ' file.zip ' , ' rb ' );

$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> openFromStream ( $ stream );

从档案中读取条目

Zipfile :: Count

返回存档中的条目数。

 $ zipFile = new  PhpZip  ZipFile ();

$ count = count ( $ zipFile );
// or
$ count = $ zipFile -> count ();
zipfile :: getListfiles

返回存档文件列表。

 $ zipFile = new  PhpZip  ZipFile ();
$ listFiles = $ zipFile -> getListFiles ();

// example array contents:
// array (
//   0 => 'info.txt',
//   1 => 'path/to/file.jpg',
//   2 => 'another path/',
//   3 => '0',
// ) 
zipfile :: getentrycontent

使用其名称返回条目内容。

 // $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new  PhpZip  ZipFile ();

$ contents = $ zipFile [ $ entryName ];
// or
$ contents = $ zipFile -> getEntryContents ( $ entryName );
Zipfile :: Hasentry

检查存档中是否有条目。

 // $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new  PhpZip  ZipFile ();

$ hasEntry = isset ( $ zipFile [ $ entryName ]);
// or
$ hasEntry = $ zipFile -> hasEntry ( $ entryName );
zipfile :: iSdirectory

检查存档中的条目是否是目录。

 // $entryName = 'path/to/';
$ zipFile = new  PhpZip  ZipFile ();

$ isDirectory = $ zipFile -> isDirectory ( $ entryName );
zipfile ::提取物

提取档案内容。目录必须存在。

 $ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> extractTo ( $ directory );

将一些文件提取到目录。目录必须存在。

 // $toDirectory = '/tmp';
$ extractOnlyFiles = [
    ' filename1 ' , 
    ' filename2 ' , 
    ' dir/dir/dir/ '
];
$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> extractTo ( $ toDirectory , $ extractOnlyFiles );

迭代条目

ZipFile是迭代器。可以迭代foreach循环中的所有条目。

 foreach ( $ zipFile as $ entryName => $ contents ){
    echo " Filename: $ entryName " . PHP_EOL ;
    echo " Contents: $ contents " . PHP_EOL ;
    echo ' ----------------------------- ' . PHP_EOL ;
}

可以通过Iterator迭代。

next(); }">
 $ iterator = new  ArrayIterator ( $ zipFile );
while ( $ iterator -> valid ())
{
    $ entryName = $ iterator -> key ();
    $ contents = $ iterator -> current ();

    echo " Filename: $ entryName " . PHP_EOL ;
    echo " Contents: $ contents " . PHP_EOL ;
    echo ' ----------------------------- ' . PHP_EOL ;

    $ iterator -> next ();
}

获取有关条目的信息

zipfile :: getarchivecomment

返回邮政编码评论。

 $ zipFile = new  PhpZip  ZipFile ();
$ commentArchive = $ zipFile -> getArchiveComment ();
zipfile :: getentrycomment

使用条目名称返回条目的注释。

 $ zipFile = new  PhpZip  ZipFile ();
$ commentEntry = $ zipFile -> getEntryComment ( $ entryName );

在档案中添加条目

将条目添加到邮政编码档案中的所有方法允许您指定一种用于压缩内容的方法。

可以使用以下压缩方法:

  • PhpZipConstantsZipCompressionMethod::STORED - 无压缩
  • PhpZipConstantsZipCompressionMethod::DEFLATED -deflate compression
  • PhpZipConstantsZipCompressionMethod::BZIP2 -bzip2压缩使用扩展ext-bz2
zipfile :: addfile

将文件从给定路径添加到邮政编码。

 $ zipFile = new  PhpZip  ZipFile ();
// $file = '...../file.ext'; 
// $entryName = 'file2.ext'
$ zipFile -> addFile ( $ file );

// you can specify the name of the entry in the archive (if null, then the last component from the file name is used)
$ zipFile -> addFile ( $ file , $ entryName );

// you can specify a compression method
$ zipFile -> addFile ( $ file , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFile ( $ file , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFile ( $ file , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
zipfile :: addsplfile

SplFileInfo添加到拉链档案中。

 // $file = '...../file.ext'; 
// $entryName = 'file2.ext'
$ zipFile = new  PhpZip  ZipFile ();

$ splFile = new  SplFileInfo ( ' README.md ' );

$ zipFile -> addSplFile ( $ splFile );
$ zipFile -> addSplFile ( $ splFile , $ entryName );
// or
$ zipFile [ $ entryName ] = new  SplFileInfo ( $ file );

// set compression method
$ zipFile -> addSplFile ( $ splFile , $ entryName , $ options = [
     PhpZip  Constants ZipOptions:: COMPRESSION_METHOD =>  PhpZip  Constants ZipCompressionMethod:: DEFLATED ,
]);
Zipfile :: AddFromFinder

SymfonyComponentFinderFinder中的文件添加到zip存档。

 $ finder = new  Symfony  Component  Finder  Finder ();
$ finder
    -> files ()
    -> name ( ' *.{jpg,jpeg,gif,png} ' )
    -> name ( ' /^[0-9a-f]./ ' )
    -> contains ( ' /lorems+ipsum$/i ' )
    -> in ( ' path ' );

$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addFromFinder ( $ finder , $ options = [
     PhpZip  Constants ZipOptions:: COMPRESSION_METHOD =>  PhpZip  Constants ZipCompressionMethod:: DEFLATED ,
     PhpZip  Constants ZipOptions:: MODIFIED_TIME => new  DateTimeImmutable ( ' -1 day 5 min ' )
]);
zipfile :: addfromstring

使用其内容将文件添加到zip档案中。

 $ zipFile = new  PhpZip  ZipFile ();

$ zipFile [ $ entryName ] = $ contents ;
// or
$ zipFile -> addFromString ( $ entryName , $ contents );

// you can specify a compression method
$ zipFile -> addFromString ( $ entryName , $ contents ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFromString ( $ entryName , $ contents ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFromString ( $ entryName , $ contents ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
Zipfile :: AddFromStream

从流到邮编档案中添加了一个条目。

 $ zipFile = new  PhpZip  ZipFile ();
// $stream = fopen(..., 'rb');

$ zipFile -> addFromStream ( $ stream , $ entryName );
// or
$ zipFile [ $ entryName ] = $ stream ;

// you can specify a compression method
$ zipFile -> addFromStream ( $ stream , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFromStream ( $ stream , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFromStream ( $ stream , $ entryName ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
Zipfile :: AddEmptydir

添加一个新目录。

addEmptyDir($path); // or $zipFile[$path] = null;">
 $ zipFile = new  PhpZip  ZipFile ();
// $path = "path/to/";
$ zipFile -> addEmptyDir ( $ path );
// or
$ zipFile [ $ path ] = null ;
Zipfile :: Addall

添加来自数组的所有条目。

 $ entries = [
    ' file.txt ' => ' file contents ' , // add an entry from the string contents
    ' empty dir/ ' => null , // add empty directory
    ' path/to/file.jpg ' => fopen ( ' ..../filename ' , ' rb ' ), // add an entry from the stream
    ' path/to/file.dat ' => new  SplFileInfo ( ' ..../filename ' ), // add an entry from the file
];

$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addAll ( $ entries );
zipfile :: adddir

从指定路径上的目录中将文件添加到没有子目录的指定路径的目录中。

 $ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addDir ( $ dirName );

// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addDir ( $ dirName , $ localPath );

// you can specify a compression method
$ zipFile -> addDir ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addDir ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addDir ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
zipfile :: adddirrecursive

通过子目录在指定路径上的目录中将文件添加到存档中。

 $ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addDirRecursive ( $ dirName );

// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addDirRecursive ( $ dirName , $ localPath );

// you can specify a compression method
$ zipFile -> addDirRecursive ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addDirRecursive ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addDirRecursive ( $ dirName , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
Zipfile :: AddFilesFromIterator

从目录的迭代器中添加文件。

 // $directoryIterator = new DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new RecursiveDirectoryIterator($dir); // with subdirectories
$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addFilesFromIterator ( $ directoryIterator );

// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath );
// or
$ zipFile [ $ localPath ] = $ directoryIterator ;

// you can specify a compression method
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromIterator ( $ directoryIterator , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression

示例一些文件忽略:

 $ ignoreFiles = [
    ' file_ignore.txt ' , 
    ' dir_ignore/sub dir ignore/ '
];

// $directoryIterator = new DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new RecursiveDirectoryIterator($dir); // with subdirectories
// use PhpZipUtilIteratorIgnoreFilesFilterIterator for non-recursive search
 
$ zipFile = new  PhpZip  ZipFile ();
$ ignoreIterator = new  PhpZip  Util  Iterator  IgnoreFilesRecursiveFilterIterator (
    $ directoryIterator , 
    $ ignoreFiles
);

$ zipFile -> addFilesFromIterator ( $ ignoreIterator );
Zipfile :: AddFilesFromGlob

添加来自Glot模式目录的文件,而无需子目录。

 $ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files

$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern );

// you can specify the path in the archive to which you want to put entries
$ localPath = ' to/path/ ' ;
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath );

// you can specify a compression method
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: STORED ); // No compression
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: DEFLATED ); // Deflate compression
$ zipFile -> addFilesFromGlob ( $ dir , $ globPattern , $ localPath ,  PhpZip  Constants ZipCompressionMethod:: BZIP2 ); // BZIP2 compression 
zipfile :: addfilesFromGlobRecursive

通过子目录添加来自Glob模式的目录中的文件。

 $ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files

$ zipFile = new  PhpZip  ZipFile ();
$ zipFile -> addFilesFromGlobRecursive ( $ dir , $ globPattern );

// you can specify the path in the archive to which you want to p					
下载源码

通过命令行克隆项目:

git clone https://github.com/Ne-Lexa/php-zip.git