PHP implementation of file compression and decompression

Keywords: Programming PHP network encoding

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

There is a ziprarchive class in PHP, which is specially used for compression and decompression of files

The following methods are mainly used in ziprarchive class:

1: Open (open a compressed package file)

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

First parameter: compressed package file to open

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, create a new one

ZIPARCHIVE::EXCL if the specified zip file exists, an error will be reported

ZIPARCHIVE::CHECKCONS perform other consistency tests on the specified zip

2: AddFile (adds the specified file to the package)

//Add the test.txt file to the package
$zip->addFile('test.txt'); //The second parameter renames the file

3: addEmptyDir (adds the specified empty directory to the package)

//Add an empty directory to zip
 $zip->addEmptyDir ('newdir');

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

// Add the new.txt file with the specified content to the zip file
$zip->addFromString('new.txt', 'To add to new.txt Text in file');

5: Extract to (extract the package to the specified directory)

$zip->extractTo('test');

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

$zip->getNameIndex(0);//Returns the file name with index 0 in the compressed package

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

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

8: Rename index (changes the filename in the compressed file based on the index in the compressed file (starting from 0)

//Change the first file in the compressed file to newname.txt 
$zip->renameIndex(0,'newname.txt');

9: Rename name (modify the filename in the compressed file according to the filename in the compressed file)

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

10: deleteIndex (delete files in a compressed file based on the index in the compressed file)

//Delete the first file in the compressed file 
$zip->deleteIndex (0);

11: deleteName (delete a file based on the filename in the compressed file)

//Delete word.txt from the compressed file
$zip->deleteName('word.txt');

Here are some common methods of ziprarchive. Here are some simple examples

1: Create a compressed package

$zip = new \ZipArchive;
if ($zip->open('test_new.zip', \ZipArchive::CREATE) === true)
{
  // Add the specified file to zip
  $zip->addFile('test.txt');
  
  // Add the test.txt file to zip and rename it to newfile.txt
  $zip->addFile('test.txt', 'newfile.txt');
  
  // Add the test.txt file to the test folder in the zip file
  $zip->addFile('test.txt', 'test/newfile.txt');
  
  //Add an empty directory to zip
  $zip->addEmptyDir ('test');
  
  // Add the new.txt file with the specified content to the zip file
  $zip->addFromString('new.txt', 'To add to new.txt Text in file');
  
  // Add new.txt with the specified content to the test folder in the zip file
  $zip->addFromString('test/new.txt', 'To add to new.txt Text in file');
  
  //Add all files in the images directory to the zip
   if ($handle = opendir('images')){
     // Add all files in directory
     while (false !== ($entry = readdir($handle))){
        if ($entry != "." && $entry != ".." && !is_dir('images/' . $entry)){
            $zip->addFile('images/' . $entry);
        }
     }
     closedir($handle);
   }
  
  // Close zip file
  $zip->close();
}

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

$zip = new \ZipArchive;
if ($zip->open('test_new.zip') === true) {
  //Get file name with index 0
  var_dump($zip->getNameIndex(0));
  
  //Extract the compressed package file to the test directory
  $zip->extractTo('test');
  
  //Gets the text stream of the specified file in the compressed package
  $stream = $zip->getStream('test.txt');
  
  // Close zip file
  $zip->close();
  $str = stream_get_contents($stream); //Note the obtained text encoding here
  var_dump($str);
}

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

$zip = new \ZipArchive;
if ($zip->open('test_new.zip') === true) {
  //Change the file with index 0 in the compressed file to newname.txt
  $zip->renameIndex(0,'newname.txt');
  //Change the new.txt in the compressed file to newword.txt
  $zip->renameName('new.txt','newword.txt');
  //Delete file with index 0 in compressed file
  $zip->deleteIndex(0);
  //Delete test.png of compressed file
  $zip->deleteName('test.png');
  // Close zip file
  $zip->close();
}

Posted by doremi on Sat, 09 May 2020 01:58:02 -0700