SoFunction
Updated on 2025-03-09

How to generate zip compressed package using php

Compress a file

We generate a compressed package for a file.

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

The above code compresses and generates the c:/wamp/www/ file and saves it in the current directory.

Compress multiple files

Compressing multiple files is actually an execution of addFile multiple times, which can be implemented through array traversal.

<?php
$fileList = array(
    "c:/wamp/www/",
    "c:/wamp/www/"
);
$filename = "";
$zip = new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);   //Open the compressed packageforeach($fileList as $file){
    $zip->addFile($file,basename($file));   //Add files to the zip package}
$zip->close();  //Close the compressed package

Compress a directory

<?php
function addFileToZip($path,$zip){
    $handler=opendir($path); //Open the current folder and is specified by $path.    while(($filename=readdir($handler))!==false){
        if($filename != "." && $filename != ".."){//The folder file names are '.' and '..', do not operate on them            if(is_dir($path."/".$filename)){// If an object read is a folder, recursively                addFileToZip($path."/".$filename, $zip);
            }else{ //Add file to zip object                $zip->addFile($path."/".$filename);
            }
        }
    }
    @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('', ZipArchive::OVERWRITE)=== TRUE){
    addFileToZip('rsa/', $zip); //Call the method, operate on the root directory to be packaged, and pass the ZipArchive object to the method    $zip->close(); //Close the processed zip file}

Compress and download the zip package

When I was, we needed to package it, provide the download, and then delete the compressed package.

It can be divided into the following steps:

  1. Determine whether the path given is a folder or a file. The folder also needs to traverse to add files.
  2. Set the relevant file header and use the readfile function to provide download.
  3. Use the unlink function to delete the compressed package
<?php
function addFileToZip($path,$zip){
    $handler=opendir($path); //Open the current folder and is specified by $path.    while(($filename=readdir($handler))!==false){
        if($filename != "." && $filename != ".."){//The folder file names are '.' and '..', do not operate on them            if(is_dir($path."/".$filename)){// If an object read is a folder, recursively                addFileToZip($path."/".$filename, $zip);
            }else{ //Add file to zip object                $zip->addFile($path."/".$filename);
            }
        }
    }
    @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('', ZipArchive::OVERWRITE)=== TRUE){
    $path = 'rsa/';
    if(is_dir($path)){  //Give the folder and package the folder        addFileToZip($path, $zip);
    }else if(is_array($path)){  //Give file path in array        foreach($path as $file){
            $zip->addFile($file);
        }
    }else{      //Only give one file        $zip->addFile($path);
    }

    $zip->close(); //Close the processed zip file}

The above is the detailed content of how to use php to generate zip compressed packages. For more information about php to generate zip compressed packages, please pay attention to my other related articles!