如何使用PHP创建ZIP文件

zip是一种常用的文件格式,可以使用数据压缩归档文件。
当我们希望允许用户从服务器立即下载多个文件夹和文件时,需要在飞行中创建ZIP文件。
它有助于压缩文件并创建存档一次以一次下载多个文件。
可以使用PHP动态创建ZIP文件,并且可以轻松地将存档文件从PHP脚本保存在服务器上。

可以使用PHP轻松实现从目录中创建zip存档。
PHP中的ZiParchive类提供了一种即时能够压缩ZIP文件中的文件或者文件夹。
我们可以使用PHP将整个目录递归到ZIP文件。
在本教程中,我们将向我们展示如何使用PHP从文件夹中创建ZIP文件。

Ziparchiver类

ZiParchiver类有助于使用PHP Zi,7Achive从服务器上的文件夹(文件和子目目表)中的zip文件。

  • zipdir() - 此函数在递归地包括父目录中创建一个文件夹的zip。
  • '$sourcepath' - 要填充目录的相对路径。
  • '$outzippath' - 保存zip文件的路径。
  • Dirtozip() - 它是ZiParchiver类的辅助功能,用于将文件夹中的文件和子目录添加到zip文件。
<?php
Class ZipArchiver {

    /**
     * Zip a folder (including itself).
     * 
     * Usage:
     * Folder path that should be zipped.
     * 
     * @param $sourcePath string 
     * Relative path of directory to be zipped.
     * 
     * @param $outZipPath string 
     * Path of output zip file. 
     *
     */
    public static function zipDir($sourcePath, $outZipPath){
        $pathInfo = pathinfo($sourcePath);
        $parentPath = $pathInfo['dirname'];
        $dirName = $pathInfo['basename'];

        $z = new ZipArchive();
        $z->open($outZipPath, ZipArchive::CREATE);
        $z->addEmptyDir($dirName);
        if($sourcePath == $dirName){
            self::dirToZip($sourcePath, $z, 0);
        }else{
            self::dirToZip($sourcePath, $z, strlen("$parentPath/"));
        }
        $z->close();

        return true;
    }

    /**
     * Add files and sub-directories in a folder to zip file.
     * 
     * @param $folder string
     * Folder path that should be zipped.
     * 
     * @param $zipFile ZipArchive
     * Zip file where files end up.
     * 
     * @param $exclusiveLength int 
     * Number of text to be excluded from the file path. 
     *
     */
    private static function dirToZip($folder, &$zipFile, $exclusiveLength){
        $handle = opendir($folder);
        while(FALSE !== $f = readdir($handle)){
            //Check for local/parent path or zipping file itself and skip
            if($f != '.' && $f != '..' && $f != basename(__FILE__)){
                $filePath = "$folder/$f";
                //Remove prefix from file path before add to zip
                $localPath = substr($filePath, $exclusiveLength);
                if(is_file($filePath)){
                    $zipFile->addFile($filePath, $localPath);
                }elseif(is_dir($filePath)){
                    //Add sub-directory
                    $zipFile->addEmptyDir($localPath);
                    self::dirToZip($filePath, $zipFile, $exclusiveLength);
                }
            }
        }
        closedir($handle);
    }

}

在PHP中创建zip文件

使用ziparchiver类存档给定文件夹的所有文件和子目录,并从PHP中的脚本创建ZIP文件。

  • 包含并初始化ZiParrive类。
  • 指定要作为zip存档的目录的路径。
  • 指定在服务器上保存zip文件的路径。
  • 调用ziparchiver类的'zipdir()'函数创建zip。
//Include and initialize ZipArchive class
require_once 'ZipArchiver.class.php';
$zipper = new ZipArchiver;
//Path of the directory to be zipped
$dirPath = '/path/to/sourceDir';
//Path of output zip file
$zipPath = '/path/to/archive-'.time().'.zip';
//Create zip archive
$zip = $zipper->zipDir($dirPath, $zipPath);
if($zip){
    echo 'ZIP archive created successfully.';
}else{
    echo 'Failed to create ZIP.';
}
日期:2020-06-02 22:15:27 来源:oir作者:oir