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.

8 Comments

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

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

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!!

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

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.

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...?

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

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

Recent Entries

  • Load Balancing with HAProxy

    Load Balancers are some of the most expensive bits of equipment small to medium size sites are likely to buy, even more expensive than database...

  • SixXS IPv6 and CentOS

    I thought its high time I get to spend some time with IPv6 so I signed up for a static tunnel from sixxs.net, apart from...

  • Location aware Bind for RedHat 5.3

    Previously I wrote about RPMs I built to GeoIP enable Bind using the original patches at http://www.caraytech.com/geodns/.I have now refreshed this for the latest CentOS...

  • CentOS 5.3

    CentOS 5.3 was released on the 1st of April, I've since updated a whole lot of my machines to this version and been very happy.There...

  • WIRED UK

    I was quite excited about the new UK edition Wired.  I'm not anymore.I got my first exposure to the Wired while in school in South...

Close