Archive on the fly, Version 1.2c

Zurück zur Übersicht

Datei: aotf/vs.unzip.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.unzip.php (class to read zip archives)

    Version 1.2c - 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.
 */

class vsUnzipFile {

    var $handle;        // Handle to the file
    var $filesize;      // Size of the file
    var $names;         // List with names and indexes
    var $directory;     // Central directory
    var $count;         // Number of entries in central directory
    var $comment;       // ZIP file comment
    var $errors;        // Array with error messages

    function vsUnzipFile()
    {
        $this->handle = 0;
        $this->errors = array();
        $this->close();
    }

    // Close archive
    function close()
    {
        if (!empty($this->handle))
            fclose($this->handle);

        $this->handle = 0;
        $this->filesize = 0;
        $this->count = 0;
        $this->names = array();
        $this->directory = array();
    }

    function _dostime2time($dostime)
    {
        return mktime( ($dostime >> 11) & 0x1f,
                       ($dostime >>  5) & 0x3f,
                       ($dostime <<  1) & 0x3f,
                       ($dostime >> 21) & 0x0f,
                       ($dostime >> 16) & 0x1f,
                      (($dostime >> 25) & 0x7f) + 1980);
    }

    function _unpack_cdfh($dir, $offs)
    {
        $hdr = unpack('Vmagic/v2version/vgpbf/vmethod/'
                    . 'Vdosdate/Vcrc32/Vcsize/Vosize/'
                    . 'vnamelen/vextra/vcmntlen/vdisk/'
                    . 'viattr/Veattr/Voffset', $dir);
        if ($hdr['magic'] != 0x02014b50
            || $hdr['version2'] > 20
            || $hdr['offset'] < 0
            || $hdr['offset'] > $offs - 30)
        {
            $this->errors[] = 'Invalid central directory file header entry';
            return false;
        }

        if ($hdr['method'] != 0 && $hdr['method'] != 8)
        {
            $this->errors[] = 'Unknown compression method';
            return false;
        }

        $name = substr($dir, 46, $hdr['namelen']);
        $extra = substr($dir, 46 + $hdr['namelen'], $hdr['extra']);

        $eattr = $hdr['eattr'] & 0x3f;
        if (substr($name, -1, 1) == '/')
            $eattr |= 0x10;

        return array(
            'magic'  => $hdr['magic'],
            'name'   => $name,
            'gpbf'   => $hdr['gpbf'],
            'method' => $hdr['method'],
            'time'   => $this->_dostime2time($hdr['dosdate']),
            'crc32'  => $hdr['crc32'],
            'csize'  => $hdr['csize'],
            'osize'  => $hdr['osize'],
            'eattr'  => $eattr,
            'offset' => $hdr['offset'],
            'extra'  => $extra,
            'hdrlen' => 46 + $hdr['namelen'] + $hdr['extra'] + $hdr['cmntlen']
        );
    }

    function _unpack_lfh($dir)
    {
        $hdr = unpack('Vmagic/vversion/vgpbf/vmethod/'
                    . 'Vdosdate/Vcrc32/Vcsize/Vosize/'
                    . 'vnamelen/vextra', $dir);
        if ($hdr['magic'] != 0x04034b50
            || $hdr['version'] > 20)
        {
            $this->errors[] = 'Invalid local file header entry';
            return false;
        }

        if ($hdr['method'] != 0 && $hdr['method'] != 8)
        {
            $this->errors[] = 'Unknown compression method';
            return false;
        }

        $name = substr($dir, 30, $hdr['namelen']);
        $extra = substr($dir, 30 + $hdr['namelen'], $hdr['extra']);

        return array(
            'magic'  => $hdr['magic'],
            'name'   => $name,
            'gpbf'   => $hdr['gpbf'],
            'method' => $hdr['method'],
            'time'   => $this->_dostime2time($hdr['dosdate']),
            'crc32'  => $hdr['crc32'],
            'csize'  => $hdr['csize'],
            'osize'  => $hdr['osize'],
            'extra'  => $extra,
            'hdrlen' => 30 + $hdr['namelen'] + $hdr['extra']
        );
    }

    function open($filename)
    {
        $this->close();
        $this->errors = array();

        $this->handle = @fopen($filename, "rb");
        if ($this->handle === false)
        {
            $this->errors[] = 'Cannot open input file';
            $this->handle = 0;
            return false;
        }

        $this->filesize = filesize($filename);
        if ($this->filesize < 22)
        {
            $this->errors[] = 'Invalid input file';
            $this->close();
            return false;
        }

        $size = 1024;
        if ($size < $this->filesize)
            $size = $this->filesize;

        $offs = $this->filesize - $size;

        fseek($this->handle, $offs, SEEK_SET);
        $tail = fread($this->handle, $size);
        $sign = pack('Vvv', 0x06054b50, 0, 0);

        $pos = strpos($tail, $sign);
        if ($pos === false)
        {
            $this->errors[] = 'Invalid input file';
            $this->close();
            return false;
        }

        $offs += $pos;
        $tail = substr($tail, $pos);

        $eod = unpack('Vmagic/v2disk/v2count/Vdirlen/Voffset/vcomment', $tail);

        if ($eod['magic'] != 0x06054b50
            || $eod['disk1'] != 0
            || $eod['disk2'] != 0
            || $eod['count1'] != $eod['count2']
            || $eod['dirlen'] < 0
            || $eod['dirlen'] > $offs
            || $eod['offset'] < 0
            || $eod['offset'] + $eod['dirlen'] > $offs)
        {
            $this->errors[] = 'Invalid input file';
            $this->close();
            return false;
        }

        $this->comment = substr($tail, 22, $eod['comment']);

        $offs = $eod['offset'];

        fseek($this->handle, $offs, SEEK_SET);
        $dir = fread($this->handle, $eod['dirlen']);

        while ($dir != '')
        {
            $cde = $this->_unpack_cdfh($dir, $offs);
            if ($cde === false)
            {
                $this->close();
                return false;
            }

            $dir = substr($dir, $cde['hdrlen']);
            $name = $cde['name'];

            $this->count += 1;
            $this->names[] = $name;
            $this->directory[$name] = $cde;

            if ($cde['osize'] == 0)
                $this->directory[$name]['data'] = '';
        }

        return true;
    }

    // Fix the index
    function _fix_index($index)
    {
        if (eregi("^[0-9]+$", $index))
            $index = $this->names[intval($index)];

        return "$index";
    }

    // Return an entry from the directory
    function &get_entry($index)
    {
        return $this->directory[$this->_fix_index($index)];
    }

    // Return the data for the given entry
    function &get_data($index)
    {
        $entry =& $this->get_entry($index);

        if (!isset($entry['data']))
        {
            fseek($this->handle, $entry['offset'], SEEK_SET);
            $dir = fread($this->handle, 30 + 512);

            $lfh = $this->_unpack_lfh($dir);
            if ($lfh === false)
            {
                $entry['data'] = '';
                $entry['error'] = 1;
                return $entry['data'];
            }

            if (   $lfh['name'  ] != $entry['name']
                || $lfh['gpbf'  ] != $entry['gpbf'  ]
                || $lfh['method'] != $entry['method']
                || $lfh['time'  ] != $entry['time'  ]
                || $lfh['crc32' ] != $entry['crc32' ]
                || $lfh['csize' ] != $entry['csize' ]
                || $lfh['osize' ] != $entry['osize' ])
            {
                $this->errors[] = 'Central directory / local file header mismatch';
                $entry['data'] = '';
                $entry['error'] = 1;
                return $entry['data'];
            }

            fseek($this->handle, $entry['offset'] + $lfh['hdrlen'], SEEK_SET);
            if ($entry['method'] != 8)
                $entry['data'] = fread($this->handle, $entry['csize']);
            else
                $entry['data'] =
                    gzinflate(fread($this->handle, $entry['csize']), $entry['osize']);

            $crc32 = crc32($entry['data']);
            if ($crc32 != $entry['crc32'])
            {
                $this->errors[] = 'Checksum error';
                $entry['error'] = 1;
            }
        }

        return $entry['data'];
    }

}

?>
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).