<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Archive::Zip::Member;

# A generic member of an archive

use strict;
use vars qw( $VERSION @ISA );

BEGIN {
    $VERSION = '1.67';
    @ISA     = qw( Archive::Zip );

    if ($^O eq 'MSWin32') {
        require Win32;
        require Encode;
        Encode-&gt;import(qw{ decode_utf8 });
    }
}

use Archive::Zip qw(
  :CONSTANTS
  :MISC_CONSTANTS
  :ERROR_CODES
  :PKZIP_CONSTANTS
  :UTILITY_METHODS
);

use Time::Local ();
use Compress::Raw::Zlib qw( Z_OK Z_STREAM_END MAX_WBITS );
use File::Path;
use File::Basename;

# Unix perms for default creation of files/dirs.
use constant DEFAULT_DIRECTORY_PERMISSIONS =&gt; 040755;
use constant DEFAULT_FILE_PERMISSIONS      =&gt; 0100666;
use constant DIRECTORY_ATTRIB              =&gt; 040000;
use constant FILE_ATTRIB                   =&gt; 0100000;
use constant OS_SUPPORTS_SYMLINK           =&gt; do {
  local $@;
  !!eval { symlink("",""); 1 };
};

# Returns self if successful, else undef
# Assumes that fh is positioned at beginning of central directory file header.
# Leaves fh positioned immediately after file header or EOCD signature.
sub _newFromZipFile {
    my $class = shift;
    my $self  = Archive::Zip::ZipFileMember-&gt;_newFromZipFile(@_);
    return $self;
}

sub newFromString {
    my $class = shift;

    my ($stringOrStringRef, $fileName);
    if (ref($_[0]) eq 'HASH') {
        $stringOrStringRef = $_[0]-&gt;{string};
        $fileName          = $_[0]-&gt;{zipName};
    } else {
        ($stringOrStringRef, $fileName) = @_;
    }

    my $self =
      Archive::Zip::StringMember-&gt;_newFromString($stringOrStringRef, $fileName);
    return $self;
}

sub newFromFile {
    my $class = shift;

    my ($fileName, $zipName);
    if (ref($_[0]) eq 'HASH') {
        $fileName = $_[0]-&gt;{fileName};
        $zipName  = $_[0]-&gt;{zipName};
    } else {
        ($fileName, $zipName) = @_;
    }

    my $self =
      Archive::Zip::NewFileMember-&gt;_newFromFileNamed($fileName, $zipName);
    return $self;
}

sub newDirectoryNamed {
    my $class = shift;

    my ($directoryName, $newName);
    if (ref($_[0]) eq 'HASH') {
        $directoryName = $_[0]-&gt;{directoryName};
        $newName       = $_[0]-&gt;{zipName};
    } else {
        ($directoryName, $newName) = @_;
    }

    my $self =
      Archive::Zip::DirectoryMember-&gt;_newNamed($directoryName, $newName);
    return $self;
}

sub new {
    my $class = shift;
    # Info-Zip 3.0 (I guess) seems to use the following values
    # for the version fields in local and central directory
    # headers, regardless of whether the member has an zip64
    # extended information extra field or not:
    #
    #   version made by: 
    #     30
    #
    #   version needed to extract:
    #     10 for directory and stored entries
    #     20 for anything else
    my $self  = {
        'lastModFileDateTime'      =&gt; 0,
        'fileAttributeFormat'      =&gt; FA_UNIX,
        'zip64'                    =&gt; 0,
        'desiredZip64Mode'         =&gt; ZIP64_AS_NEEDED,
        'versionMadeBy'            =&gt; 20,
        'versionNeededToExtract'   =&gt; 20,
        'bitFlag'                  =&gt; ($Archive::Zip::UNICODE ? 0x0800 : 0),
        'compressionMethod'        =&gt; COMPRESSION_STORED,
        'desiredCompressionMethod' =&gt; COMPRESSION_STORED,
        'desiredCompressionLevel'  =&gt; COMPRESSION_LEVEL_NONE,
        'internalFileAttributes'   =&gt; 0,
        'externalFileAttributes'   =&gt; 0,                        # set later
        'fileName'                 =&gt; '',
        'cdExtraField'             =&gt; '',
        'localExtraField'          =&gt; '',
        'fileComment'              =&gt; '',
        'crc32'                    =&gt; 0,
        'compressedSize'           =&gt; 0,
        'uncompressedSize'         =&gt; 0,
        'password'                 =&gt; undef,    # password for encrypted data
        'crc32c'                   =&gt; -1,       # crc for decrypted data
        @_
    };
    bless($self, $class);
    $self-&gt;unixFileAttributes($self-&gt;DEFAULT_FILE_PERMISSIONS);
    return $self;
}

# Morph into given class (do whatever cleanup I need to do)
sub _become {
    return bless($_[0], $_[1]);
}

sub fileAttributeFormat {
    my $self = shift;

    if (@_) {
        $self-&gt;{fileAttributeFormat} =
          (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{format} : $_[0];
    } else {
        return $self-&gt;{fileAttributeFormat};
    }
}

sub zip64 {
    shift-&gt;{'zip64'};
}

sub desiredZip64Mode {
    my $self = shift;
    my $desiredZip64Mode = $self-&gt;{'desiredZip64Mode'};
    if (@_) {
        $self-&gt;{'desiredZip64Mode'} =
          ref($_[0]) eq 'HASH' ? shift-&gt;{desiredZip64Mode} : shift;
    }
    return $desiredZip64Mode;
}

sub versionMadeBy {
    shift-&gt;{'versionMadeBy'};
}

sub versionNeededToExtract {
    shift-&gt;{'versionNeededToExtract'};
}

sub bitFlag {
    my $self = shift;

# Set General Purpose Bit Flags according to the desiredCompressionLevel setting
    if (   $self-&gt;desiredCompressionLevel == 1
        || $self-&gt;desiredCompressionLevel == 2) {
        $self-&gt;{'bitFlag'} |= DEFLATING_COMPRESSION_FAST;
    } elsif ($self-&gt;desiredCompressionLevel == 3
        || $self-&gt;desiredCompressionLevel == 4
        || $self-&gt;desiredCompressionLevel == 5
        || $self-&gt;desiredCompressionLevel == 6
        || $self-&gt;desiredCompressionLevel == 7) {
        $self-&gt;{'bitFlag'} |= DEFLATING_COMPRESSION_NORMAL;
    } elsif ($self-&gt;desiredCompressionLevel == 8
        || $self-&gt;desiredCompressionLevel == 9) {
        $self-&gt;{'bitFlag'} |= DEFLATING_COMPRESSION_MAXIMUM;
    }

    if ($Archive::Zip::UNICODE) {
        $self-&gt;{'bitFlag'} |= 0x0800;
    }
    $self-&gt;{'bitFlag'};
}

sub password {
    my $self = shift;
    $self-&gt;{'password'} = shift if @_;
    $self-&gt;{'password'};
}

sub compressionMethod {
    shift-&gt;{'compressionMethod'};
}

sub desiredCompressionMethod {
    my $self = shift;
    my $newDesiredCompressionMethod =
      (ref($_[0]) eq 'HASH') ? shift-&gt;{compressionMethod} : shift;
    my $oldDesiredCompressionMethod = $self-&gt;{'desiredCompressionMethod'};
    if (defined($newDesiredCompressionMethod)) {
        $self-&gt;{'desiredCompressionMethod'} = $newDesiredCompressionMethod;
        if ($newDesiredCompressionMethod == COMPRESSION_STORED) {
            $self-&gt;{'desiredCompressionLevel'} = 0;
            $self-&gt;{'bitFlag'} &amp;= ~GPBF_HAS_DATA_DESCRIPTOR_MASK
                if $self-&gt;uncompressedSize() == 0;
        } elsif ($oldDesiredCompressionMethod == COMPRESSION_STORED) {
            $self-&gt;{'desiredCompressionLevel'} = COMPRESSION_LEVEL_DEFAULT;
        }
    }
    return $oldDesiredCompressionMethod;
}

sub desiredCompressionLevel {
    my $self = shift;
    my $newDesiredCompressionLevel =
      (ref($_[0]) eq 'HASH') ? shift-&gt;{compressionLevel} : shift;
    my $oldDesiredCompressionLevel = $self-&gt;{'desiredCompressionLevel'};
    if (defined($newDesiredCompressionLevel)) {
        $self-&gt;{'desiredCompressionLevel'}  = $newDesiredCompressionLevel;
        $self-&gt;{'desiredCompressionMethod'} = (
            $newDesiredCompressionLevel
            ? COMPRESSION_DEFLATED
            : COMPRESSION_STORED
        );
    }
    return $oldDesiredCompressionLevel;
}

sub fileName {
    my $self    = shift;
    my $newName = shift;
    if (defined $newName) {
        $newName =~ s{[\\/]+}{/}g;    # deal with dos/windoze problems
        $self-&gt;{'fileName'} = $newName;
    }
    return $self-&gt;{'fileName'};
}

sub fileNameAsBytes {
    my $self  = shift;
    my $bytes = $self-&gt;{'fileName'};
    if($self-&gt;{'bitFlag'} &amp; 0x800){
        $bytes = Encode::encode_utf8($bytes);
    }
    return $bytes;
}

sub lastModFileDateTime {
    my $modTime = shift-&gt;{'lastModFileDateTime'};
    $modTime =~ m/^(\d+)$/;           # untaint
    return $1;
}

sub lastModTime {
    my $self = shift;
    return _dosToUnixTime($self-&gt;lastModFileDateTime());
}

sub setLastModFileDateTimeFromUnix {
    my $self   = shift;
    my $time_t = shift;
    $self-&gt;{'lastModFileDateTime'} = _unixToDosTime($time_t);
}

sub internalFileAttributes {
    shift-&gt;{'internalFileAttributes'};
}

sub externalFileAttributes {
    shift-&gt;{'externalFileAttributes'};
}

# Convert UNIX permissions into proper value for zip file
# Usable as a function or a method
sub _mapPermissionsFromUnix {
    my $self    = shift;
    my $mode    = shift;
    my $attribs = $mode &lt;&lt; 16;

    # Microsoft Windows Explorer needs this bit set for directories
    if ($mode &amp; DIRECTORY_ATTRIB) {
        $attribs |= 16;
    }

    return $attribs;

    # TODO: map more MS-DOS perms
}

# Convert ZIP permissions into Unix ones
#
# This was taken from Info-ZIP group's portable UnZip
# zipfile-extraction program, version 5.50.
# http://www.info-zip.org/pub/infozip/
#
# See the mapattr() function in unix/unix.c
# See the attribute format constants in unzpriv.h
#
# XXX Note that there's one situation that is not implemented
# yet that depends on the "extra field."
sub _mapPermissionsToUnix {
    my $self = shift;

    my $format  = $self-&gt;{'fileAttributeFormat'};
    my $attribs = $self-&gt;{'externalFileAttributes'};

    my $mode = 0;

    if ($format == FA_AMIGA) {
        $attribs = $attribs &gt;&gt; 17 &amp; 7;                         # Amiga RWE bits
        $mode    = $attribs &lt;&lt; 6 | $attribs &lt;&lt; 3 | $attribs;
        return $mode;
    }

    if ($format == FA_THEOS) {
        $attribs &amp;= 0xF1FFFFFF;
        if (($attribs &amp; 0xF0000000) != 0x40000000) {
            $attribs &amp;= 0x01FFFFFF;    # not a dir, mask all ftype bits
        } else {
            $attribs &amp;= 0x41FFFFFF;    # leave directory bit as set
        }
    }

    if (   $format == FA_UNIX
        || $format == FA_VAX_VMS
        || $format == FA_ACORN
        || $format == FA_ATARI_ST
        || $format == FA_BEOS
        || $format == FA_QDOS
        || $format == FA_TANDEM) {
        $mode = $attribs &gt;&gt; 16;
        return $mode if $mode != 0 or not $self-&gt;localExtraField;

        # warn("local extra field is: ", $self-&gt;localExtraField, "\n");

        # XXX This condition is not implemented
        # I'm just including the comments from the info-zip section for now.

        # Some (non-Info-ZIP) implementations of Zip for Unix and
        # VMS (and probably others ??) leave 0 in the upper 16-bit
        # part of the external_file_attributes field. Instead, they
        # store file permission attributes in some extra field.
        # As a work-around, we search for the presence of one of
        # these extra fields and fall back to the MSDOS compatible
        # part of external_file_attributes if one of the known
        # e.f. types has been detected.
        # Later, we might implement extraction of the permission
        # bits from the VMS extra field. But for now, the work-around
        # should be sufficient to provide "readable" extracted files.
        # (For ASI Unix e.f., an experimental remap from the e.f.
        # mode value IS already provided!)
    }

    # PKWARE's PKZip for Unix marks entries as FA_MSDOS, but stores the
    # Unix attributes in the upper 16 bits of the external attributes
    # field, just like Info-ZIP's Zip for Unix.  We try to use that
    # value, after a check for consistency with the MSDOS attribute
    # bits (see below).
    if ($format == FA_MSDOS) {
        $mode = $attribs &gt;&gt; 16;
    }

    # FA_MSDOS, FA_OS2_HPFS, FA_WINDOWS_NTFS, FA_MACINTOSH, FA_TOPS20
    $attribs = !($attribs &amp; 1) &lt;&lt; 1 | ($attribs &amp; 0x10) &gt;&gt; 4;

    # keep previous $mode setting when its "owner"
    # part appears to be consistent with DOS attribute flags!
    return $mode if ($mode &amp; 0700) == (0400 | $attribs &lt;&lt; 6);
    $mode = 0444 | $attribs &lt;&lt; 6 | $attribs &lt;&lt; 3 | $attribs;
    return $mode;
}

sub unixFileAttributes {
    my $self     = shift;
    my $oldPerms = $self-&gt;_mapPermissionsToUnix;

    my $perms;
    if (@_) {
        $perms = (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{attributes} : $_[0];

        if ($self-&gt;isDirectory) {
            $perms &amp;= ~FILE_ATTRIB;
            $perms |= DIRECTORY_ATTRIB;
        } else {
            $perms &amp;= ~DIRECTORY_ATTRIB;
            $perms |= FILE_ATTRIB;
        }
        $self-&gt;{externalFileAttributes} =
          $self-&gt;_mapPermissionsFromUnix($perms);
    }

    return $oldPerms;
}

sub localExtraField {
    my $self = shift;

    if (@_) {
        my $localExtraField =
          (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{field} : $_[0];
        my ($status, $zip64) =
          $self-&gt;_extractZip64ExtraField($localExtraField, undef, undef);
        if ($status != AZ_OK) {
            return $status;
        }
        elsif ($zip64) {
            return _formatError('invalid extra field (contains zip64 information)');
        }
        else {
            $self-&gt;{localExtraField} = $localExtraField;
            return AZ_OK;
        }
    } else {
        return $self-&gt;{localExtraField};
    }
}

sub cdExtraField {
    my $self = shift;

    if (@_) {
        my $cdExtraField =
          (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{field} : $_[0];
        my ($status, $zip64) =
          $self-&gt;_extractZip64ExtraField($cdExtraField, undef, undef);
        if ($status != AZ_OK) {
            return $status;
        }
        elsif ($zip64) {
            return _formatError('invalid extra field (contains zip64 information)');
        }
        else {
            $self-&gt;{cdExtraField} = $cdExtraField;
            return AZ_OK;
        }
    } else {
        return $self-&gt;{cdExtraField};
    }
}

sub extraFields {
    my $self = shift;
    return $self-&gt;localExtraField() . $self-&gt;cdExtraField();
}

sub fileComment {
    my $self = shift;

    if (@_) {
        $self-&gt;{fileComment} =
          (ref($_[0]) eq 'HASH')
          ? pack('C0a*', $_[0]-&gt;{comment})
          : pack('C0a*', $_[0]);
    } else {
        return $self-&gt;{fileComment};
    }
}

sub hasDataDescriptor {
    my $self = shift;
    if (@_) {
        my $shouldHave = shift;
        if ($shouldHave) {
            $self-&gt;{'bitFlag'} |= GPBF_HAS_DATA_DESCRIPTOR_MASK;
        } else {
            $self-&gt;{'bitFlag'} &amp;= ~GPBF_HAS_DATA_DESCRIPTOR_MASK;
        }
    }
    return $self-&gt;{'bitFlag'} &amp; GPBF_HAS_DATA_DESCRIPTOR_MASK;
}

sub crc32 {
    shift-&gt;{'crc32'};
}

sub crc32String {
    sprintf("%08x", shift-&gt;{'crc32'});
}

sub compressedSize {
    shift-&gt;{'compressedSize'};
}

sub uncompressedSize {
    shift-&gt;{'uncompressedSize'};
}

sub isEncrypted {
    shift-&gt;{'bitFlag'} &amp; GPBF_ENCRYPTED_MASK;
}

sub isTextFile {
    my $self = shift;
    my $bit  = $self-&gt;internalFileAttributes() &amp; IFA_TEXT_FILE_MASK;
    if (@_) {
        my $flag = (ref($_[0]) eq 'HASH') ? shift-&gt;{flag} : shift;
        $self-&gt;{'internalFileAttributes'} &amp;= ~IFA_TEXT_FILE_MASK;
        $self-&gt;{'internalFileAttributes'} |=
          ($flag ? IFA_TEXT_FILE : IFA_BINARY_FILE);
    }
    return $bit == IFA_TEXT_FILE;
}

sub isBinaryFile {
    my $self = shift;
    my $bit  = $self-&gt;internalFileAttributes() &amp; IFA_TEXT_FILE_MASK;
    if (@_) {
        my $flag = shift;
        $self-&gt;{'internalFileAttributes'} &amp;= ~IFA_TEXT_FILE_MASK;
        $self-&gt;{'internalFileAttributes'} |=
          ($flag ? IFA_BINARY_FILE : IFA_TEXT_FILE);
    }
    return $bit == IFA_BINARY_FILE;
}

sub extractToFileNamed {
    my $self = shift;

    # local FS name
    my $name = (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{name} : $_[0];

    # Create directory for regular files as well as for symbolic
    # links
    if ($^O eq 'MSWin32' &amp;&amp; $Archive::Zip::UNICODE) {
        $name = decode_utf8(Win32::GetFullPathName($name));
        mkpath_win32($name);
    } else {
        mkpath(dirname($name));    # croaks on error
    }

    # Check if the file / directory is a symbolic link *and* if
    # the operating system supports these.  Only in that case
    # call method extractToFileHandle with the name of the
    # symbolic link.  If the operating system does not support
    # symbolic links, process the member using the usual
    # extraction routines, which creates a file containing the
    # link target.
    if ($self-&gt;isSymbolicLink() &amp;&amp; OS_SUPPORTS_SYMLINK) {
        return $self-&gt;extractToFileHandle($name);
    } else {
        my ($status, $fh);
        if ($^O eq 'MSWin32' &amp;&amp; $Archive::Zip::UNICODE) {
            Win32::CreateFile($name);
            ($status, $fh) = _newFileHandle(Win32::GetANSIPathName($name), 'w');
        } else {
            ($status, $fh) = _newFileHandle($name, 'w');
        }
        return _ioError("Can't open file $name for write") unless $status;
        $status = $self-&gt;extractToFileHandle($fh);
        $fh-&gt;close();
        chmod($self-&gt;unixFileAttributes(), $name)
          or return _error("Can't chmod() ${name}: $!");
        utime($self-&gt;lastModTime(), $self-&gt;lastModTime(), $name);
        return $status;
    }
}

sub mkpath_win32 {
    my $path = shift;
    use File::Spec;

    my ($volume, @path) = File::Spec-&gt;splitdir($path);
    $path = File::Spec-&gt;catfile($volume, shift @path);
    pop @path;
    while (@path) {
        $path = File::Spec-&gt;catfile($path, shift @path);
        Win32::CreateDirectory($path);
    }
}

sub isSymbolicLink {
    return shift-&gt;{'externalFileAttributes'} == 0xA1FF0000;
}

sub isDirectory {
    return 0;
}

sub externalFileName {
    return undef;
}

# Search the given extra field string for a zip64 extended
# information extra field and "correct" the header fields given
# in the remaining parameters with the information from that
# extra field, if required.  Writes back the extra field string
# sans the zip64 information.  The extra field string and all
# header fields must be passed as lvalues or the undefined value.
#
# This method returns a pair ($status, $zip64) in list context,
# where the latter flag specifies whether a zip64 extended
# information extra field was found.
#
# This method must be called with two header fields for local
# file headers and with four header fields for Central Directory
# headers.
sub _extractZip64ExtraField
{
    my $classOrSelf = shift;

    my $extraField = $_[0];

    my ($zip64Data, $newExtraField) = (undef, '');
    while (length($extraField) &gt;= 4) {
        my ($headerId, $dataSize) = unpack('v v', $extraField);
        if (length($extraField) &lt; 4 + $dataSize) {
            return _formatError('invalid extra field (bad data)');
        }
        elsif ($headerId != 0x0001) {
            $newExtraField .= substr($extraField, 0, 4 + $dataSize);
            $extraField     = substr($extraField, 4 + $dataSize);
        }
        else {
            $zip64Data      = substr($extraField, 4, $dataSize);
            $extraField     = substr($extraField, 4 + $dataSize);
        }
    }
    if (length($extraField) != 0) {
        return _formatError('invalid extra field (bad header ID or data size)');
    }

    my $zip64 = 0;
    if (defined($zip64Data)) {
        return _zip64NotSupported() unless ZIP64_SUPPORTED;

        my $dataLength = length($zip64Data);

        # Try to be tolerant with respect to the fields to be
        # extracted from the zip64 extended information extra
        # field and derive that information from the data itself,
        # if possible.  This works around, for example, incorrect
        # extra fields written by certain versions of package
        # IO::Compress::Zip.  That package provides the disk
        # number start in the extra field without setting the
        # corresponding regular field to 0xffff.  Plus it
        # provides the full set of fields even for the local file
        # header.
        #
        # Field zero is the extra field string which we must keep
        # in @_ for future modification, so account for that.
        my @fields;
        if (@_ == 3 &amp;&amp; $dataLength == 16) {
            @fields = (undef, 0xffffffff, 0xffffffff);
        }
        elsif (@_ == 3 &amp;&amp; $dataLength == 24) {
            push(@_, undef);
            @fields = (undef, 0xffffffff, 0xffffffff, 0xffffffff);
        }
        elsif (@_ == 3 &amp;&amp; $dataLength == 28) {
            push(@_, undef, undef);
            @fields = (undef, 0xffffffff, 0xffffffff, 0xffffffff, 0xffff);
        }
        elsif (@_ == 5 &amp;&amp; $dataLength == 24) {
            @fields = (undef, 0xffffffff, 0xffffffff, 0xffffffff);
        }
        elsif (@_ == 5 &amp;&amp; $dataLength == 28) {
            @fields = (undef, 0xffffffff, 0xffffffff, 0xffffffff, 0xffff);
        }
        else {
            @fields = map { defined $_ ? $_ : 0 } @_;
        }

        my @fieldIndexes  = (0);
        my $fieldFormat   = '';
        my $expDataLength = 0;
        if ($fields[1] == 0xffffffff) {
            push(@fieldIndexes, 1);
            $fieldFormat .= 'Q&lt; ';
            $expDataLength += 8;
        }
        if ($fields[2] == 0xffffffff) {
            push(@fieldIndexes, 2);
            $fieldFormat .= 'Q&lt; ';
            $expDataLength += 8;
        }
        if (@fields &gt; 3 &amp;&amp; $fields[3] == 0xffffffff) {
            push(@fieldIndexes, 3);
            $fieldFormat .= 'Q&lt; ';
            $expDataLength += 8;
        }
        if (@fields &gt; 3 &amp;&amp; $fields[4] == 0xffff) {
            push(@fieldIndexes, 4);
            $fieldFormat .= 'L&lt; ';
            $expDataLength += 4;
        }

        if ($dataLength == $expDataLength) {
            @_[@fieldIndexes] = ($newExtraField, unpack($fieldFormat, $zip64Data));
            $zip64 = 1;
        }
        else {
            return _formatError('invalid zip64 extended information extra field');
        }
    }

    return (AZ_OK, $zip64);
}

# The following are used when copying data
sub _writeOffset {
    shift-&gt;{'writeOffset'};
}

sub _readOffset {
    shift-&gt;{'readOffset'};
}

sub writeLocalHeaderRelativeOffset {
    shift-&gt;{'writeLocalHeaderRelativeOffset'};
}

# Maintained in method Archive::Zip::Archive::writeToFileHandle
sub wasWritten {
    shift-&gt;{'wasWritten'}
}

sub _dataEnded {
    shift-&gt;{'dataEnded'};
}

sub _readDataRemaining {
    shift-&gt;{'readDataRemaining'};
}

sub _inflater {
    shift-&gt;{'inflater'};
}

sub _deflater {
    shift-&gt;{'deflater'};
}

# DOS date/time format
# 0-4 (5) Second divided by 2
# 5-10 (6) Minute (0-59)
# 11-15 (5) Hour (0-23 on a 24-hour clock)
# 16-20 (5) Day of the month (1-31)
# 21-24 (4) Month (1 = January, 2 = February, etc.)
# 25-31 (7) Year offset from 1980 (add 1980 to get actual year)

# Convert DOS date/time format to unix time_t format
# NOT AN OBJECT METHOD!
sub _dosToUnixTime {
    my $dt = shift;
    return time() unless defined($dt);

    my $year = (($dt &gt;&gt; 25) &amp; 0x7f) + 1980;
    my $mon  = (($dt &gt;&gt; 21) &amp; 0x0f) - 1;
    my $mday = (($dt &gt;&gt; 16) &amp; 0x1f);

    my $hour = (($dt &gt;&gt; 11) &amp; 0x1f);
    my $min  = (($dt &gt;&gt; 5) &amp; 0x3f);
    my $sec  = (($dt &lt;&lt; 1) &amp; 0x3e);

    # catch errors
    my $time_t =
      eval { Time::Local::timelocal($sec, $min, $hour, $mday, $mon, $year); };
    return time() if ($@);
    return $time_t;
}

# Note, this is not exactly UTC 1980, it's 1980 + 12 hours and 1
# minute so that nothing timezoney can muck us up.
my $safe_epoch = 31.676060;

# convert a unix time to DOS date/time
# NOT AN OBJECT METHOD!
sub _unixToDosTime {
    my $time_t = shift;
    unless ($time_t) {
        _error("Tried to add member with zero or undef value for time");
        $time_t = $safe_epoch;
    }
    if ($time_t &lt; $safe_epoch) {
        _ioError("Unsupported date before 1980 encountered, moving to 1980");
        $time_t = $safe_epoch;
    }
    my ($sec, $min, $hour, $mday, $mon, $year) = localtime($time_t);
    my $dt = 0;
    $dt += ($sec &gt;&gt; 1);
    $dt += ($min &lt;&lt; 5);
    $dt += ($hour &lt;&lt; 11);
    $dt += ($mday &lt;&lt; 16);
    $dt += (($mon + 1) &lt;&lt; 21);
    $dt += (($year - 80) &lt;&lt; 25);
    return $dt;
}

# Write my local header to a file handle.
# Returns a pair (AZ_OK, $headerSize) on success.
sub _writeLocalFileHeader {
    my $self    = shift;
    my $fh      = shift;
    my $refresh = @_ ? shift : 0;

    my $zip64 = $self-&gt;zip64();
    my $hasDataDescriptor = $self-&gt;hasDataDescriptor();

    my $versionNeededToExtract = $self-&gt;versionNeededToExtract();
    my $crc32;
    my $compressedSize;
    my $uncompressedSize;
    my $localExtraField = $self-&gt;localExtraField();

    if (! $zip64) {
        if ($refresh) {
            $crc32            = $self-&gt;crc32();
            $compressedSize   = $self-&gt;_writeOffset();
            $uncompressedSize = $self-&gt;uncompressedSize();

            # Handle a brain-dead corner case gracefully.
            # Otherwise we a) would always need to write zip64
            # format or b) re-write the complete member data on
            # refresh (which might not always be possible).
            if ($compressedSize &gt; 0xffffffff) {
                return _formatError('compressed size too large for refresh');
            }
        }
        elsif ($hasDataDescriptor) {
            $crc32            = 0;
            $compressedSize   = 0;
            $uncompressedSize = 0;
        }
        else {
            $crc32            = $self-&gt;crc32();
            $compressedSize   = $self-&gt;_writeOffset();
            $uncompressedSize = $self-&gt;uncompressedSize();
        }
    }
    else {
        return _zip64NotSupported() unless ZIP64_SUPPORTED;

        $versionNeededToExtract = 45 if ($versionNeededToExtract &lt; 45);

        my $zip64CompressedSize;
        my $zip64UncompressedSize;
        if ($refresh) {
            $crc32                 = $self-&gt;crc32();
            $compressedSize        = 0xffffffff;
            $uncompressedSize      = 0xffffffff;
            $zip64CompressedSize   = $self-&gt;_writeOffset();
            $zip64UncompressedSize = $self-&gt;uncompressedSize();
        }
        elsif ($hasDataDescriptor) {
            $crc32                 = 0;
            $compressedSize        = 0xffffffff;
            $uncompressedSize      = 0xffffffff;
            $zip64CompressedSize   = 0;
            $zip64UncompressedSize = 0;
        }
        else {
            $crc32                 = $self-&gt;crc32();
            $compressedSize        = 0xffffffff;
            $uncompressedSize      = 0xffffffff;
            $zip64CompressedSize   = $self-&gt;_writeOffset();
            $zip64UncompressedSize = $self-&gt;uncompressedSize();
        }

        $localExtraField .= pack('S&lt; S&lt; Q&lt; Q&lt;',
                                 0x0001, 16,
                                 $zip64UncompressedSize, 
                                 $zip64CompressedSize);
    }

    my $fileNameLength    = length($self-&gt;fileNameAsBytes());
    my $localFieldLength  = length($localExtraField);

    my $signatureData = pack(SIGNATURE_FORMAT, LOCAL_FILE_HEADER_SIGNATURE);
    $self-&gt;_print($fh, $signatureData)
      or return _ioError("writing local header signature");

    my $header =
      pack(LOCAL_FILE_HEADER_FORMAT,
           $versionNeededToExtract,
           $self-&gt;{'bitFlag'},
           $self-&gt;desiredCompressionMethod(),
           $self-&gt;lastModFileDateTime(), 
           $crc32,
           $compressedSize,
           $uncompressedSize,
           $fileNameLength,
           $localFieldLength);
    $self-&gt;_print($fh, $header)
      or return _ioError("writing local header");

    # Write these only if required
    if (! $refresh || $zip64) {
        if ($fileNameLength) {
            $self-&gt;_print($fh, $self-&gt;fileNameAsBytes())
              or return _ioError("writing local header filename");
        }
        if ($localFieldLength) {
            $self-&gt;_print($fh, $localExtraField)
              or return _ioError("writing local extra field");
        }
    }

    return
      (AZ_OK,
       LOCAL_FILE_HEADER_LENGTH +
       SIGNATURE_LENGTH +
       $fileNameLength +
       $localFieldLength);
}

# Re-writes the local file header with new crc32 and compressedSize fields.
# To be called after writing the data stream.
# Assumes that filename and extraField sizes didn't change since last written.
sub _refreshLocalFileHeader {
    my $self = shift;
    my $fh   = shift;

    my $here = $fh-&gt;tell();
    $fh-&gt;seek($self-&gt;writeLocalHeaderRelativeOffset(), IO::Seekable::SEEK_SET)
      or return _ioError("seeking to rewrite local header");

    my ($status, undef) = $self-&gt;_writeLocalFileHeader($fh, 1);
    return $status if $status != AZ_OK;

    $fh-&gt;seek($here, IO::Seekable::SEEK_SET)
      or return _ioError("seeking after rewrite of local header");

    return AZ_OK;
}

# Write central directory file header.
# Returns a pair (AZ_OK, $headerSize) on success.
sub _writeCentralDirectoryFileHeader {
    my $self   = shift;
    my $fh     = shift;
    my $adz64m = shift;         # $archiveDesiredZip64Mode

    # (Re-)Determine whether to write zip64 format.  Assume
    # {'diskNumberStart'} is always zero.
    my $zip64 = $adz64m == ZIP64_HEADERS
             || $self-&gt;desiredZip64Mode() == ZIP64_HEADERS
             || $self-&gt;_writeOffset() &gt; 0xffffffff
             || $self-&gt;uncompressedSize() &gt; 0xffffffff
             || $self-&gt;writeLocalHeaderRelativeOffset() &gt; 0xffffffff;

    $self-&gt;{'zip64'} ||= $zip64;

    my $versionMadeBy             = $self-&gt;versionMadeBy();
    my $versionNeededToExtract    = $self-&gt;versionNeededToExtract();
    my $compressedSize            = $self-&gt;_writeOffset();
    my $uncompressedSize          = $self-&gt;uncompressedSize();
    my $localHeaderRelativeOffset = $self-&gt;writeLocalHeaderRelativeOffset();
    my $cdExtraField              = $self-&gt;cdExtraField();

    if (!$zip64) {
        # no-op
    }
    else {
        return _zip64NotSupported() unless ZIP64_SUPPORTED;

        $versionNeededToExtract = 45 if ($versionNeededToExtract &lt; 45);

        my $extraFieldFormat = '';
        my @extraFieldValues = ();
        my $extraFieldSize   = 0;
        if ($uncompressedSize &gt; 0xffffffff) {
            $extraFieldFormat .= 'Q&lt; ';
            push(@extraFieldValues, $uncompressedSize);
            $extraFieldSize += 8;
            $uncompressedSize = 0xffffffff;
        }
        if ($compressedSize &gt; 0xffffffff) {
            $extraFieldFormat .= 'Q&lt; ';
            push(@extraFieldValues, $compressedSize);
            $extraFieldSize += 8;
            $compressedSize = 0xffffffff;
        }
        # Avoid empty zip64 extended information extra fields
        if (   $localHeaderRelativeOffset &gt; 0xffffffff
            || @extraFieldValues == 0) {
            $extraFieldFormat .= 'Q&lt; ';
            push(@extraFieldValues, $localHeaderRelativeOffset);
            $extraFieldSize += 8;
            $localHeaderRelativeOffset = 0xffffffff;
        }

        $cdExtraField .=
          pack("S&lt; S&lt; $extraFieldFormat",
               0x0001, $extraFieldSize,
               @extraFieldValues);
    }

    my $fileNameLength    = length($self-&gt;fileNameAsBytes());
    my $extraFieldLength  = length($cdExtraField);
    my $fileCommentLength = length($self-&gt;fileComment());

    my $sigData =
      pack(SIGNATURE_FORMAT, CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE);
    $self-&gt;_print($fh, $sigData)
      or return _ioError("writing central directory header signature");

    my $header = pack(
        CENTRAL_DIRECTORY_FILE_HEADER_FORMAT,
        $versionMadeBy,
        $self-&gt;fileAttributeFormat(),
        $versionNeededToExtract,
        $self-&gt;bitFlag(),
        $self-&gt;desiredCompressionMethod(),
        $self-&gt;lastModFileDateTime(),
        $self-&gt;crc32(),            # these three fields should have been updated
        $compressedSize,           # by writing the data stream out
        $uncompressedSize,         #
        $fileNameLength,
        $extraFieldLength,
        $fileCommentLength,
        0,                         # {'diskNumberStart'},
        $self-&gt;internalFileAttributes(),
        $self-&gt;externalFileAttributes(),
        $localHeaderRelativeOffset);

    $self-&gt;_print($fh, $header)
      or return _ioError("writing central directory header");

    if ($fileNameLength) {
        $self-&gt;_print($fh, $self-&gt;fileNameAsBytes())
          or return _ioError("writing central directory header signature");
    }
    if ($extraFieldLength) {
        $self-&gt;_print($fh, $cdExtraField)
          or return _ioError("writing central directory extra field");
    }
    if ($fileCommentLength) {
        $self-&gt;_print($fh, $self-&gt;fileComment())
          or return _ioError("writing central directory file comment");
    }

    # Update object members with information which might have
    # changed while writing this member.  We already did the
    # zip64 flag.  We must not update the extra fields with any
    # zip64 information, since we consider that internal.
    $self-&gt;{'versionNeededToExtract'} = $versionNeededToExtract;
    $self-&gt;{'compressedSize'}         = $self-&gt;_writeOffset();

    return
      (AZ_OK,
       CENTRAL_DIRECTORY_FILE_HEADER_LENGTH +
       SIGNATURE_LENGTH + 
       $fileNameLength +
       $extraFieldLength +
       $fileCommentLength)
}

# This writes a data descriptor to the given file handle.
# Assumes that crc32, writeOffset, and uncompressedSize are
# set correctly (they should be after a write).
# Returns a pair (AZ_OK, $dataDescriptorSize) on success.
# Further, the local file header should have the
# GPBF_HAS_DATA_DESCRIPTOR_MASK bit set.
sub _writeDataDescriptor {
    my $self   = shift;
    my $fh     = shift;

    my $descriptor;
    if (! $self-&gt;zip64()) {
        $descriptor =
          pack(SIGNATURE_FORMAT . DATA_DESCRIPTOR_FORMAT,
               DATA_DESCRIPTOR_SIGNATURE,
               $self-&gt;crc32(),
               $self-&gt;_writeOffset(),   # compressed size
               $self-&gt;uncompressedSize());
    }
    else {
        return _zip64NotSupported() unless ZIP64_SUPPORTED;

        $descriptor =
          pack(SIGNATURE_FORMAT . DATA_DESCRIPTOR_ZIP64_FORMAT,
               DATA_DESCRIPTOR_SIGNATURE,
               $self-&gt;crc32(),
               $self-&gt;_writeOffset(),   # compressed size
               $self-&gt;uncompressedSize());
    }

    $self-&gt;_print($fh, $descriptor)
      or return _ioError("writing data descriptor");

    return (AZ_OK, length($descriptor));
}

sub readChunk {
    my $self = shift;
    my $chunkSize = (ref($_[0]) eq 'HASH') ? $_[0]-&gt;{chunkSize} : $_[0];

    if ($self-&gt;readIsDone()) {
        $self-&gt;endRead();
        my $dummy = '';
        return (\$dummy, AZ_STREAM_END);
    }

    $chunkSize = $Archive::Zip::ChunkSize if not defined($chunkSize);
    $chunkSize = $self-&gt;_readDataRemaining()
      if $chunkSize &gt; $self-&gt;_readDataRemaining();

    my $buffer = '';
    my $outputRef;
    my ($bytesRead, $status) = $self-&gt;_readRawChunk(\$buffer, $chunkSize);
    return (\$buffer, $status) unless $status == AZ_OK;

    $buffer &amp;&amp; $self-&gt;isEncrypted and $buffer = $self-&gt;_decode($buffer);
    $self-&gt;{'readDataRemaining'} -= $bytesRead;
    $self-&gt;{'readOffset'} += $bytesRead;

    if ($self-&gt;compressionMethod() == COMPRESSION_STORED) {
        $self-&gt;{'crc32'} = $self-&gt;computeCRC32($buffer, $self-&gt;{'crc32'});
    }

    ($outputRef, $status) = &amp;{$self-&gt;{'chunkHandler'}}($self, \$buffer);
    $self-&gt;{'writeOffset'} += length($$outputRef);

    $self-&gt;endRead()
      if $self-&gt;readIsDone();

    return ($outputRef, $status);
}

# Read the next raw chunk of my data. Subclasses MUST implement.
#   my ( $bytesRead, $status) = $self-&gt;_readRawChunk( \$buffer, $chunkSize );
sub _readRawChunk {
    my $self = shift;
    return $self-&gt;_subclassResponsibility();
}

# A place holder to catch rewindData errors if someone ignores
# the error code.
sub _noChunk {
    my $self = shift;
    return (\undef, _error("trying to copy chunk when init failed"));
}

# Basically a no-op so that I can have a consistent interface.
# ( $outputRef, $status) = $self-&gt;_copyChunk( \$buffer );
sub _copyChunk {
    my ($self, $dataRef) = @_;
    return ($dataRef, AZ_OK);
}

# ( $outputRef, $status) = $self-&gt;_deflateChunk( \$buffer );
sub _deflateChunk {
    my ($self, $buffer) = @_;
    my ($status) = $self-&gt;_deflater()-&gt;deflate($buffer, my $out);

    if ($self-&gt;_readDataRemaining() == 0) {
        my $extraOutput;
        ($status) = $self-&gt;_deflater()-&gt;flush($extraOutput);
        $out .= $extraOutput;
        $self-&gt;endRead();
        return (\$out, AZ_STREAM_END);
    } elsif ($status == Z_OK) {
        return (\$out, AZ_OK);
    } else {
        $self-&gt;endRead();
        my $retval = _error('deflate error', $status);
        my $dummy = '';
        return (\$dummy, $retval);
    }
}

# ( $outputRef, $status) = $self-&gt;_inflateChunk( \$buffer );
sub _inflateChunk {
    my ($self, $buffer) = @_;
    my ($status) = $self-&gt;_inflater()-&gt;inflate($buffer, my $out);
    my $retval;
    $self-&gt;endRead() unless $status == Z_OK;
    if ($status == Z_OK || $status == Z_STREAM_END) {
        $retval = ($status == Z_STREAM_END) ? AZ_STREAM_END : AZ_OK;
        return (\$out, $retval);
    } else {
        $retval = _error('inflate error', $status);
        my $dummy = '';
        return (\$dummy, $retval);
    }
}

sub rewindData {
    my $self = shift;
    my $status;

    # set to trap init errors
    $self-&gt;{'chunkHandler'} = $self-&gt;can('_noChunk');

    # Work around WinZip bug with 0-length DEFLATED files
    $self-&gt;desiredCompressionMethod(COMPRESSION_STORED)
      if $self-&gt;uncompressedSize() == 0;

    # assume that we're going to read the whole file, and compute the CRC anew.
    $self-&gt;{'crc32'} = 0
      if ($self-&gt;compressionMethod() == COMPRESSION_STORED);

    # These are the only combinations of methods we deal with right now.
    if (    $self-&gt;compressionMethod() == COMPRESSION_STORED
        and $self-&gt;desiredCompressionMethod() == COMPRESSION_DEFLATED) {
        ($self-&gt;{'deflater'}, $status) = Compress::Raw::Zlib::Deflate-&gt;new(
            '-Level'      =&gt; $self-&gt;desiredCompressionLevel(),
            '-WindowBits' =&gt; -MAX_WBITS(),                     # necessary magic
            '-Bufsize'    =&gt; $Archive::Zip::ChunkSize,
            @_
        );    # pass additional options
        return _error('deflateInit error:', $status)
          unless $status == Z_OK;
        $self-&gt;{'chunkHandler'} = $self-&gt;can('_deflateChunk');
    } elsif ($self-&gt;compressionMethod() == COMPRESSION_DEFLATED
        and $self-&gt;desiredCompressionMethod() == COMPRESSION_STORED) {
        ($self-&gt;{'inflater'}, $status) = Compress::Raw::Zlib::Inflate-&gt;new(
            '-WindowBits' =&gt; -MAX_WBITS(),               # necessary magic
            '-Bufsize'    =&gt; $Archive::Zip::ChunkSize,
            @_
        );    # pass additional options
        return _error('inflateInit error:', $status)
          unless $status == Z_OK;
        $self-&gt;{'chunkHandler'} = $self-&gt;can('_inflateChunk');
    } elsif ($self-&gt;compressionMethod() == $self-&gt;desiredCompressionMethod()) {
        $self-&gt;{'chunkHandler'} = $self-&gt;can('_copyChunk');
    } else {
        return _error(
            sprintf(
                "Unsupported compression combination: read %d, write %d",
                $self-&gt;compressionMethod(),
                $self-&gt;desiredCompressionMethod()));
    }

    $self-&gt;{'readDataRemaining'} =
      ($self-&gt;compressionMethod() == COMPRESSION_STORED)
      ? $self-&gt;uncompressedSize()
      : $self-&gt;compressedSize();
    $self-&gt;{'dataEnded'}  = 0;
    $self-&gt;{'readOffset'} = 0;

    return AZ_OK;
}

sub endRead {
    my $self = shift;
    delete $self-&gt;{'inflater'};
    delete $self-&gt;{'deflater'};
    $self-&gt;{'dataEnded'}         = 1;
    $self-&gt;{'readDataRemaining'} = 0;
    return AZ_OK;
}

sub readIsDone {
    my $self = shift;
    return ($self-&gt;_dataEnded() or !$self-&gt;_readDataRemaining());
}

sub contents {
    my $self        = shift;
    my $newContents = shift;

    if (defined($newContents)) {

        # Change our type and ensure that succeeded to avoid
        # endless recursion
        $self-&gt;_become('Archive::Zip::StringMember');
        $self-&gt;_ISA('Archive::Zip::StringMember') or
          return
            wantarray
            ? (undef, $self-&gt;_error('becoming Archive::Zip::StringMember'))
            : undef;

        # Now call the subclass contents method
        my $retval = 
          $self-&gt;contents(pack('C0a*', $newContents)); # in case of Unicode

        return wantarray ? ($retval, AZ_OK) : $retval;
    } else {
        my $oldCompression =
          $self-&gt;desiredCompressionMethod(COMPRESSION_STORED);
        my $status = $self-&gt;rewindData(@_);
        if ($status != AZ_OK) {
            $self-&gt;endRead();
            return wantarray ? (undef, $status) : undef;
        }
        my $retval = '';
        while ($status == AZ_OK) {
            my $ref;
            ($ref, $status) = $self-&gt;readChunk($self-&gt;_readDataRemaining());

            # did we get it in one chunk?
            if (length($$ref) == $self-&gt;uncompressedSize()) {
                $retval = $$ref;
            } else {
                $retval .= $$ref
            }
        }
        $self-&gt;desiredCompressionMethod($oldCompression);
        $self-&gt;endRead();
        $status = AZ_OK if $status == AZ_STREAM_END;
        $retval = undef unless $status == AZ_OK;
        return wantarray ? ($retval, $status) : $retval;
    }
}

sub extractToFileHandle {
    my $self = shift;
    # This can be the link name when "extracting" symbolic links
    my $fhOrName = (ref($_[0]) eq 'HASH') ? shift-&gt;{fileHandle} : shift;
    _binmode($fhOrName) if ref($fhOrName);
    my $oldCompression = $self-&gt;desiredCompressionMethod(COMPRESSION_STORED);
    my $status         = $self-&gt;rewindData(@_);
    $status = $self-&gt;_writeData($fhOrName) if $status == AZ_OK;
    $self-&gt;desiredCompressionMethod($oldCompression);
    $self-&gt;endRead();
    return $status;
}

# write local header and data stream to file handle.
# Returns a pair ($status, $memberSize) if successful.
# Stores the offset to the start of the header in my
# writeLocalHeaderRelativeOffset member.
sub _writeToFileHandle {
    my $self         = shift;
    my $fh           = shift;
    my $fhIsSeekable = shift;
    my $offset       = shift;
    my $adz64m       = shift;   # $archiveDesiredZip64Mode

    return _error("no member name given for $self")
      if $self-&gt;fileName() eq '';

    $self-&gt;{'writeLocalHeaderRelativeOffset'} = $offset;

    # Determine if I need to refresh the header in a second pass
    # later.  If in doubt, I'd rather refresh, since it does not
    # seem to be worth the hassle to save the extra seeks and
    # writes.  In addition, having below condition independent of
    # any specific compression methods helps me piping through
    # members with unknown compression methods unchanged.  See
    # test t/26_bzip2.t for details.
    my $headerFieldsUnknown = $self-&gt;uncompressedSize() &gt; 0;

    # Determine if I need to write a data descriptor
    # I need to do this if I can't refresh the header
    # and I don't know compressed size or crc32 fields.
    my $shouldWriteDataDescriptor =
      ($headerFieldsUnknown and not $fhIsSeekable);

    $self-&gt;hasDataDescriptor(1)
      if ($shouldWriteDataDescriptor);

    # Determine whether to write zip64 format
    my $zip64 = $adz64m == ZIP64_HEADERS
             || $self-&gt;desiredZip64Mode() == ZIP64_HEADERS
             || $self-&gt;uncompressedSize() &gt; 0xffffffff;

    $self-&gt;{'zip64'} ||= $zip64;

    $self-&gt;{'writeOffset'} = 0;

    my $status = $self-&gt;rewindData();
    return $status if $status != AZ_OK;

    my $memberSize;
    ($status, $memberSize) = $self-&gt;_writeLocalFileHeader($fh);
    return $status if $status != AZ_OK;

    $status = $self-&gt;_writeData($fh);
    return $status if $status != AZ_OK;
    $memberSize += $self-&gt;_writeOffset();

    if ($self-&gt;hasDataDescriptor()) {
        my $ddSize;
        ($status, $ddSize) = $self-&gt;_writeDataDescriptor($fh);
        $memberSize += $ddSize;
    } elsif ($headerFieldsUnknown) {
        $status = $self-&gt;_refreshLocalFileHeader($fh);
    }
    return $status if $status != AZ_OK;

    return ($status, $memberSize);
}

# Copy my (possibly compressed) data to given file handle.
# Returns C&lt;AZ_OK&gt; on success
sub _writeData {
    my $self     = shift;
    my $fhOrName = shift;

    if ($self-&gt;isSymbolicLink() &amp;&amp; OS_SUPPORTS_SYMLINK) {
        my $chunkSize = $Archive::Zip::ChunkSize;
        my ($outRef, $status) = $self-&gt;readChunk($chunkSize);
        symlink($$outRef, $fhOrName)
          or return _ioError("creating symbolic link");
    } else {
        return AZ_OK if ($self-&gt;uncompressedSize() == 0);
        my $status;
        my $chunkSize = $Archive::Zip::ChunkSize;
        while ($self-&gt;_readDataRemaining() &gt; 0) {
            my $outRef;
            ($outRef, $status) = $self-&gt;readChunk($chunkSize);
            return $status if ($status != AZ_OK and $status != AZ_STREAM_END);

            if (length($$outRef) &gt; 0) {
                $self-&gt;_print($fhOrName, $$outRef)
                  or return _ioError("write error during copy");
            }

            last if $status == AZ_STREAM_END;
        }
    }
    return AZ_OK;
}

# Return true if I depend on the named file
sub _usesFileNamed {
    return 0;
}

# ##############################################################################
#
# Decrypt section
#
# H.Merijn Brand (Tux) 2011-06-28
#
# ##############################################################################

# This code is derived from the crypt source of unzip-6.0 dated 05 Jan 2007
# Its license states:
#
# --8&lt;---
# Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.

# See the accompanying file LICENSE, version 2005-Feb-10 or later
# (the contents of which are also included in (un)zip.h) for terms of use.
# If, for some reason, all these files are missing, the Info-ZIP license
# also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
#
# crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]

# The main encryption/decryption source code for Info-Zip software was
# originally written in Europe.  To the best of our knowledge, it can
# be freely distributed in both source and object forms from any country,
# including the USA under License Exception TSU of the U.S. Export
# Administration Regulations (section 740.13(e)) of 6 June 2002.

# NOTE on copyright history:
# Previous versions of this source package (up to version 2.8) were
# not copyrighted and put in the public domain.  If you cannot comply
# with the Info-Zip LICENSE, you may want to look for one of those
# public domain versions.
#
# This encryption code is a direct transcription of the algorithm from
# Roger Schlafly, described by Phil Katz in the file appnote.txt.  This
# file (appnote.txt) is distributed with the PKZIP program (even in the
# version without encryption capabilities).
# --&gt;8---

# As of January 2000, US export regulations were amended to allow export
# of free encryption source code from the US.  As of June 2002, these
# regulations were further relaxed to allow export of encryption binaries
# associated with free encryption source code.  The Zip 2.31, UnZip 5.52
# and Wiz 5.02 archives now include full crypto source code.  As of the
# Zip 2.31 release, all official binaries include encryption support; the
# former "zcr" archives ceased to exist.
# (Note that restrictions may still exist in other countries, of course.)

# For now, we just support the decrypt stuff
# All below methods are supposed to be private

# use Data::Peek;

my @keys;
my @crct = do {
    my $xor = 0xedb88320;
    my @crc = (0) x 1024;

    # generate a crc for every 8-bit value
    foreach my $n (0 .. 255) {
        my $c = $n;
        $c = $c &amp; 1 ? $xor ^ ($c &gt;&gt; 1) : $c &gt;&gt; 1 for 1 .. 8;
        $crc[$n] = _revbe($c);
    }

    # generate crc for each value followed by one, two, and three zeros */
    foreach my $n (0 .. 255) {
        my $c = ($crc[($crc[$n] &gt;&gt; 24) ^ 0] ^ ($crc[$n] &lt;&lt; 8)) &amp; 0xffffffff;
        $crc[$_ * 256 + $n] = $c for 1 .. 3;
    }
    map { _revbe($crc[$_]) } 0 .. 1023;
};

sub _crc32 {
    my ($c, $b) = @_;
    return ($crct[($c ^ $b) &amp; 0xff] ^ ($c &gt;&gt; 8));
}    # _crc32

sub _revbe {
    my $w = shift;
    return (($w &gt;&gt; 24) +
          (($w &gt;&gt; 8) &amp; 0xff00) +
          (($w &amp; 0xff00) &lt;&lt; 8) +
          (($w &amp; 0xff) &lt;&lt; 24));
}    # _revbe

sub _update_keys {
    use integer;
    my $c = shift;    # signed int
    $keys[0] = _crc32($keys[0], $c);
    $keys[1] = (($keys[1] + ($keys[0] &amp; 0xff)) * 0x08088405 + 1) &amp; 0xffffffff;
    my $keyshift = $keys[1] &gt;&gt; 24;
    $keys[2] = _crc32($keys[2], $keyshift);
}    # _update_keys

sub _zdecode ($) {
    my $c = shift;
    my $t = ($keys[2] &amp; 0xffff) | 2;
    _update_keys($c ^= ((($t * ($t ^ 1)) &gt;&gt; 8) &amp; 0xff));
    return $c;
}    # _zdecode

sub _decode {
    my $self = shift;
    my $buff = shift;

    $self-&gt;isEncrypted or return $buff;

    my $pass = $self-&gt;password;
    defined $pass or return "";

    @keys = (0x12345678, 0x23456789, 0x34567890);
    _update_keys($_) for unpack "C*", $pass;

    # DDumper { uk =&gt; [ @keys ] };

    my $head = substr $buff, 0, 12, "";
    my @head = map { _zdecode($_) } unpack "C*", $head;
    my $x =
      $self-&gt;{externalFileAttributes}
      ? ($self-&gt;{lastModFileDateTime} &gt;&gt; 8) &amp; 0xff
      : $self-&gt;{crc32} &gt;&gt; 24;
    $head[-1] == $x or return "";    # Password fail

    # Worth checking ...
    $self-&gt;{crc32c} = (unpack LOCAL_FILE_HEADER_FORMAT, pack "C*", @head)[3];

    # DHexDump ($buff);
    $buff = pack "C*" =&gt; map { _zdecode($_) } unpack "C*" =&gt; $buff;

    # DHexDump ($buff);
    return $buff;
}    # _decode

1;
</pre></body></html>