php實現ZIP壓縮檔解壓縮

來源:互聯網
上載者:User

標籤:rpo   color   origin   sub   小數   ansi   close   file   檔案格式   

測試使用了兩個辦法都可以實現:

第一個:需要開啟配置php_aip.dll

 1 <?php 2 //需開啟配置 php_zip.dll 3 //phpinfo(); 4 header("Content-type:text/html;charset=utf-8"); 5  6 function get_zip_originalsize($filename, $path) { 7   //先判斷待解壓的檔案是否存在 8   if(!file_exists($filename)){ 9     die("檔案 $filename 不存在!");10   }11   $starttime = explode(‘ ‘,microtime()); //解壓開始的時間12 13   //將檔案名稱和路徑轉成windows系統預設的gb2312編碼,否則將會讀取不到14   $filename = iconv("utf-8","gb2312",$filename);15   $path = iconv("utf-8","gb2312",$path);16   //開啟壓縮包17   $resource = zip_open($filename);18   $i = 1;19   //遍曆讀取壓縮包裡面的一個個檔案20   while ($dir_resource = zip_read($resource)) {21     //如果能開啟則繼續22     if (zip_entry_open($resource,$dir_resource)) {23       //擷取當前項目的名稱,即壓縮包裡面當前對應的檔案名稱24       $file_name = $path.zip_entry_name($dir_resource);25       //以最後一個“/”分割,再用字串截取出路徑部分26       $file_path = substr($file_name,0,strrpos($file_name, "/"));27       //如果路徑不存在,則建立一個目錄,true表示可以建立多級目錄28       if(!is_dir($file_path)){29         mkdir($file_path,0777,true);30       }31       //如果不是目錄,則寫入檔案32       if(!is_dir($file_name)){33         //讀取這個檔案34         $file_size = zip_entry_filesize($dir_resource);35         //最大讀取6M,如果檔案過大,跳過解壓,繼續下一個36         if($file_size<(1024*1024*30)){37           $file_content = zip_entry_read($dir_resource,$file_size);38           file_put_contents($file_name,$file_content);39         }else{40           echo "<p> ".$i++." 此檔案已被跳過,原因:檔案過大, -> ".iconv("gb2312","utf-8",$file_name)." </p>";41         }42       }43       //關閉當前44       zip_entry_close($dir_resource);45     }46   }47   //關閉壓縮包48   zip_close($resource);49   $endtime = explode(‘ ‘,microtime()); //解壓結束的時間50   $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);51   $thistime = round($thistime,3); //保留3為小數52   echo "<p>解壓完畢!,本次解壓花費:$thistime 秒。</p>";53 }54 55 $size = get_zip_originalsize(‘../../textaa.zip‘,‘../../ffff/‘);

上述方法中:$filename:檔案名稱,是要解壓的檔案名稱,包括相對於方法的路徑,$path表示的是:解壓到(什麼目錄下,以/結束)

 

第二個方法是:使用pclzip內建的類,項目過程中因為路徑的問題採用的第一種方法

<?php/*php 從zip壓縮檔中提取檔案*/$zip = new ZipArchive;if ($zip->open(‘test.zip‘) === TRUE) {//中文檔案名稱要使用ANSI編碼的檔案格式  $zip->extractTo(‘foldername‘);//提取全部檔案  //$zip->extractTo(‘/my/destination/dir/‘, array(‘pear_item.gif‘, ‘testfromfile.php‘));//提取部分檔案  $zip->close();  echo ‘ok‘;} else {  echo ‘failed‘;}

 

php實現ZIP壓縮檔解壓縮

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.