Home > Backend Development > PHP Tutorial > PHP uses ZipArchive class to implement file compression and decompression

PHP uses ZipArchive class to implement file compression and decompression

王林
Release: 2023-04-08 09:04:02
forward
3358 people have browsed it

PHP uses ZipArchive class to implement file compression and decompression

The ZipArchive class is a class specially used for file compression and decompression operations. By compressing files, disk space can be saved, and the compressed file size is smaller, which facilitates network transmission.

In the ZipArchive class we mainly use the following methods:

1: open (open a compressed package file)

$zip = new \ZipArchive;
$zip->open('test_new.zip', \ZipArchive::CREATE)
Copy after login

Parameter description :

The first parameter: the compressed package file to be opened

The second parameter:

ZIPARCHIVE::OVERWRITEAlways Create a new file. If the specified zip file exists, it will be overwritten.

ZIPARCHIVE::CREATEIf the specified zip file does not exist, create a new one.

ZIPARCHIVE::EXCLIf the specified zip file exists, an error will be reported.

ZIPARCHIVE::CHECKCONSPerform additional consistency tests on the specified zip.

(Free learning video tutorial sharing: php video tutorial)

2: addFile (add the specified file to the compressed package)

//将test.txt文件添加到压缩包中
$zip->addFile('test.txt'); //第二个参数可对文件进行重命名
Copy after login

3: addEmptyDir (add the specified empty directory to the compressed package)

//将一个空的目录添加到zip中
 $zip->addEmptyDir ('newdir');
Copy after login

4: addFromString (add the file with the specified content to the compressed package)

// 将有指定内容的new.txt文件添加到zip文件中
$zip->addFromString('new.txt', '要添加到new.txt文件中的文本');
Copy after login

5: extractTO (Extract the compressed package to the specified directory)

$zip->extractTo('test');
Copy after login

6: getNameIndex (return the file name according to the index)

$zip->getNameIndex(0);//返回压缩包中索引为0的文件名称
Copy after login

7: getStream (according to the file name in the compressed file, obtain the text stream of the file)

$zip->getStream('hello.txt');
Copy after login

8: renameIndex (according to the file name in the compressed file) Index (starting from 0) Modify the file name in the compressed file)

/把压缩文件内第一个文件修改成newname.txt
$zip->renameIndex(0,'newname.txt');
Copy after login

9: renameName (Modify the file name in the compressed file based on the file name in the compressed file)

//把压缩文件内的word.txt修改成newword.txt
$zip->renameName('word.txt','newword.txt');
Copy after login

10: deleteIndex (Delete the file in the compressed file based on the index in the compressed file)

//把压缩文件内第一个文件删除
$zip->deleteIndex (0);
Copy after login

11: deleteName (Delete the file in the compressed file based on the index Delete files by name)

//把压缩文件内的word.txt删除
$zip->deleteName('word.txt');
Copy after login

The above are some common methods of the ZipArchive class. Here are some simple examples:

1: Create a compressed package

$zip = new \ZipArchive;
if ($zip->open('test_new.zip', \ZipArchive::CREATE) === true)
{
  // 将指定文件添加到zip中
  $zip->addFile('test.txt');
   
  // test.txt文件添加到zip并将其重命名为newfile.txt
  $zip->addFile('test.txt', 'newfile.txt');
   
  // 将test.txt文件添加到zip文件中的test文件夹内
  $zip->addFile('test.txt', 'test/newfile.txt');
   
  //将一个空的目录添加到zip中
  $zip->addEmptyDir ('test');
   
  // 将有指定内容的new.txt文件添加到zip文件中
  $zip->addFromString('new.txt', '要添加到new.txt文件中的文本');
   
  // 将有指定内容的new.txt添加到zip文件中的test文件夹
  $zip->addFromString('test/new.txt', '要添加到new.txt文件中的文本');
   
  //将images目录下所有文件添加到zip中
   if ($handle = opendir('images')){
     // 添加目录中的所有文件
     while (false !== ($entry = readdir($handle))){
        if ($entry != "." && $entry != ".." && !is_dir('images/' . $entry)){
            $zip->addFile('images/' . $entry);
        }
     }
     closedir($handle);
   }
   
  // 关闭zip文件
  $zip->close();
}
Copy after login

Two: Obtain the file information of the compressed package and decompress the specified compressed package

$zip = new \ZipArchive;
if ($zip->open('test_new.zip') === true) {
  //获取索引为0的文件名称
  var_dump($zip->getNameIndex(0));
   
  //将压缩包文件解压到test目录下
  $zip->extractTo('test');
   
  //获取压缩包指定文件的文本流
  $stream = $zip->getStream('test.txt');
   
  // 关闭zip文件
  $zip->close();
  $str = stream_get_contents($stream); //这里注意获取到的文本编码
  var_dump($str);
}
Copy after login

Three: Modify the file name of the specified file in the compressed package and delete it from the compressed package Specify file

$zip = new \ZipArchive;
if ($zip->open('test_new.zip') === true) {
  //把压缩文件内索引为0的文件修改成newname.txt
  $zip->renameIndex(0,'newname.txt');
  //把压缩文件内的new.txt修改成newword.txt
  $zip->renameName('new.txt','newword.txt');
  //删除压缩文件内索引为0的文件
  $zip->deleteIndex(0);
  //删除压缩文件的test.png
  $zip->deleteName('test.png');
  // 关闭zip文件
  $zip->close();
}
Copy after login

Recommended related articles and tutorials:php tutorial

The above is the detailed content of PHP uses ZipArchive class to implement file compression and decompression. For more information, please follow other related articles on the PHP Chinese website!

source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template