Search code examples
phpcsvtype-conversionunzipgunzip

(PHP) Unzip and rename CSV files?


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!


Solution

  • 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