首頁  >  問答  >  主體

linux - php 解壓縮.gz檔案格式的方法。

  1. 現在專案我透過url取得到了.gz的檔案

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

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

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

第一次接觸,求指教!

黄舟黄舟2667 天前918

全部回覆(2)我來回復

  • 仅有的幸福

    仅有的幸福2017-06-08 11:03:56

    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);

    回覆
    0
  • 学习ing

    学习ing2017-06-08 11:03:56

    一般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

    回覆
    0
  • 取消回覆