Home  >  Q&A  >  body text

linux - php method to decompress .gz file format.

  1. Now I have obtained the .gz file of the project through the url

  2. I want to process the contents of the decompressed compressed file

  3. Now I know a way, which is to use php to call the Linux system command tar to decompress and then process the data

I would like to ask friends who have dealt with it before. It can be handled by php function!

This is my first contact, please give me some advice!

黄舟黄舟2667 days ago919

reply all(2)I'll reply

  • 仅有的幸福

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

    php original sound support, the following is from 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);

    reply
    0
  • 学习ing

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

    Generally, php installation comes with the expansion package of tar. This is the decompression function of tar in my project, for reference only

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

    Also refer to: Three ways to decompress PHP tar format
    You can also call the linux commandtar in php through linux, and then execexecute

    reply
    0
  • Cancelreply