Jun 12 2007

PHP zipping algorithm

php zipping library

How can we zip two files and save output as output.zip. For example, in gmail you can download all the attachment as a single zip file. How can we write that code using php.

Click more for Source Code:


/**
* Php code for file zip
* Author : Sajith.M.R
* Date: 02-02-2007
*/

/** Include the Pear Library for Zip */
include ('Archive/Zip.php');

/** Create a Zipping Object...
* Name of zip file to be created..
* You can specify the path too */
$obj = new Archive_Zip('test.zip');
/**
* create a file array of Files to be Added in Zip
*/
$files = array('black.gif',
'blue.gif',
);

/**
* creating zip file..if success do something else do something...
* if Error in file creation ..it is either due to permission problem (Solution: give 777 to that folder)
* Or Corruption of File Problem..
*/

if ($obj->create($files)) {
// echo 'Created successfully!';
} else {
//echo 'Error in file creation';
}

?>; // We'll be outputting a ZIP
header('Content-type: application/zip');

// It will be called test.zip
header('Content-Disposition: attachment; filename="test.zip"');

//read a file and send
readfile('test.zip');
?>;

//This will output test.zip to the browser. If you dont want zip file as output (just saving into directory) leave the second part of above code

//This will output test.zip to the browser. If you dont want zip file as output (just saving into directory) leave the second part of above code

Note:

Download the pear library from: http://download.pear.php.net/package/Archive_Zip-0.1.1.tgz

or

install via pear pear install http://download.pear.php.net/package/Archive_Zip-0.1.1.tgz

TAGS:

6 Comments on this post

Trackbacks

  1. Dante said:

    I like your site

    July 1st, 2007 at 1:41 am
  2. rofovnifo said:

    Hi all!

    Looks good! Very useful, good stuff. Good resources here. Thanks much!

    Bye

    July 1st, 2007 at 4:46 pm
  3. MiramarDesign said:

    I’ve always wondered / wanted to do this w/ a download but never seen a library to do it. Thanks!

    March 12th, 2008 at 3:21 pm
  4. den said:

    Thanks, i try use it.

    March 26th, 2008 at 4:58 pm
  5. shobn said:

    Thanks fpr the tutorial mate..I am trying to learn PHP!!!

    March 31st, 2008 at 4:12 pm
  6. gisho said:

    how about zipping an auto generated dynamic content e.g. a .php page?

    June 24th, 2008 at 9:27 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts