Examples of Common Methods for PHP to Generate zip Compressed Packages

  • 2021-12-21 04:15:13
  • OfStack

This paper describes the common methods of generating zip compressed packets by PHP. Share it for your reference, as follows:

Compress 1 file

We generate 1 file into 1 compressed package.


<?php
$path = "c:/wamp/www/log.txt";
$filename = "test.zip";
$zip = new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);  // Open the compressed package 
$zip->addFile($path,basename($path));  // Add a file to the zip package 
$zip->close(); // Close the compressed package 

The above code compresses the c:/wamp/www/log. txt file into test. zip and saves it in the current directory.

Compress multiple files

Compressing multiple files is actually addFile executed multiple times, which can be realized through the traversal of arrays.


<?php
$fileList = array(
  "c:/wamp/www/log.txt",
  "c:/wamp/www/weixin.class.php"
);
$filename = "test.zip";
$zip = new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);  // Open the compressed package 
foreach($fileList as $file){
  $zip->addFile($file,basename($file));  // Add a file to the zip package 
}
$zip->close(); // Close the compressed package 

Compress 1 directory


<?php
function addFileToZip($path,$zip){
  $handler=opendir($path); // Open the current folder by the $path Specify. 
  while(($filename=readdir($handler))!==false){
    if($filename != "." && $filename != ".."){// The folder file name is '.' And ' ..' Do not operate on them 
      if(is_dir($path."/".$filename)){//  If one of the objects read is a folder, recursive 
        addFileToZip($path."/".$filename, $zip);
      }else{ // Add a file zip Object 
        $zip->addFile($path."/".$filename);
      }
    }
  }
  @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('rsa.zip', ZipArchive::OVERWRITE)=== TRUE){
  addFileToZip('rsa/', $zip); // Call the method, operate on the root directory to be packaged, and set the ZipArchive Pass the object of to the method 
  $zip->close(); // Close the processed zip Documents 
}

Compress and download the zip package

My time, we need to pack it, provide it for download, and then delete the compressed package.

It can be divided into the following steps:

Determine whether the given path is a folder or a file. Folders also need to traverse to add files. Set the relevant file headers and use the readfile function to provide downloads. Use the unlink function to delete the compressed package

<?php
function addFileToZip($path,$zip){
  $handler=opendir($path); // Open the current folder by the $path Specify. 
  while(($filename=readdir($handler))!==false){
    if($filename != "." && $filename != ".."){// The folder file name is '.' And ' ..' Do not operate on them 
      if(is_dir($path."/".$filename)){//  If one of the objects read is a folder, recursive 
        addFileToZip($path."/".$filename, $zip);
      }else{ // Add a file zip Object 
        $zip->addFile($path."/".$filename);
      }
    }
  }
  @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('rsa.zip', ZipArchive::OVERWRITE)=== TRUE){
  $path = 'rsa/';
  if(is_dir($path)){ // Give folders, pack folders 
    addFileToZip($path, $zip);
  }else if(is_array($path)){ // Give the file path as an array 
    foreach($path as $file){
      $zip->addFile($file);
    }
  }else{   // Give only 1 Files 
    $zip->addFile($path);
  }
  $zip->close(); // Close the processed zip Documents 
}

For more readers interested in PHP related content, please check the topics on this site: "PHP Operation zip File and Compression Skills Summary", "php File Operation Summary", "PHP Basic Grammar Introduction Course", "php Object-Oriented Programming Introduction Course", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Course" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: