【问题标题】:(PHP) Unzip and rename CSV files?(PHP) 解压缩并重命名 CSV 文件?
【发布时间】:2017-10-16 15:35:16
【问题描述】:

我想从一些发布者那里下载不同的提要。但不幸的是,它们首先被压缩为.gz,其次是格式不正确。您可以下载其中一个提要并查看它。他们没有任何文件规范......所以,我不得不自己添加.csv......

我现在的问题是,如何从不同的 url 解压缩这些文件? 我如何重命名它们,我知道。但是如何解压呢?

我已经搜索过了,找到了这个:

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

但是对于那些提要它不起作用...... 这里有两个示例文件:file one | file two

你有什么想法吗? - 将不胜感激! 问候!

【问题讨论】:

  • 这段代码有什么错误?

标签: php csv type-conversion unzip gunzip


【解决方案1】:

windows 10 + php7.1.4 可以工作。

以下代码具有相同的效果。

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

或者你可以尝试使用gzip命令解压,然后使用它。 Program execution Functions

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多