A blend of programming and seo

How to create a zip archive using PHP

The following is a library that allows you to generate zip file archives using php.


<?php
include('ziplib.php');

$zipfile = new Ziplib;
$zipfile->zl_add_file('This is a test file','path/to/file','g9');
//You can stream the ZIP file or write it in a file on your server
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename=\'testfile.zip\'');
echo $zipfile->zl_pack('zip file comments');
?>

This script will dynamically create a zip archive using the files specified with zl_add_file and output it to the browser (the final zip file will be named: testfile.zip).

Options

zl_add_file allows you to specify the compression level of the file file that will be added to the archive.

  • n (none)
  • b (bzip)
  • g (gzip)

Download

The php zip library can be downloaded Here

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Reddit
  • Twitter
  • HackerNews
  • StumbleUpon
  • Technorati

9 comments

1 pahan { 06.24.09 at 3:31 am }

can i use this code to achieve multiple files to a one zip file?
please. can u explain me how ?

2 James { 08.12.09 at 8:36 pm }

I believe you can just run the following command for each file you want to include:

$zipfile->zl_add_file(’This is a test file’,'path/to/file’,'g9′);

James

3 JibKAS { 08.27.09 at 7:42 am }

Do not waste your time on this class. use the all powerful PHP bundled extension ZipArchive. it can do anything you want. very easily

4 Justin (rawseo) { 08.27.09 at 8:24 am }

@jibKAS:

You are right,this is a much faster extension that you can use to create zip archives. However, some hosting providers don’t have it installed. Also, if you are releasing a program and want to make it as painless as possible for your end user, you can use this library.

5 IT???? | 2009?8/23?8/29????????? { 08.30.09 at 9:43 am }

[...] zip????????????? [...]

6 chirag { 11.03.09 at 4:33 pm }

it is possible to save zip file to some folder ?

7 Roy (rawseo) { 11.05.09 at 2:52 pm }

@chirag,

You can output it to a file or send it to the browser.

8 edmund { 01.05.10 at 2:46 am }

@Roy ,
Noob question : how to write it to file ?

9 pahan { 01.18.10 at 11:30 am }

can i use this class to code to achieve some directories together with some files also. lets say we have a mixture of files and directories.

Leave a Comment