📄 member.pm
字号:
package Archive::Zip::Member;# A generic membet of an archiveuse strict;use vars qw( $VERSION @ISA );BEGIN { $VERSION = '1.23'; @ISA = qw( Archive::Zip );}use Archive::Zip qw( :CONSTANTS :MISC_CONSTANTS :ERROR_CODES :PKZIP_CONSTANTS :UTILITY_METHODS);use Time::Local ();use Compress::Zlib qw( Z_OK Z_STREAM_END MAX_WBITS );use File::Path;use File::Basename;use constant ZIPFILEMEMBERCLASS => 'Archive::Zip::ZipFileMember';use constant NEWFILEMEMBERCLASS => 'Archive::Zip::NewFileMember';use constant STRINGMEMBERCLASS => 'Archive::Zip::StringMember';use constant DIRECTORYMEMBERCLASS => 'Archive::Zip::DirectoryMember';# Unix perms for default creation of files/dirs.use constant DEFAULT_DIRECTORY_PERMISSIONS => 040755;use constant DEFAULT_FILE_PERMISSIONS => 0100666;use constant DIRECTORY_ATTRIB => 040000;use constant FILE_ATTRIB => 0100000;# 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 = $class->ZIPFILEMEMBERCLASS->_newFromZipFile(@_); return $self;}sub newFromString { my $class = shift; my $self = $class->STRINGMEMBERCLASS->_newFromString(@_); return $self;}sub newFromFile { my $class = shift; my $self = $class->NEWFILEMEMBERCLASS->_newFromFileNamed(@_); return $self;}sub newDirectoryNamed { my $class = shift; my $self = $class->DIRECTORYMEMBERCLASS->_newNamed(@_); return $self;}sub new { my $class = shift; my $self = { 'lastModFileDateTime' => 0, 'fileAttributeFormat' => FA_UNIX, 'versionMadeBy' => 20, 'versionNeededToExtract' => 20, 'bitFlag' => 0, 'compressionMethod' => COMPRESSION_STORED, 'desiredCompressionMethod' => COMPRESSION_STORED, 'desiredCompressionLevel' => COMPRESSION_LEVEL_NONE, 'internalFileAttributes' => 0, 'externalFileAttributes' => 0, # set later 'fileName' => '', 'cdExtraField' => '', 'localExtraField' => '', 'fileComment' => '', 'crc32' => 0, 'compressedSize' => 0, 'uncompressedSize' => 0, @_ }; bless( $self, $class ); $self->unixFileAttributes( $self->DEFAULT_FILE_PERMISSIONS ); return $self;}sub _becomeDirectoryIfNecessary { my $self = shift; $self->_become(DIRECTORYMEMBERCLASS) if $self->isDirectory(); return $self;}# Morph into given class (do whatever cleanup I need to do)sub _become { return bless( $_[0], $_[1] );}sub versionMadeBy { shift->{'versionMadeBy'};}sub fileAttributeFormat { ( $#_ > 0 ) ? ( $_[0]->{'fileAttributeFormat'} = $_[1] ) : $_[0]->{'fileAttributeFormat'};}sub versionNeededToExtract { shift->{'versionNeededToExtract'};}sub bitFlag { shift->{'bitFlag'};}sub compressionMethod { shift->{'compressionMethod'};}sub desiredCompressionMethod { my $self = shift; my $newDesiredCompressionMethod = shift; my $oldDesiredCompressionMethod = $self->{'desiredCompressionMethod'}; if ( defined($newDesiredCompressionMethod) ) { $self->{'desiredCompressionMethod'} = $newDesiredCompressionMethod; if ( $newDesiredCompressionMethod == COMPRESSION_STORED ) { $self->{'desiredCompressionLevel'} = 0; $self->{'bitFlag'} &= ~GPBF_HAS_DATA_DESCRIPTOR_MASK; } elsif ( $oldDesiredCompressionMethod == COMPRESSION_STORED ) { $self->{'desiredCompressionLevel'} = COMPRESSION_LEVEL_DEFAULT; } } return $oldDesiredCompressionMethod;}sub desiredCompressionLevel { my $self = shift; my $newDesiredCompressionLevel = shift; my $oldDesiredCompressionLevel = $self->{'desiredCompressionLevel'}; if ( defined($newDesiredCompressionLevel) ) { $self->{'desiredCompressionLevel'} = $newDesiredCompressionLevel; $self->{'desiredCompressionMethod'} = ( $newDesiredCompressionLevel ? COMPRESSION_DEFLATED : COMPRESSION_STORED ); } return $oldDesiredCompressionLevel;}sub fileName { my $self = shift; my $newName = shift; if ($newName) { $newName =~ s{[\\/]+}{/}g; # deal with dos/windoze problems $self->{'fileName'} = $newName; } return $self->{'fileName'};}sub lastModFileDateTime { my $modTime = shift->{'lastModFileDateTime'}; $modTime =~ m/^(\d+)$/; # untaint return $1;}sub lastModTime { my $self = shift; return _dosToUnixTime( $self->lastModFileDateTime() );}sub setLastModFileDateTimeFromUnix { my $self = shift; my $time_t = shift; $self->{'lastModFileDateTime'} = _unixToDosTime($time_t);}sub internalFileAttributes { shift->{'internalFileAttributes'};}sub externalFileAttributes { shift->{'externalFileAttributes'};}# Convert UNIX permissions into proper value for zip file# NOT A METHOD!sub _mapPermissionsFromUnix { my $perms = shift; return $perms << 16; # TODO: map MS-DOS perms too (RHSA?)}# 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 isn't implemented# yet that depends on the "extra field."sub _mapPermissionsToUnix { my $self = shift; my $format = $self->{'fileAttributeFormat'}; my $attribs = $self->{'externalFileAttributes'}; my $mode = 0; if ( $format == FA_AMIGA ) { $attribs = $attribs >> 17 & 7; # Amiga RWE bits $mode = $attribs << 6 | $attribs << 3 | $attribs; return $mode; } if ( $format == FA_THEOS ) { $attribs &= 0xF1FFFFFF; if ( ( $attribs & 0xF0000000 ) != 0x40000000 ) { $attribs &= 0x01FFFFFF; # not a dir, mask all ftype bits } else { $attribs &= 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 >> 16; return $mode if $mode != 0 or not $self->localExtraField; # warn("local extra field is: ", $self->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 >> 16; } # FA_MSDOS, FA_OS2_HPFS, FA_WINDOWS_NTFS, FA_MACINTOSH, FA_TOPS20 $attribs = !( $attribs & 1 ) << 1 | ( $attribs & 0x10 ) >> 4; # keep previous $mode setting when its "owner" # part appears to be consistent with DOS attribute flags! return $mode if ( $mode & 0700 ) == ( 0400 | $attribs << 6 ); $mode = 0444 | $attribs << 6 | $attribs << 3 | $attribs; return $mode;}sub unixFileAttributes { my $self = shift; my $oldPerms = $self->_mapPermissionsToUnix(); if (@_) { my $perms = shift; if ( $self->isDirectory() ) { $perms &= ~FILE_ATTRIB; $perms |= DIRECTORY_ATTRIB; } else { $perms &= ~DIRECTORY_ATTRIB; $perms |= FILE_ATTRIB; } $self->{'externalFileAttributes'} = _mapPermissionsFromUnix($perms); } return $oldPerms;}sub localExtraField { ( $#_ > 0 ) ? ( $_[0]->{'localExtraField'} = $_[1] ) : $_[0]->{'localExtraField'};}sub cdExtraField { ( $#_ > 0 ) ? ( $_[0]->{'cdExtraField'} = $_[1] ) : $_[0]->{'cdExtraField'};}sub extraFields { my $self = shift; return $self->localExtraField() . $self->cdExtraField();}sub fileComment { ( $#_ > 0 ) ? ( $_[0]->{'fileComment'} = pack( 'C0a*', $_[1] ) ) : $_[0]->{'fileComment'};}sub hasDataDescriptor { my $self = shift; if (@_) { my $shouldHave = shift; if ($shouldHave) { $self->{'bitFlag'} |= GPBF_HAS_DATA_DESCRIPTOR_MASK; } else { $self->{'bitFlag'} &= ~GPBF_HAS_DATA_DESCRIPTOR_MASK; } } return $self->{'bitFlag'} & GPBF_HAS_DATA_DESCRIPTOR_MASK;}sub crc32 { shift->{'crc32'};}sub crc32String { sprintf( "%08x", shift->{'crc32'} );}sub compressedSize { shift->{'compressedSize'};}sub uncompressedSize { shift->{'uncompressedSize'};}sub isEncrypted { shift->bitFlag() & GPBF_ENCRYPTED_MASK;}sub isTextFile { my $self = shift; my $bit = $self->internalFileAttributes() & IFA_TEXT_FILE_MASK; if (@_) { my $flag = shift; $self->{'internalFileAttributes'} &= ~IFA_TEXT_FILE_MASK; $self->{'internalFileAttributes'} |= ( $flag ? IFA_TEXT_FILE: IFA_BINARY_FILE ); } return $bit == IFA_TEXT_FILE;}sub isBinaryFile { my $self = shift; my $bit = $self->internalFileAttributes() & IFA_TEXT_FILE_MASK; if (@_) { my $flag = shift; $self->{'internalFileAttributes'} &= ~IFA_TEXT_FILE_MASK; $self->{'internalFileAttributes'} |= ( $flag ? IFA_BINARY_FILE: IFA_TEXT_FILE ); } return $bit == IFA_BINARY_FILE;}sub extractToFileNamed { my $self = shift; my $name = shift; # local FS name return _error("encryption unsupported") if $self->isEncrypted(); mkpath( dirname($name) ); # croaks on error my ( $status, $fh ) = _newFileHandle( $name, 'w' ); return _ioError("Can't open file $name for write") unless $status; my $retval = $self->extractToFileHandle($fh); $fh->close(); utime( $self->lastModTime(), $self->lastModTime(), $name ); return $retval;}sub isDirectory { return 0;}sub externalFileName { return undef;}# The following are used when copying datasub _writeOffset { shift->{'writeOffset'};}sub _readOffset { shift->{'readOffset'};}sub writeLocalHeaderRelativeOffset { shift->{'writeLocalHeaderRelativeOffset'};}sub wasWritten { shift->{'wasWritten'} }sub _dataEnded { shift->{'dataEnded'};}sub _readDataRemaining { shift->{'readDataRemaining'};}sub _inflater { shift->{'inflater'};}sub _deflater { shift->{'deflater'};}# Return the total size of my local headersub _localHeaderSize { my $self = shift; return SIGNATURE_LENGTH + LOCAL_FILE_HEADER_LENGTH + length( $self->fileName() ) + length( $self->localExtraField() );}# Return the total size of my CD headersub _centralDirectoryHeaderSize { my $self = shift; return SIGNATURE_LENGTH + CENTRAL_DIRECTORY_FILE_HEADER_LENGTH + length( $self->fileName() ) + length( $self->cdExtraField() ) + length( $self->fileComment() );}# 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 >> 25 ) & 0x7f ) + 80; my $mon = ( ( $dt >> 21 ) & 0x0f ) - 1; my $mday = ( ( $dt >> 16 ) & 0x1f ); my $hour = ( ( $dt >> 11 ) & 0x1f ); my $min = ( ( $dt >> 5 ) & 0x3f ); my $sec = ( ( $dt << 1 ) & 0x3e ); # catch errors my $time_t = eval { Time::Local::timelocal( $sec, $min, $hour, $mday, $mon, $year ); }; return time() if ($@); return $time_t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -