Exemple #1
0
/**
 * @bref:将文件或者目录下的文件加入到打包文件中,
 * $zip->addFile($file)和$zip->addFile($file,$baseDir)的
 * 区别在于,如果采用第一种,解压后的文件夹的最外层目录是根目录/,而如果采用的是第二种,解压后的文件夹的最外层目录是压缩前的
 * 最外层目录的父目录,显然我们想要的是第二种
 * @param $path 待打包的文件或者文件夹
 * @param $zip zip包处理流
 * @param $baseDir 最外层目录的父目录
 * @author liuhengsheng
 */
function addFileToZip($path, $zip, $baseDir)
{
    $tempdirs = explode($baseDir, $path);
    $handler = opendir($path);
    //打开当前文件夹由$path指定。
    while (($filename = readdir($handler)) !== false) {
        if ($filename != "." && $filename != "..") {
            //文件夹文件名字为'.'和‘..’,不要对他们进行操作
            if (is_dir($path . "/" . $filename)) {
                // 如果读取的某个对象是文件夹,则递归
                addFileToZip($path . "/" . $filename, $zip, $baseDir);
            } else {
                //将文件加入zip对象
                $zip->addFile($path . "/" . $filename, $tempdirs[1] . '/' . $filename);
            }
        }
    }
    @closedir($path);
}
Exemple #2
0
function addFileToZip($path, $zip, $filterArray)
{
    $handler = opendir($path);
    //打开当前文件夹由$path指定
    while (($filename = readdir($handler)) !== false) {
        if ($filename != "." && $filename != ".." && $filename != "index.htm" && !in_array($filename, $filterArray)) {
            //文件夹文件名字为'.'和‘..’,不要对他们进行操作
            if (is_dir($path . "/" . $filename)) {
                // 如果读取的某个对象是文件夹,则递归
                addFileToZip($path . "/" . $filename, $zip, $filterArray);
            } else {
                //将文件加入zip对象
                if (file_exists($path . "/" . $filename) && is_readable($path . "/" . $filename)) {
                    $zip->addFile($path . "/" . $filename);
                }
            }
        }
    }
    @closedir($path);
}
Exemple #3
0
 function addFileToZip($path, $zip)
 {
     $handler = opendir($path);
     //打开当前文件夹由$path指定。
     /*
     循环的读取文件夹下的所有文件和文件夹
     其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,
     为了不陷于死循环,所以还要让$filename !== false。
     一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环
     */
     while (($filename = readdir($handler)) !== false) {
         if ($filename != "." && $filename != "..") {
             //文件夹文件名字为'.'和‘..’,不要对他们进行操作
             if (is_dir($path . "/" . $filename)) {
                 // 如果读取的某个对象是文件夹,则递归
                 addFileToZip($path . "/" . $filename, $zip);
             } else {
                 //将文件加入zip对象
                 $zip->addFile($path . "/" . $filename);
             }
         }
     }
     @closedir($path);
 }
Exemple #4
0
 /**
  * 资源下载
  * @return 
  */
 public function fetch()
 {
     if ($this->lock) {
         return;
     }
     $page_list = $this->get_data();
     foreach ($page_list as $file) {
         $this->fetch_src($file);
     }
     $this->log("fetch finish!");
     $this->status = 4;
     // 文件打包
     $filename = $this->path . '/' . $this->name . '.zip';
     if (class_exists('ZipArchive')) {
         $zip = new ZipArchive();
         if ($zip->open($filename, ZipArchive::OVERWRITE) === TRUE) {
             addFileToZip($this->path, $zip);
             //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
             $zip->close();
             //关闭处理的zip文件
         } else {
             $this->log("create zip failed!");
         }
     } else {
         $this->log("zip not exist !");
     }
 }
Exemple #5
0
 function addFileToZip($path, $zip)
 {
     //打开当前文件夹
     $handle = opendir($path);
     //为防止文件名本身可被转换为false的情况(比如为"0"),则需用不全等!==
     while ($file = readdir($handle) !== FALSE) {
         //过滤假文件夹
         if ($file != '.' && $file != '..') {
             if (is_dir($path . '/' . $file)) {
                 //对于子文件夹则递归调用本函数
                 addFileToZip($path . '/' . $file, $zip);
             } else {
                 $zip->addFile($path . '/' . $file);
             }
         }
     }
     closedir($path);
 }
Exemple #6
0
 function addFileToZip($path, $zip)
 {
     $handler = opendir($path);
     while (($filename = readdir($handler)) !== false) {
         if ($filename != "." && $filename != "..") {
             if (is_dir($path . "/" . $filename)) {
                 addFileToZip($path . "/" . $filename, $zip);
             } else {
                 $zip->addFile($path . "/" . $filename);
             }
         }
     }
     @closedir($path);
 }