天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

php ZipArchive壓縮函數詳解實例

用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
刪除壓縮包中的某一個文件名稱,同時也將文件刪除。

php技術php ZipArchive壓縮函數詳解實例,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 少妇无码吹潮久久精品AV | 欧美精品一区二区三区视频 | 亚洲国产成人在线视频 | 高清国产免费观看视频在线 | 亚洲欧美一区二区三区蜜芽 | 亚洲一区自拍高清亚洲精品 | 97亚洲狠狠色综合久久久久 | 翁止熄痒禁伦短文合集免费视频 | 久久精品国产亚洲AV麻豆欧美玲 | 午夜国产大片免费观看 | qvod播放电影| 国产中文视频无码成人精品 | 久草在线福利视频在线播放 | 97亚洲狠狠色综合久久位 | 九色PORNY蝌蚪视频首页 | 秋霞在线看片无码免费 | 色爰情人网站 | 蜜芽在线播放免费人成日韩视频 | 娇小亚裔被两个黑人 | 波多结衣一区二区三区 | 97超级碰久久久久香蕉人人 | 国产欧美无码亚洲 | 校园女教师之禁区 | 99精品视频免费在线观看 | 亚洲国产中文字幕新在线 | 99精品免费久久久久久久久日本 | 亲爱的妈妈6韩国电影免费观看 | adc免费观看| 国偷自产视频一区二区久 | 国语自产视频在线 | 97国产蝌蚪视频在线观看 | 亚洲黄色在线 | 久久国产av偷拍在线 | 国产精品无码AV天天爽人妻蜜桃 | 嗯啊不要老师 | 精品久久免费观看 | 色琪琪无码成人AV视频 | 精品高潮呻吟99AV无码 | 亚洲免费精品视频 | 狠狠狠色丁香婷婷综合久久 | 扒开小处30p |