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

php ZipArchive壓縮函數(shù)詳解實(shí)例

用ZipArchive壓縮文件,這個(gè)是php的擴(kuò)展類,自php5.2版本以后就已經(jīng)支持這個(gè)擴(kuò)展,如果你在使用的時(shí)候出現(xiàn)錯(cuò)誤,查看下php.ini里面的extension=php_zip.dll前面的分號(hào)有沒(méi)有去掉,然后再重啟Apache這樣才能使用這個(gè)類庫(kù)。
例1、生成zip 壓縮文件
復(fù)制代碼 代碼如下:
<?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']);//去掉層級(jí)目錄
        }
        //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__ )); //網(wǎng)站路徑

$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 、壓縮文件夾下面的所有文
復(fù)制代碼 代碼如下:
<?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' 文件夾產(chǎn)生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
添加一個(gè)新的文件目錄
2.ZipArchive::addFile
將文件添加到指定zip壓縮包中。
3.ZipArchive::addFromString
添加的文件同時(shí)將內(nèi)容添加進(jìn)去
4.ZipArchive::close
關(guān)閉ziparchive
5.ZipArchive::extractTo
將壓縮包解壓
6.ZipArchive::open
打開(kāi)一個(gè)zip壓縮包
7.ZipArchive::getStatusString
返回壓縮時(shí)的狀態(tài)內(nèi)容,包括錯(cuò)誤信息,壓縮信息等等
8.ZipArchive::deleteIndex
刪除壓縮包中的某一個(gè)文件,如:deleteIndex(0)刪除第一個(gè)文件
9.ZipArchive::deleteName
刪除壓縮包中的某一個(gè)文件名稱,同時(shí)也將文件刪除。

php技術(shù)php ZipArchive壓縮函數(shù)詳解實(shí)例,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 99热都是精品 | 一二三四在线高清中文版免费观看电影 | 99久久国产综合精品成人影院 | 国产成人综合高清在线观看 | 久久亚洲精品中文字幕60分钟 | 黑人强伦姧人妻日韩那庞大的 | 久久视频在线视频观品15 | 做a爱片的全过程 | 四虎永久免费网址 | 成人亚洲视频在线观看 | 九九热伊人| 三级黄色在线 | 国产午夜精品不卡视频 | 妹妹我要操| 亚洲高清无码在线 视频 | 美女搜查官被高难度黑人在线播放 | 成人在线观看国产 | 公交车轮C关老师 | 99热只有这里有精品 | 亚洲免费无码中文在线亚洲在 | 亚州笫一色惰网站 | 91亚洲 欧美 国产 制服 动漫 | 久久精品国产亚洲AV忘忧草蜜臀 | 男女爽爽无遮挡午夜视频在线观看 | 狠狠狠的在啪线香蕉 | 国产成人一区二区三区在线观看 | 桃色窝 | 中文字幕 人妻熟女 | 最近日本免费观看MV免费 | 色欲AV精品人妻一区二区麻豆 | 波多结衣一区二区三区 | 沦为公交两奶头春药高潮迭起 | 国产性夜夜春夜夜爽1A片 | 国产乱人偷精品视频A人人澡 | 久久re热在线视频精99 | 久久电影精品久久99久久 | 亚洲国产精品第一影院在线观看 | 久久久GOGO无码啪啪艺术 | 8x8x我要打机飞在线观看 | 99视频精品免视3 | xxxxx69hd杨幂|