More on ZIP File Creation

| | Comments (7)

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.

7 Comments

CE Tullos said:

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

R,I,Pienaar said:

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

tonibottos said:

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

ken aikin said:

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

Mohandass said:

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.

Tris said:

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

P said:

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

Leave a comment

Currently Reading

Archives