当前位置: 移动技术网 > IT编程>开发语言>PHP > php ZipArchive压缩函数详解实例

php ZipArchive压缩函数详解实例

2019年04月03日  | 移动技术网IT编程  | 我要评论

用ziparchive压缩文件,这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启apache这样才能使用这个类库。
例1、生成zip 压缩文件

复制代码 代码如下:

<?php
/* 生成zip 压缩文件 */
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
/*
php zip压缩文件夹下面的所有文件
*/
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
删除压缩包中的某一个文件名称,同时也将文件删除。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网