Now I have obtained the .gz file of the project through the url
I want to process the contents of the decompressed compressed file
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!
仅有的幸福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);
学习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 thelinux
commandtar
inphp
throughlinux
, and thenexec
execute