linux - php method to decompress .gz file format.
黄舟
黄舟 2017-06-08 11:01:56
0
2
968
  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!

黄舟
黄舟

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

reply all(2)
仅有的幸福

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);
学习ing

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template