現(xiàn)在項目我通過url獲取到了.gz的文件
我要處理解壓處理壓縮文件的內(nèi)容
現(xiàn)在知道一種辦法,是php調(diào)用linux的系統(tǒng)命令tar去解壓,然后處理數(shù)據(jù)
我想問問有沒有處理過的朋友,可以php函數(shù)處理的!
第一次接觸,求指教!
人生最曼妙的風景,竟是內(nèi)心的淡定與從容!
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);
一般php
安裝都自帶tar
的擴展包,這是我項目中tar
的解壓函數(shù),僅供參考
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
中調(diào)用linux
命令tar
然后exec
執(zhí)行