www.devco.net by r.i.pienaar

25May/058

More on ZIP File Creation

I did some more testing with the code I posted yesterday and found it isn't 100% compatible with some unzip programs. Works with unix unzip, Mac OS X default tool, WinZip, WinRAR but annoyingly not with the default XP zip folder thing.

The problem seem to be 4 rogue bytes that gets inserted somewhere. So back to Google, eventually I found a much better library at PHP Concept Library Zip. It works more or less the same, except you must pass it files to add instead of just data in variables, either works for me.

Some sample code:

require ("incl/pclzip.lib.php");
$zipfile = new PclZip('zipfile.zip');
$v_list = $zipfile->create('incl/pclzip.lib.php');
if ($v_list == 0) {
die ("Error: " . $zipfile->errorInfo(true));
}
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=zipfile.zip");
readfile("zipfile.zip");

This one works with every unzip tool I've tried, Windows, OS X, command line unzip etc. It also has decent error reporting etc.

About R.I. Pienaar

Systems Administrator, Consultant, Linux Guy, Automator, Ruby Coder
Tagged as: , Leave a comment
Comments (8) Trackbacks (0)
  1. Hey, i was wondering how you make it save to file, i can’t seem to find an example anywhere that will allow me to save the created zip to a specific directory on my web server

  2. In the example above the file will be saved in ‘zipfile.zip’ you can put any path in there.

  3. Have you tried to extract a file that is not in the same directory of the php script?
    Something like
    $zipfile = new PclZip(‘test\zipfile.zip’)
    I was not able to extract it!!

  4. I have been trying to use the example code above, but I get an error:
    Error: PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open archive ‘zipfile.zip’ in wb mode.
    I have tried changing the filename to something that doesn’t exist, and that gives a different error, so I know that php is able to access the file that I want to zip.
    Are there some settings in php that have to be enabled to make this execute?
    Thanks

  5. Hi … i can convert one single file to zip format using this code . How can use this code for converting a folder to zip foramt.

  6. I got this to work great… until I tried to include an 80Mb file.. and it all fell down.
    has anyone had any luck with compiling ‘large’ zip files…?

  7. Both Windows XP and Winrar are unable to read the zips this thing generates. Says they are invalid format. Booo.

  8. I was just wondering if you could tell me how to stop the open box from popping up. Essentially I want to just zip the file and echo that it has been zipped. Thanks,


Leave a comment


Trackbacks are disabled.