Skip to content

Instantly share code, notes, and snippets.

@pareshsojitra
Forked from ninadsp/FixZipArchive.php
Created February 17, 2021 16:52
Show Gist options
  • Save pareshsojitra/4ebd0f55cd15d71df85e093dd63f4738 to your computer and use it in GitHub Desktop.
Save pareshsojitra/4ebd0f55cd15d71df85e093dd63f4738 to your computer and use it in GitHub Desktop.

Revisions

  1. @ninadsp ninadsp revised this gist Jul 30, 2013. 2 changed files with 6 additions and 2 deletions.
    4 changes: 3 additions & 1 deletion FixZipArchive.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?
    /**
    * FlxZipArchive, Extends ZipArchiv.
    * Add Dirs with Files and Subdirs.
    @@ -48,4 +49,5 @@ private function addDirDo($location, $name) {
    $this->$do($location . $file, $name . $file);
    }
    } // EO addDirDo();
    }
    }
    ?>
    4 changes: 3 additions & 1 deletion sample_usage.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    //Don't forget to remove the trailing slash

    $the_folder = '/path/to/folder/to/be/zipped';
    @@ -12,4 +13,5 @@
    $za->close();
    }
    else
    echo 'Could not create a zip archive';
    echo 'Could not create a zip archive';
    ?>
  2. @ninadsp ninadsp created this gist Jul 28, 2013.
    51 changes: 51 additions & 0 deletions FixZipArchive.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    /**
    * FlxZipArchive, Extends ZipArchiv.
    * Add Dirs with Files and Subdirs.
    *
    * <code>
    * $archive = new FlxZipArchive;
    * // .....
    * $archive->addDir( 'test/blub', 'blub' );
    * </code>
    */
    class FlxZipArchive extends ZipArchive {
    /**
    * Add a Dir with Files and Subdirs to the archive
    *
    * @param string $location Real Location
    * @param string $name Name in Archive
    * @author Nicolas Heimann
    * @access private
    **/

    public function addDir($location, $name) {
    $this->addEmptyDir($name);

    $this->addDirDo($location, $name);
    } // EO addDir;

    /**
    * Add Files & Dirs to archive.
    *
    * @param string $location Real Location
    * @param string $name Name in Archive
    * @author Nicolas Heimann
    * @access private
    **/

    private function addDirDo($location, $name) {
    $name .= '/';
    $location .= '/';

    // Read all Files in Dir
    $dir = opendir ($location);
    while ($file = readdir($dir))
    {
    if ($file == '.' || $file == '..') continue;

    // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
    $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
    $this->$do($location . $file, $name . $file);
    }
    } // EO addDirDo();
    }
    15 changes: 15 additions & 0 deletions sample_usage.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    //Don't forget to remove the trailing slash

    $the_folder = '/path/to/folder/to/be/zipped';
    $zip_file_name = '/path/to/zip/archive.zip';

    $za = new FlxZipArchive;

    $res = $za->open($zip_file_name, ZipArchive::CREATE);

    if($res === TRUE) {
    $za->addDir($the_folder, basename($the_folder));
    $za->close();
    }
    else
    echo 'Could not create a zip archive';