SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member subhasis_bubu's Avatar
    Join Date
    Sep 2005
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to zip a folder in PHP

    Hi all,

    I am facing a huge problem while trying to zip an entire folder using PHP
    This is to be done in such a way that the entire structure of the folder do not get hampered that is all its subfolders and files remain the same when the zip file is unzipped.

    Any sort of help is always welcome.

    Subhasis

  2. #2
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haven't tested it, but worth a look:
    http://www.planet-source-code.com/vb...d=957&lngWId=8

  3. #3
    SitePoint Member subhasis_bubu's Avatar
    Join Date
    Sep 2005
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks spamonkey8,

    but my problem is that I cannot zip a folder which has subfolders within it. I want to make the zip file in such a way that the structure is never hampeered. The code you suggested is for zipping a folder which contains files only and no sub folders.

    Any way thanks for your response
    Subhasis

  4. #4
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you will need to write a little recursion (or a stack maybe) to navigate the tree and add all the files in certain directories individually.

    Yes, I tested this and it is completely compatible with WinRAR.

  5. #5
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Example:

    PHP Code:
    $zipfile = new zipfile();

    //The page says "add the subdirectory ... important!", but
    //in my tests it wasn't needed.  It may only be needed to make
    //and empty directory
    //$zipfile -> add_dir("data/");

    $filedata "(read your file into \$filedata)";
    $zipfile -> add_file($filedata"file.txt");

    $filedata file_get_contents('/input/a/file/called/script.php');
    $zipfile -> add_file($filedata"script.php");

    $filedata "foo to the bar";
    $zipfile -> add_file($filedata"subfolder/foo.bar");

    file_put_contents('/path/to/output/test.zip'$zipfile -> file()); 

  6. #6
    SitePoint Member subhasis_bubu's Avatar
    Join Date
    Sep 2005
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot !!
    I am trying this out right now
    Subhasis

  7. #7
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Even better! I took a bit of interest in this (I have the perfect use for it) and decided to go with it. You should add these two functions to the zipfile class and call add_dir_recur() as in the example to recursively add a directory, including all files and even empty folders. This is based upon the function du() I found on php.net a while ago (which calculates the total size of the files in a directory), then modified to track the number of files and dirs, then adapted for this use. This was written and fully tested for Windows servers but you can probably just reverse the slashes for Unix-compatability. Enjoy!

    PHP Code:
    //Add me to your zipfile class
    function add_dir_recur($location) {
        
    //Add a trailing slash
        
    if (substr($location, -11) != '\\')
            
    $location .= '\\';
        return 
    $this -> add_dir_recur_helper($location$location);
    }
    //Add me to your zipfile class
    function add_dir_recur_helper($root$location) {
        if (!
    $location || !is_dir($location))
            return 
    0;

        
    $size 0;
        
    $files 0;
        
    $dirs 0;

        
    $all opendir($location);
        while (
    $file readdir($all)) {
            if (
    is_dir($location.'\\'.$file) and $file != ".." and $file != ".") {
                
    $temp $this -> add_dir_recur_helper($root$location.'\\'.$file);
                
    $size += $temp['size'];
                
    $files += $temp['files'];
                
    $dirs += $temp['dirs'] + 1;
                
                
    $this -> add_dir(str_replace($root.'\\'''$location.'\\'.$file).'/');
                
                unset(
    $temp);
            } elseif (!
    is_dir($location.'\\'.$file)) {
                
    $stats stat($location.'\\'.$file);
                
    $size += $stats['size'];
                
    $files++;
                
                
    $filedata file_get_contents($location.'\\'.$file);
                if (
    $location == $root)
                    
    $this -> add_file($filedata$file);
                else
                    
    $this -> add_file($filedatastr_replace($root.'\\'''$location.'\\'.$file));
                
                unset(
    $filedata);
            }
            unset(
    $file);
        }
        
    closedir($all);
        unset(
    $all);
        return array(
    'size' => $size'files' => $files'dirs' => $dirs);
    }

    //Example usage:

    $zipfile = new zipfile();

    //It doesn't matter if there's a trailing slash
    $info $zipfile -> add_dir_recur('D:\test\path');

    echo 
    number_format($info['size'])." bytes in ".$info['files']." files and ".$info['dirs']." directories added to test.zip";

    file_put_contents('D:\folder\test.zip'$zipfile -> file()); 

  8. #8
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In trying to make an inverse function based upon code from php.net's read-only zip file extension, I ran into some problems. The script hangs and then Apache won't work until the server is restarted. (The service can't even be stopped, started, or restarted!)

    Is there a known issue with this?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's another option, Pear File_Archive:

    http://poocl.la-grotte.org/index.php

    Haven't used it before though.

  10. #10
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also look at http://ez.no/products/ez_components , they have Zip Archive handling classes.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •