zip the folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swethak
    New Member
    • May 2008
    • 118

    zip the folder

    hi,

    i want to zip the folder using php. Any body plz help for that.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Swethak.

    Check out PHP's zip functions (http://php.net/zip).

    Comment

    • swethak
      New Member
      • May 2008
      • 118

      #3
      problem with creation of zip file

      hi,

      i have the code for to create the zip folder.But in that i got the errors.plz tell that whats the mistake in my code.

      createZip.inc.p hp

      [php]

      <?php
      class createZip {
      public $compressedData = array();
      public $centralDirecto ry = array(); // central directory
      public $endOfCentralDi rectory = "\x50\x4b\x05\x 06\x00\x00\x00\ x00"; //end of Central directory record
      public $oldOffset = 0;

      public function addDirectory($d irectoryName) {
      $directoryName = str_replace("\\ ", "/", $directoryName) ;

      $feedArrayRow = "\x50\x4b\x03\x 04";
      $feedArrayRow .= "\x0a\x00";
      $feedArrayRow .= "\x00\x00";
      $feedArrayRow .= "\x00\x00";
      $feedArrayRow .= "\x00\x00\x00\x 00";

      $feedArrayRow .= pack("V",0);
      $feedArrayRow .= pack("V",0);
      $feedArrayRow .= pack("V",0);
      $feedArrayRow .= pack("v", strlen($directo ryName) );
      $feedArrayRow .= pack("v", 0 );
      $feedArrayRow .= $directoryName;

      $feedArrayRow .= pack("V",0);
      $feedArrayRow .= pack("V",0);
      $feedArrayRow .= pack("V",0);

      $this -> compressedData[] = $feedArrayRow;

      $newOffset = strlen(implode( "", $this->compressedData ));

      $addCentralReco rd = "\x50\x4b\x01\x 02";
      $addCentralReco rd .="\x00\x00";
      $addCentralReco rd .="\x0a\x00";
      $addCentralReco rd .="\x00\x00";
      $addCentralReco rd .="\x00\x00";
      $addCentralReco rd .="\x00\x00\x00 \x00";
      $addCentralReco rd .= pack("V",0);
      $addCentralReco rd .= pack("V",0);
      $addCentralReco rd .= pack("V",0);
      $addCentralReco rd .= pack("v", strlen($directo ryName) );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $ext = "\x00\x00\x10\x 00";
      $ext = "\xff\xff\xff\x ff";
      $addCentralReco rd .= pack("V", 16 );

      $addCentralReco rd .= pack("V", $this -> oldOffset );
      $this -> oldOffset = $newOffset;

      $addCentralReco rd .= $directoryName;

      $this -> centralDirector y[] = $addCentralReco rd;
      }

      /**
      * Function to add file(s) to the specified directory in the archive
      *
      * @param $directoryName string
      *
      */

      public function addFile($data, $directoryName) {

      $directoryName = str_replace("\\ ", "/", $directoryName) ;

      $feedArrayRow = "\x50\x4b\x03\x 04";
      $feedArrayRow .= "\x14\x00";
      $feedArrayRow .= "\x00\x00";
      $feedArrayRow .= "\x08\x00";
      $feedArrayRow .= "\x00\x00\x00\x 00";

      $uncompressedLe ngth = strlen($data);
      $compression = crc32($data);
      $gzCompressedDa ta = gzcompress($dat a);
      $gzCompressedDa ta = substr( substr($gzCompr essedData, 0, strlen($gzCompr essedData) - 4), 2);
      $compressedLeng th = strlen($gzCompr essedData);
      $feedArrayRow .= pack("V",$compr ession);
      $feedArrayRow .= pack("V",$compr essedLength);
      $feedArrayRow .= pack("V",$uncom pressedLength);
      $feedArrayRow .= pack("v", strlen($directo ryName) );
      $feedArrayRow .= pack("v", 0 );
      $feedArrayRow .= $directoryName;

      $feedArrayRow .= $gzCompressedDa ta;

      $feedArrayRow .= pack("V",$compr ession);
      $feedArrayRow .= pack("V",$compr essedLength);
      $feedArrayRow .= pack("V",$uncom pressedLength);

      $this -> compressedData[] = $feedArrayRow;

      $newOffset = strlen(implode( "", $this->compressedData ));

      $addCentralReco rd = "\x50\x4b\x01\x 02";
      $addCentralReco rd .="\x00\x00";
      $addCentralReco rd .="\x14\x00";
      $addCentralReco rd .="\x00\x00";
      $addCentralReco rd .="\x08\x00";
      $addCentralReco rd .="\x00\x00\x00 \x00";
      $addCentralReco rd .= pack("V",$compr ession);
      $addCentralReco rd .= pack("V",$compr essedLength);
      $addCentralReco rd .= pack("V",$uncom pressedLength);
      $addCentralReco rd .= pack("v", strlen($directo ryName) );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("v", 0 );
      $addCentralReco rd .= pack("V", 32 );

      $addCentralReco rd .= pack("V", $this -> oldOffset );
      $this -> oldOffset = $newOffset;

      $addCentralReco rd .= $directoryName;

      $this -> centralDirector y[] = $addCentralReco rd;
      }

      /**
      * Fucntion to return the zip file
      *
      * @return zipfile (archive)
      */

      public function getZippedfile() {

      $data = implode("", $this -> compressedData) ;
      $controlDirecto ry = implode("", $this -> centralDirector y);

      return
      $data.
      $controlDirecto ry.
      $this -> endOfCentralDir ectory.
      pack("v", sizeof($this -> centralDirector y)).
      pack("v", sizeof($this -> centralDirector y)).
      pack("V", strlen($control Directory)).
      pack("V", strlen($data)).
      "\x00\x00";
      }

      /**
      *
      * Function to force the download of the archive as soon as it is created
      *
      * @param archiveName string - name of the created archive file
      */

      public function forceDownload($ archiveName) {
      $headerInfo = '';

      if(ini_get('zli b.output_compre ssion')) {
      ini_set('zlib.o utput_compressi on', 'Off');
      }

      // Security checks
      if( $archiveName == "" ) {
      echo "<html><title>P ublic Photo Directory - Download </title><body><BR ><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
      exit;
      }
      elseif ( ! file_exists( $archiveName ) ) {
      echo "<html><title>P ublic Photo Directory - Download </title><body><BR ><B>ERROR:</B> File not found.</body></html>";
      exit;
      }

      header("Pragma: public");
      header("Expires : 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private",false) ;
      header("Content-Type: application/zip");
      header("Content-Disposition: attachment; filename=".base name($archiveNa me).";" );
      header("Content-Transfer-Encoding: binary");
      header("Content-Length: ".filesize($arc hiveName));
      readfile("$arch iveName");

      }

      }

      ?>

      [/php]

      Zipfile.php

      [php]

      <?php
      include_once("c reateZip.inc.ph p");
      $createZip = new createZip;

      $createZip -> addDirectory("d ir/");

      $fileContents = file_get_conten ts("img.jpg");
      $createZip -> addFile($fileCo ntents, "dir/img.jpg");
      $fileName = "archive.zi p";
      $fd = fopen ($fileName, "wb");
      $out = fwrite ($fd, $createZip -> getZippedfile() );
      fclose ($fd);

      $createZip -> forceDownload($ fileName);
      @unlink($fileNa me);
      ?>
      [/php]

      error :

      Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in F:\Facebook\fur niture11\create Zip.inc.php on line 3

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        I've merged your new thread into this one, seeing as they are dealing with the same exact topic.
        Try not to spread the same problem over multiple threads. It can get very messy very quickly.

        As to the code.
        Which version of PHP are you running?

        The code you posted is clearly written for PHP5. The class visibility keywords are not supported by the old, deprecated, PHP4, nor any of it's predecessors, and would most likely cause parse errors, like the one you posted.

        If you are still using PHP4, I strongly recommend upgrading to PHP5 as soon as possible.

        Comment

        • swethak
          New Member
          • May 2008
          • 118

          #5
          Originally posted by swethak
          hi,

          i have the code for to create the zip folder.But in that i got the errors.plz tell that whats the mistake in my code.

          createZip.inc.p hp

          [php]

          <?php
          class createZip {
          public $compressedData = array();
          public $centralDirecto ry = array(); // central directory
          public $endOfCentralDi rectory = "\x50\x4b\x05\x 06\x00\x00\x00\ x00"; //end of Central directory record
          public $oldOffset = 0;

          public function addDirectory($d irectoryName) {
          $directoryName = str_replace("\\ ", "/", $directoryName) ;

          $feedArrayRow = "\x50\x4b\x03\x 04";
          $feedArrayRow .= "\x0a\x00";
          $feedArrayRow .= "\x00\x00";
          $feedArrayRow .= "\x00\x00";
          $feedArrayRow .= "\x00\x00\x00\x 00";

          $feedArrayRow .= pack("V",0);
          $feedArrayRow .= pack("V",0);
          $feedArrayRow .= pack("V",0);
          $feedArrayRow .= pack("v", strlen($directo ryName) );
          $feedArrayRow .= pack("v", 0 );
          $feedArrayRow .= $directoryName;

          $feedArrayRow .= pack("V",0);
          $feedArrayRow .= pack("V",0);
          $feedArrayRow .= pack("V",0);

          $this -> compressedData[] = $feedArrayRow;

          $newOffset = strlen(implode( "", $this->compressedData ));

          $addCentralReco rd = "\x50\x4b\x01\x 02";
          $addCentralReco rd .="\x00\x00";
          $addCentralReco rd .="\x0a\x00";
          $addCentralReco rd .="\x00\x00";
          $addCentralReco rd .="\x00\x00";
          $addCentralReco rd .="\x00\x00\x00 \x00";
          $addCentralReco rd .= pack("V",0);
          $addCentralReco rd .= pack("V",0);
          $addCentralReco rd .= pack("V",0);
          $addCentralReco rd .= pack("v", strlen($directo ryName) );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $ext = "\x00\x00\x10\x 00";
          $ext = "\xff\xff\xff\x ff";
          $addCentralReco rd .= pack("V", 16 );

          $addCentralReco rd .= pack("V", $this -> oldOffset );
          $this -> oldOffset = $newOffset;

          $addCentralReco rd .= $directoryName;

          $this -> centralDirector y[] = $addCentralReco rd;
          }

          /**
          * Function to add file(s) to the specified directory in the archive
          *
          * @param $directoryName string
          *
          */

          public function addFile($data, $directoryName) {

          $directoryName = str_replace("\\ ", "/", $directoryName) ;

          $feedArrayRow = "\x50\x4b\x03\x 04";
          $feedArrayRow .= "\x14\x00";
          $feedArrayRow .= "\x00\x00";
          $feedArrayRow .= "\x08\x00";
          $feedArrayRow .= "\x00\x00\x00\x 00";

          $uncompressedLe ngth = strlen($data);
          $compression = crc32($data);
          $gzCompressedDa ta = gzcompress($dat a);
          $gzCompressedDa ta = substr( substr($gzCompr essedData, 0, strlen($gzCompr essedData) - 4), 2);
          $compressedLeng th = strlen($gzCompr essedData);
          $feedArrayRow .= pack("V",$compr ession);
          $feedArrayRow .= pack("V",$compr essedLength);
          $feedArrayRow .= pack("V",$uncom pressedLength);
          $feedArrayRow .= pack("v", strlen($directo ryName) );
          $feedArrayRow .= pack("v", 0 );
          $feedArrayRow .= $directoryName;

          $feedArrayRow .= $gzCompressedDa ta;

          $feedArrayRow .= pack("V",$compr ession);
          $feedArrayRow .= pack("V",$compr essedLength);
          $feedArrayRow .= pack("V",$uncom pressedLength);

          $this -> compressedData[] = $feedArrayRow;

          $newOffset = strlen(implode( "", $this->compressedData ));

          $addCentralReco rd = "\x50\x4b\x01\x 02";
          $addCentralReco rd .="\x00\x00";
          $addCentralReco rd .="\x14\x00";
          $addCentralReco rd .="\x00\x00";
          $addCentralReco rd .="\x08\x00";
          $addCentralReco rd .="\x00\x00\x00 \x00";
          $addCentralReco rd .= pack("V",$compr ession);
          $addCentralReco rd .= pack("V",$compr essedLength);
          $addCentralReco rd .= pack("V",$uncom pressedLength);
          $addCentralReco rd .= pack("v", strlen($directo ryName) );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("v", 0 );
          $addCentralReco rd .= pack("V", 32 );

          $addCentralReco rd .= pack("V", $this -> oldOffset );
          $this -> oldOffset = $newOffset;

          $addCentralReco rd .= $directoryName;

          $this -> centralDirector y[] = $addCentralReco rd;
          }

          /**
          * Fucntion to return the zip file
          *
          * @return zipfile (archive)
          */

          public function getZippedfile() {

          $data = implode("", $this -> compressedData) ;
          $controlDirecto ry = implode("", $this -> centralDirector y);

          return
          $data.
          $controlDirecto ry.
          $this -> endOfCentralDir ectory.
          pack("v", sizeof($this -> centralDirector y)).
          pack("v", sizeof($this -> centralDirector y)).
          pack("V", strlen($control Directory)).
          pack("V", strlen($data)).
          "\x00\x00";
          }

          /**
          *
          * Function to force the download of the archive as soon as it is created
          *
          * @param archiveName string - name of the created archive file
          */

          public function forceDownload($ archiveName) {
          $headerInfo = '';

          if(ini_get('zli b.output_compre ssion')) {
          ini_set('zlib.o utput_compressi on', 'Off');
          }

          // Security checks
          if( $archiveName == "" ) {
          echo "<html><title>P ublic Photo Directory - Download </title><body><BR ><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
          exit;
          }
          elseif ( ! file_exists( $archiveName ) ) {
          echo "<html><title>P ublic Photo Directory - Download </title><body><BR ><B>ERROR:</B> File not found.</body></html>";
          exit;
          }

          header("Pragma: public");
          header("Expires : 0");
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
          header("Cache-Control: private",false) ;
          header("Content-Type: application/zip");
          header("Content-Disposition: attachment; filename=".base name($archiveNa me).";" );
          header("Content-Transfer-Encoding: binary");
          header("Content-Length: ".filesize($arc hiveName));
          readfile("$arch iveName");

          }

          }

          ?>

          [/php]

          Zipfile.php

          [php]

          <?php
          include_once("c reateZip.inc.ph p");
          $createZip = new createZip;

          $createZip -> addDirectory("d ir/");

          $fileContents = file_get_conten ts("img.jpg");
          $createZip -> addFile($fileCo ntents, "dir/img.jpg");
          $fileName = "archive.zi p";
          $fd = fopen ($fileName, "wb");
          $out = fwrite ($fd, $createZip -> getZippedfile() );
          fclose ($fd);

          $createZip -> forceDownload($ fileName);
          @unlink($fileNa me);
          ?>
          [/php]

          error :

          Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in F:\Facebook\fur niture11\create Zip.inc.php on line 3
          By using the php5 i solved the problem.In that only we created the zip file and created the new dir folder in that then after add the files into that folder.But my requirement is to convert the zip file from already existed folder.How i modify the above code.plz give the solution for that.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            That's pretty much how you have to do it. You create a new empty directory in the zip archive and then add the files.

            Comment

            Working...