使用文件壓縮類, 注意傳的路徑是相對路徑.如果傳絕對路徑就把addFile里面的第二個參數去掉/
$zip = new ZipFolder(); $zipFile = './autoloadClass/Sxf/zip/img.zip';//生成壓縮文件的路徑 $path = './autoloadClass/Sxf/images/';//被壓縮文件夾的路徑 $zip->zip($zipFile,$path);
文件壓縮類
/** * Zip 文件包工具 * @author 王召波 * @date 2019-07-07 */ class ZipFolder { protected $zip; protected $root; protected $ignored_names; public function __construct(){ $this->zip = new ZipArchive; } /** * 解壓zip文件到指定文件夾 * * @access public * @param string $zipfile 壓縮文件路徑 * @param string $path 壓縮包解壓到的目標路徑 * @return booleam 解壓成功返回 true 否則返回 false */ public function unzip ($zipfile, $path) { if ($this->zip->open($zipfile) === true) { $file_tmp = @fopen($zipfile, "rb"); $bin = fread($file_tmp, 15); //只讀15字節 各個不同文件類型,頭信息不一樣。 fclose($file_tmp); /* 只針對zip的壓縮包進行處理 */ if (true === $this->getTypeList($bin)) { $result = $this->zip->extractTo($path); $this->zip->close(); return $result; } else { return false; } } return false; } /** * 創建壓縮文件 * @access public * @param string $zipfile 將要生成的壓縮文件路徑 * @param strng $folder 將要被壓縮的文件夾路徑 * @param array $ignored 要忽略的文件列表 * @return booleam 壓縮包生成成功返回true 否則返回 false */ public function zip ($zipfile, $folder, $ignored = null) { $this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array(); if ($this->zip->open($zipfile, ZIPARCHIVE::CREATE) !== true) { throw new Exception("cannot open <$zipfile>\n"); } $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder; if(strstr($folder, '/')) { $this->root = substr($folder, 0, strrpos($folder, '/')+1); $folder = substr($folder, strrpos($folder, '/')+1); } $this->createZip($folder); return $this->zip->close(); } /** * 遞歸添加文件到壓縮包 * * @access private * @param string $folder 添加到壓縮包的文件夾路徑 * @param string $parent 添加到壓縮包的文件夾上級路徑 * @return void */ private function createZip ($folder, $parent=null) { $full_path = $this->root . $parent . $folder; $zip_path = $parent . $folder; $this->zip->addEmptyDir($zip_path); $dir = new DirectoryIterator($full_path); foreach($dir as $file) { if(!$file->isDot()) { $filename = $file->getFilename(); if(!in_array($filename, $this->ignored_names)) { if($file->isDir()) { $this->createZip($filename, $zip_path.'/'); }else { //第二個參數是重命名文件名,帶上路徑就可以改變當前文件在壓縮包里面的路徑. $this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename); } } } } } /** * 讀取壓縮包文件與目錄列表 * * @access public * @param string $zipfile 壓縮包文件 * @return array 文件與目錄列表 */ public function fileList($zipfile) { $file_dir_list = array(); $file_list = array(); if ($this->zip->open($zipfile) == true) { for ($i = 0; $i < $this->zip->numFiles; $i++) { $numfiles = $this->zip->getNameIndex($i); if (preg_match('/\/$/i', $numfiles)) { $file_dir_list[] = $numfiles; } else { $file_list[] = $numfiles; } } } return array('files'=>$file_list, 'dirs'=>$file_dir_list); } /** * 得到文件頭與文件類型映射表 * * @author wengxianhu * @date 2013-08-10 * @param $bin string 文件的二進制前一段字符 * @return boolean */ private function getTypeList ($bin) { $array = array( array("504B0304", "zip") ); foreach ($array as $v) { $blen = strlen(pack("H*", $v[0])); //得到文件頭標記字節數 $tbin = substr($bin, 0, intval($blen)); ///需要比較文件頭長度 if(strtolower($v[0]) == strtolower(array_shift(unpack("H*", $tbin)))) { return true; } } return false; } }
參考:https://www.jb51.net/article/142637.htm