Jump to content

Help with unzipping .gz files


affordit

Recommended Posts

Hello all

I have been trying to loop thru a dir. of .gz files and unzip them into the dir. that this script runs in, can not seem to get it can someone help? Here is what I have

$buffer_size = 4096; // read 4kb at a time
if ($handle = opendir('./gz')) {

    while ($in_file = readdir($handle)) {
if($in_file != "." && $in_file != ".." && $in_file != "index.php"){

$file = gzopen($in_name, 'rb');
$out_file = fopen($out_file_name, 'wb');


while(!gzeof($file)) {

    fwrite($out_file, gzread($file, $buffer_size));
}

fclose($out_file);
gzclose($file);

}
}		


    


closedir($handle);
}

Thank all

Link to comment
https://forums.phpfreaks.com/topic/228036-help-with-unzipping-gz-files/
Share on other sites

I changed to this and still get nothing

$buffer_size = 4096; // read 4kb at a time
if ($handle = opendir('./gz')) {

    while ($in_file = readdir($handle)) {
if($in_file != "." && $in_file != ".." && $in_file != "index.php"){
$out_file_name = str_replace('.gz', '', $in_file);
$file = gzopen($out_file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');


while(!gzeof($file)) {

    fwrite($out_file, gzread($file, $buffer_size));


fclose($out_file);
gzclose($file);
}
}
}		


    


closedir($handle);
}

You might want to try using gzfile to read the file into an array and then using file_put_contents to create the new file:

<?php
$files = glob('./gz/*.gz');
foreach ($files as $in_file) {
    $tmp = gzfile($in_file);
    file_put_contents(str_replace('.gz','',basename($in_file)),implode('',$tmp));
}
?>

 

Note: completely untested.

 

Ken

I just tried my code on my server and it worked fine. There was one .gz file in my gz directory and when the program completed the expanded file was in the directory where I ran the code.

 

You might want to put a debugging line in your code, something like:

<?php
$files = glob('./gz/*.gz');
foreach ($files as $in_file) {
    $tmp = gzfile($in_file);
    echo basename($in_file) . ' ==> ' . str_replace('.gz','',basename($in_file)) . "\n";
    file_put_contents(str_replace('.gz','',basename($in_file)),implode('',$tmp));
}
?>

 

to see if you're even getting the files.

 

Ken

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.