本节内容:
ZipArchive压缩函数
php编程中,生成zip压缩文件,使用ZipArchive函数即可。
ZipArchive是php的扩展类,自php5.2版本以后支持该扩展。
如果在使用报出错误,请查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启apache即可使用这个类库。
例1、生成zip 压缩文件
 
<?php 
/*
* 生成zip 压缩文件
* 编辑:www.jb200.com
*/
function create_zip($files = array(),$destination = '',$overwrite = false) { 
    //if the zip file already exists and overwrite is false, return false 
    if(file_exists($destination) && !$overwrite) { return false; } 
    //vars 
    $valid_files = array(); 
    //if files were passed in... 
    if(is_array($files)) { 
        //cycle through each file 
        foreach($files as $file) { 
            //make sure the file exists 
            if(file_exists($file)) { 
                $valid_files[] = $file; 
            } 
        } 
    } 
    //if we have good files... 
    if(count($valid_files)) { 
        //create the archive 
        $zip = new ZipArchive(); 
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
            return false; 
        } 
        //add the files 
        foreach($valid_files as $file) { 
            $file_info_arr= pathinfo($file); 
            $zip->addFile($file,$file_info_arr['basename']);//去掉层级目录 
        } 
        //debug 
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done! 
        $zip->close();
        //check to make sure the file exists 
        return file_exists($destination); 
    } 
    else
    { 
        return false; 
    } 
}
define('ROOTPATH',dirname ( __FILE__ )); //网站路径
$files_to_zip = array( 
    ROOTPATH.DIRECTORY_SEPARATOR.'PHP+jquery+Cookbook.pdf', 
    ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv'
); 
//if true, good; if false, zip creation failed 
$filename='my-archive.zip'; 
$result = create_zip($files_to_zip,$filename);
 
例2 、压缩文件夹中的所有文件
 
<?php 
/* 
* zip压缩文件夹下的所有文件
* 编辑:www.jb200.com
*/
class HZip 
{ 
  /** 
   * 添加文件和子目录的文件到zip文件 
   * @param string $folder 
   * @param ZipArchive $zipFile 
   * @param int $exclusiveLength Number of text to be exclusived from the file path. 
   */
  private static function folderToZip($folder, &$zipFile, $exclusiveLength) { 
    $handle = opendir($folder); 
    while (false !== $f = readdir($handle)) { 
      if ($f != '.' && $f != '..') { 
        $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)) { 
          // 添加子文件夹 
          $zipFile->addEmptyDir($localPath); 
          self::folderToZip($filePath, $zipFile, $exclusiveLength); 
        } 
      } 
    } 
    closedir($handle); 
  }
  /** 
   * Zip a folder (include itself). 
   * Usage: 
   *   HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); 
   * 
   * @param string $sourcePath Path of directory to be zip. 
   * @param string $outZipPath Path of output zip file. 
   */
  public static function zipDir($sourcePath, $outZipPath) 
  { 
    $pathInfo = pathInfo($sourcePath); 
    $parentPath = $pathInfo['dirname']; 
    $dirName = $pathInfo['basename']; 
    $sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug 
    $z = new ZipArchive(); 
    $z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件 
    $z->addEmptyDir($dirName);//建立文件夹 
    self::folderToZip($sourcePath, $z, strlen("$parentPath/")); 
    $z->close(); 
  } 
}
//调用示例
HZip::zipDir('yourlife', 'yourlife.zip'); 
?>
代码说明:
 
1.ZipArchive::addEmptyDir
添加一个新的文件目录
2.ZipArchive::addFile
将文件添加到指定zip压缩包中。
3.ZipArchive::addFromString
添加的文件同时将内容添加进去
4.ZipArchive::close
关闭ziparchive
5.ZipArchive::extractTo
将压缩包解压
6.ZipArchive::open
打开一个zip压缩包
7.ZipArchive::getStatusString
返回压缩时的状态内容,包括错误信息,压缩信息等等
8.ZipArchive::deleteIndex
删除压缩包中的某一个文件,例如:deleteIndex(0)删除第一个文件
9.ZipArchive::deleteName
删除压缩包中的某一个文件名称,同时也将文件删除。