Method of Realizing File Compression and Decompression by PHP

  • 2021-12-11 17:36:21
  • OfStack

In php, sometimes we need to use compressed file operation, compressed file can save disk space; And the compressed file is smaller, which is convenient for network transmission and high efficiency. Let's learn about the compression and decompression related operations of php

There is one ZipArchive class in PHP, which is specially used for file compression and decompression related operations

The following methods are mainly used in ZipArchive class:

1: open (open 1 compressed package file)


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

Parameter 1: The compressed package file to open

The second parameter:

ZIPARCHIVE:: OVERWRITE Always creates a new file. If the specified zip file exists, it will be overwritten

ZIPARCHIVE:: CREATE If the specified zip file does not exist, a new one is created

ZIPARCHIVE:: EXCL an error is reported if the specified zip file exists

ZIPARCHIVE:: CHECKCONS Performs additional 1 test on the specified zip

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


// Will test.txt File to the compressed package 
$zip->addFile('test.txt'); // No. 1 2 Parameters to rename a file 

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


// Will 1 Empty directories are added to the zip Medium 
 $zip->addEmptyDir ('newdir');

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


//  Object with the specified content new.txt File is added to the zip In a file 
$zip->addFromString('new.txt', ' To add to the new.txt Text in a file ');

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


 $zip->extractTo('test');

6: getNameIndex (Returns file name by index)


$zip->getNameIndex(0);// Returns the index in the compressed package as 0 File name of 

7: getStream (get the text stream of the file according to the file name in the compression)


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

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


/ Put the number in the compressed file 1 Files are modified to newname.txt 
$zip->renameIndex(0,'newname.txt');

9: renameName (Modify the file name in the compressed file according to the file name in the compressed file)


// Inside the compressed file word.txt Modify to newword.txt 
$zip->renameName('word.txt','newword.txt');

10: deleteIndex (Delete files within the zip file based on the index within the zip file)


/ Put the number in the compressed file 1 File deletion  
$zip->deleteIndex (0);

11: deleteName (Delete file based on file name within zip file)


// Will test.txt File to the compressed package 
$zip->addFile('test.txt'); // No. 1 2 Parameters to rename a file 
0

The above are some common methods of ZipArchive, and the following are some simple examples

1: Create a compressed package


// Will test.txt File to the compressed package 
$zip->addFile('test.txt'); // No. 1 2 Parameters to rename a file 
1

2: Get the file information of the compressed package and extract the specified compressed package


// Will test.txt File to the compressed package 
$zip->addFile('test.txt'); // No. 1 2 Parameters to rename a file 
2

3: Modify the file name of the specified file in the compressed package and delete the specified file in the compressed package


$zip = new \ZipArchive;
if ($zip->open('test_new.zip') === true) {
  // Index the compressed file to 0 The file of is modified to newname.txt
  $zip->renameIndex(0,'newname.txt');
  // Inside the compressed file new.txt Modify to newword.txt
  $zip->renameName('new.txt','newword.txt');
  // Delete the index in the compressed file as 0 File of 
  $zip->deleteIndex(0);
  // Delete the of the compressed file test.png
  $zip->deleteName('test.png');
  //  Shut down zip Documents 
  $zip->close();
}


Related articles: