linux - php 解压.gz文件格式的方法。

查看:97
本文介绍了linux - php 解压.gz文件格式的方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

  1. 现在项目我通过url获取到了.gz的文件

  2. 我要处理解压处理压缩文件的内容

  3. 现在知道一种办法,是php调用linux的系统命令tar去解压,然后处理数据

我想问问有没有处理过的朋友,可以php函数处理的!

第一次接触,求指教!

解决方案

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

这篇关于linux - php 解压.gz文件格式的方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆