Archive on the fly, Version 1.2a (alt)

Hinweis: dies ist nicht die neueste Version!

Zurück zur Übersicht

Datei: aotf/vs.zip.php

<?php

/*
    Flocke's "Archive on the Fly"

    Set of PHP classes to create archives on the fly plus a class to extract
    the contents of ZIP files. See the included file README.txt for information
    on how to use it.

    This file: vs.zip.php (class to create zip archives)

    Version 1.2 - always find the latest version at
    http://flocke.vssd.de/prog/code/php/aotf/

    Copyright (C) 2005 Volker Siebert <flocke@vssd.de>
    All rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
 */

require_once("vs.arc.php");

class vsZipFile extends vsArcFile {

    var $directory;     // Central directory

    // Reset to create a new archive
    function clear()
    {
        $this->directory = '';
        $this->_clear('application/x-zip', '.zip');
    }

    // Add a new entry to the file
    //   NAME is the stored file name
    //   DATE is the modification date/time
    //   FTYPE is one of the VSARC_FTYPE_ constants
    //   DATA is the uncompressed data for files and empty otherwise
    //   LINKTARGET is the target of the symbolic link
    function _add_entry($name, $date, $ftype, &$data, $linktarget = '')
    {
        // General variables
        $crc32 = crc32($data);      // CRC-32 checksum
        $osize = strlen($data);     // original data size
        $csize = $osize;            // compressed data size
        $nmlen = strlen($name);     // length of the filename
        $gpbf  = 0;                 // General purpose bit flag
        $meth  = 0;                 // Compression method (0=stored)
        $extra = '';
        $exlen = 0;

        // Check if we can compress the file
        if ($ftype != VSARC_FTYPE_FILE || $osize <= 3)
            $level = 0;
        else
            $level = $this->_fix_level();

        if ($level > 0)
        {
            $comp = gzdeflate($data, $level);
            if (strlen($comp) >= $osize)
            {
                $level = 0;
                unset($comp);
            }
            else
            {
                $data = &$comp;
                $csize = strlen($comp);

                $meth = 8;  // Compression method (8=deflated)
                if ($level <= 2)
                    $gpbf |= 2;
                elseif ($level >= 8)
                    $gpbf |= 4;
            }
        }

        // DOS date & time
        $date = getdate($date);
        if ($date['year'] < 1980)
            $dosdate = (1 << 21) | (1 << 16);
        else
            $dosdate = (($date['year'] - 1980) << 25)
                     | ( $date['mon']          << 21)
                     | ( $date['mday']         << 16)
                     | ( $date['hours']        << 11)
                     | ( $date['minutes']      <<  5)
                     | ( $date['seconds']      >>  1);

        // DOS attribute
        if ($ftype == VSARC_FTYPE_DIR)
            $dosattr = 0x10;    // directory
        else
            $dosattr = 0x20;    // archive

        if ($ftype == VSARC_FTYPE_SYMLINK && !empty($linktarget))
        {
            $extra = pack('vvVVvv',
                          0x000d,
                          12 + strlen($linktarget),
                          $date, $date, 0, 0)
                   . $linktarget;
            $exlen = strlen($extra);
        }

        // Local file header
        $lfh = pack('VvvvVVVVvv', 
                    0x04034b50,     // local file header signature
                    20,             // version needed to extract (2.0)
                    $gpbf,          // general purpose bit flag
                    $meth,          // compression method
                    $dosdate,       // last mod file time + date
                    $crc32,         // crc-32
                    $csize,         // compressed size
                    $osize,         // uncompressed size
                    $nmlen,         // file name length
                    $exlen);        // extra field length

        // Central directory file header
        $dfh = pack('VvvvvVVVVvvvvvVV',
                    0x02014b50,     // central file header signature
                    20,             // version made by (2.0)
                    20,             // version needed to extract (2.0)
                    $gpbf,          // general purpose bit flag
                    $meth,          // compression method
                    $dosdate,       // last mod file time + date
                    $crc32,         // crc-32
                    $csize,         // compressed size
                    $osize,         // uncompressed size
                    $nmlen,         // file name length
                    $exlen,         // extra field length
                    0,              // file comment length
                    0,              // disk number start
                    0,              // internal file attributes
                    $dosattr,       // external file attributes
                    $this->offset); // relative offset of local header

        // Add it to the data & directory streams
        $this->_add_data($lfh);
        $this->_add_data($name);
        $this->_add_data($extra);
        $this->_add_data($data);

        $this->directory .= $dfh;
        $this->directory .= $name;
        $this->directory .= $extra;
    }

    // Returns the final archive data that still needs to be written
    function finish()
    {
        $dirlen = strlen($this->directory);

        // End of central directory record
        $eod = pack('VvvvvVVv',
                    0x06054b50,     // end of central dir signature
                    0,              // number of this disk
                    0,              // number of the disk with the start of the central directory
                    $this->count,   // total number of entries in the central directory on this disk
                    $this->count,   // total number of entries in the central directory
                    $dirlen,        // size of the central directory
                    $this->offset,  // offset of start of central directory w.r.t. the starting disk number
                    0);             // .ZIP file comment length

        $res = $this->data . $this->directory . $eod;

        $this->clear();
        return $res;
    }

}

?>
Flocke's Garage
Valid HTML 4.01 Transitional Valid CSS!
(C) 2005-2018 Volker Siebert.
Creative Commons-LizenzvertragDer gesamte Inhalt dieser Webseite steht unter einer Creative Commons-Lizenz (sofern nicht anders angegeben).