You are here:  » Simple Gunzip or Unzip Code

Support Forum



Simple Gunzip or Unzip Code

Submitted by lunen on Mon, 2011-01-17 16:38 in

Do you have a simple PHP code to run after files have been imported that will Gunzip or Unzip files?

For example:

merchant1.txt.gz
merchant2.zip
merchant3.txt.gz
merchant4.zip

After decompression:

merchant1.txt
merchant2.xml
merchant3.txt
merchant4.csv

Thanks!

Submitted by support on Mon, 2011-01-17 16:45

Hi Lunen,

Do you mean prior to importing - I assume that this is after an automatic FTP process that you have set-up to transfer files to your /feeds/ folder...?

.gz files can be uncompressed in bulk easily as there is no filename data in the compressed file - it's the same filename but without .gz. So for .gz, in your fetch.sh you could have

cd /path/to/feeds/
/usr/bin/gzip -d \*.gz

The \ is required to "escape" the * otherwise it is replaced by the command interpreter that is running your fetch script.

.zip files can be processed by wildcard also, but you can't guarantee the output filename as this is stored in the .zip file format itself. If that's not a problem, the following would do the trick...

cd /path/to/feeds/
/usr/bin/unzip \*.gz

(the cd line can be common of course)

Otherwise, the safest thing to do would be to uncompress to a known filename after each fetch, as described in the automation guide...

http://www.pricetapestry.com/node/198

Apologies if misunderstood, or you're looking for something outside of a .sh file - just let me know...

Cheers,
David.
--
PriceTapestry.com

Submitted by lunen on Mon, 2011-01-17 16:57

Thanks david,

I'm using Rackspace cloud sites for hosting and I can't shell out. I'm just looking for a simple .php scrip to run inside the feeds folder. I have most of my feeds FTPed into the feeds folder, and I just need to unzip and import them. I know, it's may new years goal to automate all this. I'm using a script I made up that unzips each file by name. I call it unzipfiles.php and run it from the url.

<?php
// shell out to unzip command
  $filename = "NitroRCX-NitroRCX_Product_Catalog.txt.gz";
  $cmd = "/usr/bin/gunzip -f ".$filename." > ".$filename.".unzipped";
  exec($cmd);
  unlink($filename);
  ?>

It seems to work off and on... and I have to list each file name.

Thanks,

BTW -- can you email me your Paypal address?? I owe you some beers for your help!!

Submitted by support on Mon, 2011-01-17 17:40

Hi lunen,

If the above is working, have a go with just:

<?php
  // shell out to unzip command
  chdir("/path/to/feeds/");
  $cmd = "/usr/bin/gzip -d *.gz";
  exec($cmd);
?>

chdir() might be significant as PHP installations vary as to whether they internally change directory to the same folder as the script, so using chdir() to specify the actual server location of your /feeds/ folder should help (replace /path/to/feeds/ with your actual feeds folder path)

There shouldn't be any need for the unlink as gzip automatically replaces the .gz version with the base filename (without .gz)...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com