PHP Zipping and entire directory

  • Tdotwire
  • Proficient
  • Proficient
  • User avatar
  • Posts: 486
  • Loc: Toronto

Post 3+ Months Ago

I am in a situation whereas I would like to copy an entire directory of files (approx 170,000) into a zip file that I could send to a different server.

Just something in php which would have a list of files in an array and then it would add each of them into either a zip, tar, gzip, or any other known zipped file format...

I am aware of bz2, however I do not have that installed on my webserver and I need to use something different.

If anyone could help me out...

thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • typhon
  • Guru
  • Guru
  • User avatar
  • Posts: 1294
  • Loc: Memphis, Tn

Post 3+ Months Ago

Code: [ Select ]
<?php
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
      echo "$file\n";
    }
  }
  closedir($handle);
}
?>
  1. <?php
  2. if ($handle = opendir('.')) {
  3.   while (false !== ($file = readdir($handle))) {
  4.     if ($file != "." && $file != "..") {
  5.       echo "$file\n";
  6.     }
  7.   }
  8.   closedir($handle);
  9. }
  10. ?>

This returns a listing of all file in a directory except '.' and '..'
IF thats what your asking.
How to go about scipting in a zip/gz/tar/etc Im clueless....I have no Idea about accessing those via scripts, although I know it can be done.
  • Tdotwire
  • Proficient
  • Proficient
  • User avatar
  • Posts: 486
  • Loc: Toronto

Post 3+ Months Ago

that's good, it scans through the entire directory (or if you have php5, you can use scandir(".")...)

what I need to have done is all those files that are in that directory need to be added into a zipped file...
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

PHP Code: [ Select ]
 
<?php
 
/*
 
Zip file creation class
makes zip files on the fly...
 
use the functions add_dir() and add_file() to build the zip file;
see example code below
 
by Eric Mueller
http://www.themepark.com
 
v1.1 9-20-01
 - added comments to example
 
v1.0 2-5-01
 
initial version with:
 - class appearance
 - add_file() and file() methods
 - gzcompress() output hacking
by Denis O.Philippov, webmaster at atlant.ru, www.atlant.ru
 
official ZIP file format: http://www. // pkware.com/appnote.txt
 
*/
 
class zipfile
{
   var $datasec = array(); // array to store compressed data
   var $ctrl_dir = array(); // central directory
   var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
   var $old_offset = 0;
 
   // adds "directory" to archive - do this before putting any files in directory!
   // $name - name of directory... like this: "path/"
   // ...then you can add files using add_file with names like "path/file.txt"
   function add_dir($name)  
   {
      $name = str_replace("\", "/", $name);
 
      $fr = "\x50\x4b\x03\x04";
      $fr .= "\x0a\x00";  // ver needed to extract
      $fr .= "\x00\x00";  // gen purpose bit flag
      $fr .= "\x00\x00";  // compression method
      $fr .= "\x00\x00\x00\x00"; // last mod time and date
 
      $fr .= pack("V",0); // crc32
      $fr .= pack("V",0); //compressed filesize
      $fr .= pack("V",0); //uncompressed filesize
      $fr .= pack("v", strlen($name) ); //length of pathname
      $fr .= pack("v", 0 ); //extra field length
      $fr .= $name;
      // end of "local file header" segment
 
      // no "file data" segment for path
 
      // "data descriptor" segment (optional but necessary if archive is not served as file)
      $fr .= pack("V",$crc); //crc32
      $fr .= pack("V",$c_len); //compressed filesize
      $fr .= pack("V",$unc_len); //uncompressed filesize
 
      // add this entry to array
      $this -> datasec[] = $fr;
 
      $new_offset = strlen(implode("", $this->datasec));
 
      // ext. file attributes mirrors MS-DOS directory attr byte, detailed
      // at http://support.microsoft.com/support/kb ... 5/0/19.asp
 
      // now add to central record
      $cdrec = "\x50\x4b\x01\x02";
      $cdrec .="\x00\x00";  // version made by
      $cdrec .="\x0a\x00";  // version needed to extract
      $cdrec .="\x00\x00";  // gen purpose bit flag
      $cdrec .="\x00\x00";  // compression method
      $cdrec .="\x00\x00\x00\x00"; // last mod time & date
      $cdrec .= pack("V",0); // crc32
      $cdrec .= pack("V",0); //compressed filesize
      $cdrec .= pack("V",0); //uncompressed filesize
      $cdrec .= pack("v", strlen($name) ); //length of filename
      $cdrec .= pack("v", 0 ); //extra field length  
      $cdrec .= pack("v", 0 ); //file comment length
      $cdrec .= pack("v", 0 ); //disk number start
      $cdrec .= pack("v", 0 ); //internal file attributes
      $ext = "\x00\x00\x10\x00";
      $ext = "\xff\xff\xff\xff";
      $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set
 
      $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
      $this -> old_offset = $new_offset;
 
      $cdrec .= $name;
      // optional extra field, file comment goes here
      // save to array
      $this -> ctrl_dir[] = $cdrec;
   }
 
   // adds "file" to archive  
   // $data - file contents
   // $name - name of file in archive. Add path if your want
   function add_file($data, $name)  
   {
      $name = str_replace("\", "/", $name);
 
      $fr = "\x50\x4b\x03\x04";
      $fr .= "\x14\x00";  // ver needed to extract
      $fr .= "\x00\x00";  // gen purpose bit flag
      $fr .= "\x08\x00";  // compression method
      $fr .= "\x00\x00\x00\x00"; // last mod time and date
 
      $unc_len = strlen($data);
      $crc = crc32($data);
      $zdata = gzcompress($data);
      $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
      $c_len = strlen($zdata);
      $fr .= pack("V",$crc); // crc32
      $fr .= pack("V",$c_len); //compressed filesize
      $fr .= pack("V",$unc_len); //uncompressed filesize
      $fr .= pack("v", strlen($name) ); //length of filename
      $fr .= pack("v", 0 ); //extra field length
      $fr .= $name;
      // end of "local file header" segment
 
      // "file data" segment
      $fr .= $zdata;
 
      // "data descriptor" segment (optional but necessary if archive is not served as file)
      $fr .= pack("V",$crc); //crc32
      $fr .= pack("V",$c_len); //compressed filesize
      $fr .= pack("V",$unc_len); //uncompressed filesize
 
      // add this entry to array
      $this -> datasec[] = $fr;
 
      $new_offset = strlen(implode("", $this->datasec));
 
      // now add to central directory record
      $cdrec = "\x50\x4b\x01\x02";
      $cdrec .="\x00\x00";  // version made by
      $cdrec .="\x14\x00";  // version needed to extract
      $cdrec .="\x00\x00";  // gen purpose bit flag
      $cdrec .="\x08\x00";  // compression method
      $cdrec .="\x00\x00\x00\x00"; // last mod time & date
      $cdrec .= pack("V",$crc); // crc32
      $cdrec .= pack("V",$c_len); //compressed filesize
      $cdrec .= pack("V",$unc_len); //uncompressed filesize
      $cdrec .= pack("v", strlen($name) ); //length of filename
      $cdrec .= pack("v", 0 ); //extra field length
      $cdrec .= pack("v", 0 ); //file comment length
      $cdrec .= pack("v", 0 ); //disk number start
      $cdrec .= pack("v", 0 ); //internal file attributes
      $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set
 
      $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
      $this -> old_offset = $new_offset;
 
      $cdrec .= $name;
      // optional extra field, file comment goes here
      // save to central directory
      $this -> ctrl_dir[] = $cdrec;
   }
 
   function file()
   {
      // dump out file
      $data = implode("", $this -> datasec);
      $ctrldir = implode("", $this -> ctrl_dir);
 
      return
         $data.
         $ctrldir.
         $this -> eof_ctrl_dir.
         pack("v", sizeof($this -> ctrl_dir)).  // total # of entries "on this disk"
         pack("v", sizeof($this -> ctrl_dir)).  // total # of entries overall
         pack("V", strlen($ctrldir)).        // size of central dir
         pack("V", strlen($data)).           // offset to start of central dir
         "\x00\x00";                      // .zip file comment length
   }
}
 
?>
 
 
  1.  
  2. <?php
  3.  
  4. /*
  5.  
  6. Zip file creation class
  7. makes zip files on the fly...
  8.  
  9. use the functions add_dir() and add_file() to build the zip file;
  10. see example code below
  11.  
  12. by Eric Mueller
  13. http://www.themepark.com
  14.  
  15. v1.1 9-20-01
  16.  - added comments to example
  17.  
  18. v1.0 2-5-01
  19.  
  20. initial version with:
  21.  - class appearance
  22.  - add_file() and file() methods
  23.  - gzcompress() output hacking
  24. by Denis O.Philippov, webmaster at atlant.ru, www.atlant.ru
  25.  
  26. official ZIP file format: http://www. // pkware.com/appnote.txt
  27.  
  28. */
  29.  
  30. class zipfile
  31. {
  32.    var $datasec = array(); // array to store compressed data
  33.    var $ctrl_dir = array(); // central directory
  34.    var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
  35.    var $old_offset = 0;
  36.  
  37.    // adds "directory" to archive - do this before putting any files in directory!
  38.    // $name - name of directory... like this: "path/"
  39.    // ...then you can add files using add_file with names like "path/file.txt"
  40.    function add_dir($name)  
  41.    {
  42.       $name = str_replace("\", "/", $name);
  43.  
  44.       $fr = "\x50\x4b\x03\x04";
  45.       $fr .= "\x0a\x00";  // ver needed to extract
  46.       $fr .= "\x00\x00";  // gen purpose bit flag
  47.       $fr .= "\x00\x00";  // compression method
  48.       $fr .= "\x00\x00\x00\x00"; // last mod time and date
  49.  
  50.       $fr .= pack("V",0); // crc32
  51.       $fr .= pack("V",0); //compressed filesize
  52.       $fr .= pack("V",0); //uncompressed filesize
  53.       $fr .= pack("v", strlen($name) ); //length of pathname
  54.       $fr .= pack("v", 0 ); //extra field length
  55.       $fr .= $name;
  56.       // end of "local file header" segment
  57.  
  58.       // no "file data" segment for path
  59.  
  60.       // "data descriptor" segment (optional but necessary if archive is not served as file)
  61.       $fr .= pack("V",$crc); //crc32
  62.       $fr .= pack("V",$c_len); //compressed filesize
  63.       $fr .= pack("V",$unc_len); //uncompressed filesize
  64.  
  65.       // add this entry to array
  66.       $this -> datasec[] = $fr;
  67.  
  68.       $new_offset = strlen(implode("", $this->datasec));
  69.  
  70.       // ext. file attributes mirrors MS-DOS directory attr byte, detailed
  71.       // at http://support.microsoft.com/support/kb ... 5/0/19.asp
  72.  
  73.       // now add to central record
  74.       $cdrec = "\x50\x4b\x01\x02";
  75.       $cdrec .="\x00\x00";  // version made by
  76.       $cdrec .="\x0a\x00";  // version needed to extract
  77.       $cdrec .="\x00\x00";  // gen purpose bit flag
  78.       $cdrec .="\x00\x00";  // compression method
  79.       $cdrec .="\x00\x00\x00\x00"; // last mod time & date
  80.       $cdrec .= pack("V",0); // crc32
  81.       $cdrec .= pack("V",0); //compressed filesize
  82.       $cdrec .= pack("V",0); //uncompressed filesize
  83.       $cdrec .= pack("v", strlen($name) ); //length of filename
  84.       $cdrec .= pack("v", 0 ); //extra field length  
  85.       $cdrec .= pack("v", 0 ); //file comment length
  86.       $cdrec .= pack("v", 0 ); //disk number start
  87.       $cdrec .= pack("v", 0 ); //internal file attributes
  88.       $ext = "\x00\x00\x10\x00";
  89.       $ext = "\xff\xff\xff\xff";
  90.       $cdrec .= pack("V", 16 ); //external file attributes - 'directory' bit set
  91.  
  92.       $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
  93.       $this -> old_offset = $new_offset;
  94.  
  95.       $cdrec .= $name;
  96.       // optional extra field, file comment goes here
  97.       // save to array
  98.       $this -> ctrl_dir[] = $cdrec;
  99.    }
  100.  
  101.    // adds "file" to archive  
  102.    // $data - file contents
  103.    // $name - name of file in archive. Add path if your want
  104.    function add_file($data, $name)  
  105.    {
  106.       $name = str_replace("\", "/", $name);
  107.  
  108.       $fr = "\x50\x4b\x03\x04";
  109.       $fr .= "\x14\x00";  // ver needed to extract
  110.       $fr .= "\x00\x00";  // gen purpose bit flag
  111.       $fr .= "\x08\x00";  // compression method
  112.       $fr .= "\x00\x00\x00\x00"; // last mod time and date
  113.  
  114.       $unc_len = strlen($data);
  115.       $crc = crc32($data);
  116.       $zdata = gzcompress($data);
  117.       $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  118.       $c_len = strlen($zdata);
  119.       $fr .= pack("V",$crc); // crc32
  120.       $fr .= pack("V",$c_len); //compressed filesize
  121.       $fr .= pack("V",$unc_len); //uncompressed filesize
  122.       $fr .= pack("v", strlen($name) ); //length of filename
  123.       $fr .= pack("v", 0 ); //extra field length
  124.       $fr .= $name;
  125.       // end of "local file header" segment
  126.  
  127.       // "file data" segment
  128.       $fr .= $zdata;
  129.  
  130.       // "data descriptor" segment (optional but necessary if archive is not served as file)
  131.       $fr .= pack("V",$crc); //crc32
  132.       $fr .= pack("V",$c_len); //compressed filesize
  133.       $fr .= pack("V",$unc_len); //uncompressed filesize
  134.  
  135.       // add this entry to array
  136.       $this -> datasec[] = $fr;
  137.  
  138.       $new_offset = strlen(implode("", $this->datasec));
  139.  
  140.       // now add to central directory record
  141.       $cdrec = "\x50\x4b\x01\x02";
  142.       $cdrec .="\x00\x00";  // version made by
  143.       $cdrec .="\x14\x00";  // version needed to extract
  144.       $cdrec .="\x00\x00";  // gen purpose bit flag
  145.       $cdrec .="\x08\x00";  // compression method
  146.       $cdrec .="\x00\x00\x00\x00"; // last mod time & date
  147.       $cdrec .= pack("V",$crc); // crc32
  148.       $cdrec .= pack("V",$c_len); //compressed filesize
  149.       $cdrec .= pack("V",$unc_len); //uncompressed filesize
  150.       $cdrec .= pack("v", strlen($name) ); //length of filename
  151.       $cdrec .= pack("v", 0 ); //extra field length
  152.       $cdrec .= pack("v", 0 ); //file comment length
  153.       $cdrec .= pack("v", 0 ); //disk number start
  154.       $cdrec .= pack("v", 0 ); //internal file attributes
  155.       $cdrec .= pack("V", 32 ); //external file attributes - 'archive' bit set
  156.  
  157.       $cdrec .= pack("V", $this -> old_offset ); //relative offset of local header
  158.       $this -> old_offset = $new_offset;
  159.  
  160.       $cdrec .= $name;
  161.       // optional extra field, file comment goes here
  162.       // save to central directory
  163.       $this -> ctrl_dir[] = $cdrec;
  164.    }
  165.  
  166.    function file()
  167.    {
  168.       // dump out file
  169.       $data = implode("", $this -> datasec);
  170.       $ctrldir = implode("", $this -> ctrl_dir);
  171.  
  172.       return
  173.          $data.
  174.          $ctrldir.
  175.          $this -> eof_ctrl_dir.
  176.          pack("v", sizeof($this -> ctrl_dir)).  // total # of entries "on this disk"
  177.          pack("v", sizeof($this -> ctrl_dir)).  // total # of entries overall
  178.          pack("V", strlen($ctrldir)).        // size of central dir
  179.          pack("V", strlen($data)).           // offset to start of central dir
  180.          "\x00\x00";                      // .zip file comment length
  181.    }
  182. }
  183.  
  184. ?>
  185.  
  186.  


You can see an example of this on RentYourDot.com

If you are really doing 170,000 files, you may need to call flush(); every once in a while so the browser doesn't time out.
  • typhon
  • Guru
  • Guru
  • User avatar
  • Posts: 1294
  • Loc: Memphis, Tn

Post 3+ Months Ago

And I found this but I dont know if it will do you any good. Do you have access to server setup? Or is this through someone elses server?

http://pear.php.net/package/File_Archive

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.