linux - php 解壓縮.gz檔案格式的方法。
黄舟
黄舟 2017-06-08 11:01:56
0
2
916
  1. 現在專案我透過url取得到了.gz的檔案

  2. #我要處理解壓處理壓縮檔案的內容

  3. 現在知道一種辦法,是php呼叫linux的系統指令tar去解壓縮,然後處理資料

#我想問有沒有處理過的朋友,可以php函數處理的!

第一次接觸,求指教!

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回覆(2)
仅有的幸福

php原聲支持,以下來自SO

// This input should be from somewhere else, hard-coded in this example
$file_name = 'file.txt.gz';

// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name);

// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');

// Keep repeating until the end of the input file
while(!gzeof($file)) {
    // Read buffer-size bytes
    // Both fwrite and gzread and binary-safe
    fwrite($out_file, gzread($file, $buffer_size));
}

// Files are done, close files
fclose($out_file);
gzclose($file);
学习ing

一般php安裝都自備tar的擴充包,這是我專案中tar的解壓縮函數,僅供參考

function get_files_name_in_tar($file) {
    require_once 'Archive/Tar.php';
    $ext = get_file_extension($file);
    $tar_handle = null;
    if ($ext === "bz2") {
        $tar_handle = new Archive_Tar($file, "bz2");
    } else if ($ext === "gz") {
        $tar_handle = new Archive_Tar($file, "gz");
    } else if ($ext === "tar") {
        $tar_handle = new Archive_Tar($file);
    } else {
        return false;
    }    
    if (!$tar_handle) {
        return false;
    }    
    $entry_names = $tar_handle->listContent();

    return array_column($entry_names, 'filename');
}

另外可參考:PHP tar格式解壓縮的三種方式
還可以透過linux,在php中呼叫linux指令tar

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!