duan7772 2017-05-17 09:08
浏览 195
已采纳

(PHP)解压缩并重命名CSV文件?

I want to download different feeds form some publishers. But the poor thing is, that they are first of all zipped as .gz and as second not in the right format. You can download one of the feeds and check it out. They do not have any filespec... So, I'm forced to add the .csv by myself..

My question now is, how can I unzip those files from the different urls? How I do rename them, I know. But how do I unzip them?

I already searched for it and found this one:

//This input should be from somewhere else, hard-coded in this example
$file_name = '2013-07-16.dump.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);

But with those feeds it doesn't work... Here a two example files: file one | file two

Do you have an idea? - Would be very grateful! Greetings!

  • 写回答

1条回答 默认 最新

  • doubiankang2845 2017-05-17 10:52
    关注

    windows 10 + php7.1.4 it's work.

    The following code has the same effect.

    ob_start();
    readgzfile($file_name);
    file_put_contents($output_filename', ob_get_contents());
    ob_clean();
    

    Or you can try to use the gzip command to decompress, and then use the it. Program execution Functions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。