zip.pm

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PM 代码 · 共 2,030 行 · 第 1/4 页

PM
2,030
字号
    return join ( '/', @dirs );}# Return an absolute local name for a zip name.# Assume a directory if zip name has trailing slash.# Takes an optional volume name in FS format (like 'a:').#sub _asLocalName    {	my $name   = shift;    # zip format	my $volume = shift;	$volume = '' unless defined($volume);    # local FS format	my @paths = split ( /\//, $name );	my $filename = pop (@paths);	$filename = '' unless defined($filename);	my $localDirs = @paths?File::Spec->catdir(@paths):'';	my $localName = File::Spec->catpath( $volume, $localDirs, $filename );	$localName = File::Spec->rel2abs($localName) unless $volume;	return $localName;}1;__END__=pod=head1 NAMEArchive::Zip - Provide an interface to ZIP archive files.=head1 SYNOPSIS   # Create a Zip file   use Archive::Zip qw( :ERROR_CODES :CONSTANTS );   my $zip = Archive::Zip->new();      # Add a directory   my $dir_member = $zip->addDirectory( 'dirname/' );      # Add a file from a string with compression   my $string_member = $zip->addString( 'This is a test', 'stringMember.txt' );   $string_member->desiredCompressionMethod( COMPRESSION_DEFLATED );      # Add a file from disk   my $file_member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' );      # Save the Zip file   unless ( $zip->writeToFileNamed('someZip.zip') == AZ_OK ) {       die 'write error';   }      # Read a Zip file   my $somezip = Archive::Zip->new();   unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {       die 'read error';   }      # Change the compression type for a file in the Zip   my $member = $somezip->memberNamed( 'stringMember.txt' );   $member->desiredCompressionMethod( COMPRESSION_STORED );   unless ( $zip->writeToFileNamed( 'someOtherZip.zip' ) == AZ_OK ) {       die 'write error';   }=head1 DESCRIPTIONThe Archive::Zip module allows a Perl program to create, manipulate, read,and write Zip archive files.Zip archives can be created, or you can read from existing zip files.Once created, they can be written to files, streams, or strings. Memberscan be added, removed, extracted, replaced, rearranged, and enumerated.They can also be renamed or have their dates, comments, or other attributesqueried or modified. Their data can be compressed or uncompressed as needed.Members can be created from members in existing Zip files, or from existingdirectories, files, or strings.This module uses the L<Compress::Zlib> library to read and write thecompressed streams inside the files.=head2 File NamingRegardless of what your local file system uses for file naming, names in aZip file are in Unix format (I<forward> slashes (/) separating directorynames, etc.).C<Archive::Zip> tries to be consistent with file naming conventions, and willtranslate back and forth between native and Zip file names.However, it can't guess which format names are in. So two rules control whatkind of file name you must pass various routines:=over 4=item Names of files are in local format.C<File::Spec> and C<File::Basename> are used for various fileoperations. When you're referring to a file on your system, use itsfile naming conventions.=item Names of archive members are in Unix format.This applies to every method that refers to an archive member, orprovides a name for new archive members. The C<extract()> methodsthat can take one or two names will convert from local to zip namesif you call them with a single name.=back=head2 Archive::Zip Object Model=head2 OverviewArchive::Zip::Archive objects are what you ordinarily deal with.These maintain the structure of a zip file, without necessarilyholding data. When a zip is read from a disk file, the (possiblycompressed) data still lives in the file, not in memory. Archivemembers hold information about the individual members, but not(usually) the actual member data. When the zip is written to a(different) file, the member data is compressed or copied as needed.It is possible to make archive members whose data is held in a stringin memory, but this is not done when a zip file is read. Directorymembers don't have any data.=head2 Inheritance  Exporter   Archive::Zip                            Common base class, has defs.       Archive::Zip::Archive               A Zip archive.       Archive::Zip::Member                Abstract superclass for all members.           Archive::Zip::StringMember      Member made from a string           Archive::Zip::FileMember        Member made from an external file               Archive::Zip::ZipFileMember Member that lives in a zip file               Archive::Zip::NewFileMember Member whose data is in a file           Archive::Zip::DirectoryMember   Member that is a directory=head1 EXPORTS=over 4=item :CONSTANTSExports the following constants:FA_MSDOS FA_UNIX GPBF_ENCRYPTED_MASKGPBF_DEFLATING_COMPRESSION_MASK GPBF_HAS_DATA_DESCRIPTOR_MASKCOMPRESSION_STORED COMPRESSION_DEFLATED IFA_TEXT_FILE_MASKIFA_TEXT_FILE IFA_BINARY_FILE COMPRESSION_LEVEL_NONECOMPRESSION_LEVEL_DEFAULT COMPRESSION_LEVEL_FASTESTCOMPRESSION_LEVEL_BEST_COMPRESSION=item :MISC_CONSTANTSExports the following constants (only necessary for extending themodule):FA_AMIGA FA_VAX_VMS FA_VM_CMS FA_ATARI_ST FA_OS2_HPFSFA_MACINTOSH FA_Z_SYSTEM FA_CPM FA_WINDOWS_NTFSGPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASKGPBF_IMPLODING_3_SHANNON_FANO_TREES_MASKGPBF_IS_COMPRESSED_PATCHED_DATA_MASK COMPRESSION_SHRUNKDEFLATING_COMPRESSION_NORMAL DEFLATING_COMPRESSION_MAXIMUMDEFLATING_COMPRESSION_FAST DEFLATING_COMPRESSION_SUPER_FASTCOMPRESSION_REDUCED_1 COMPRESSION_REDUCED_2 COMPRESSION_REDUCED_3COMPRESSION_REDUCED_4 COMPRESSION_IMPLODED COMPRESSION_TOKENIZEDCOMPRESSION_DEFLATED_ENHANCEDCOMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED=item :ERROR_CODESExplained below. Returned from most methods.AZ_OK AZ_STREAM_END AZ_ERROR AZ_FORMAT_ERROR AZ_IO_ERROR=back=head1 ERROR CODESMany of the methods in Archive::Zip return error codes. These are implementedas inline subroutines, using the C<use constant> pragma. They can be importedinto your namespace using the C<:ERROR_CODES> tag:  use Archive::Zip qw( :ERROR_CODES );    ...    unless ( $zip->read( 'myfile.zip' ) == AZ_OK ) {      die "whoops!";  }=over 4=item AZ_OK (0)Everything is fine.=item AZ_STREAM_END (1)The read stream (or central directory) ended normally.=item AZ_ERROR (2)There was some generic kind of error.=item AZ_FORMAT_ERROR (3)There is a format error in a ZIP file being read.=item AZ_IO_ERROR (4)There was an IO error.=back=head2 CompressionArchive::Zip allows each member of a ZIP file to be compressed (using theDeflate algorithm) or uncompressed.Other compression algorithms that some versions of ZIP have been able toproduce are not supported. Each member has two compression methods: theone it's stored as (this is always COMPRESSION_STORED for string and externalfile members), and the one you desire for the member in the zip file.These can be different, of course, so you can make a zip member that is notcompressed out of one that is, and vice versa.You can inquire about the current compression and set the desiredcompression method:  my $member = $zip->memberNamed( 'xyz.txt' );  $member->compressionMethod();    # return current compression    # set to read uncompressed  $member->desiredCompressionMethod( COMPRESSION_STORED );    # set to read compressed  $member->desiredCompressionMethod( COMPRESSION_DEFLATED );There are two different compression methods:=over 4=item COMPRESSION_STOREDFile is stored (no compression)=item COMPRESSION_DEFLATEDFile is Deflated=back=head2 Compression LevelsIf a member's desiredCompressionMethod is COMPRESSION_DEFLATED, youcan choose different compression levels. This choice may affect thespeed of compression and decompression, as well as the size of thecompressed member data.  $member->desiredCompressionLevel( 9 );The levels given can be:=over 4=item 0 or COMPRESSION_LEVEL_NONEThis is the same as saying  $member->desiredCompressionMethod( COMPRESSION_STORED );=item 1 .. 91 gives the best speed and worst compression, and 9 gives thebest compression and worst speed.=item COMPRESSION_LEVEL_FASTESTThis is a synonym for level 1.=item COMPRESSION_LEVEL_BEST_COMPRESSIONThis is a synonym for level 9.=item COMPRESSION_LEVEL_DEFAULTThis gives a good compromise between speed and compression,and is currently equivalent to 6 (this is in the zlib code).This is the level that will be used if not specified.=back=head1 Archive::Zip MethodsThe Archive::Zip class (and its invisible subclass Archive::Zip::Archive)implement generic zip file functionality. Creating a new Archive::Zip objectactually makes an Archive::Zip::Archive object, but you don't have to worryabout this unless you're subclassing.=head2 Constructor=over 4=item new( [$fileName] )Make a new, empty zip archive.    my $zip = Archive::Zip->new();If an additional argument is passed, new() will call read()to read the contents of an archive:    my $zip = Archive::Zip->new( 'xyz.zip' );If a filename argument is passed and the read fails for anyreason, new will return undef. For this reason, it may bebetter to call read separately.=back=head2 Zip Archive Utility MethodsThese Archive::Zip methods may be called as functions or as objectmethods. Do not call them as class methods:    $zip = Archive::Zip->new();    $crc = Archive::Zip::computeCRC32( 'ghijkl' );    # OK    $crc = $zip->computeCRC32( 'ghijkl' );            # also OK    $crc = Archive::Zip->computeCRC32( 'ghijkl' );    # NOT OK=over 4=item Archive::Zip::computeCRC32( $string [, $crc] )This is a utility function that uses the Compress::Zlib CRCroutine to compute a CRC-32. You can get the CRC of a string:    $crc = Archive::Zip::computeCRC32( $string );Or you can compute the running CRC:    $crc = 0;    $crc = Archive::Zip::computeCRC32( 'abcdef', $crc );    $crc = Archive::Zip::computeCRC32( 'ghijkl', $crc );=item Archive::Zip::setChunkSize( $number )Report or change chunk size used for reading and writing.This can make big differences in dealing with large files.Currently, this defaults to 32K. This also changes the chunksize used for Compress::Zlib. You must call setChunkSize()before reading or writing. This is not exportable, so youmust call it like:    Archive::Zip::setChunkSize( 4096 );or as a method on a zip (though this is a global setting).Returns old chunk size.=item Archive::Zip::chunkSize()Returns the current chunk size:    my $chunkSize = Archive::Zip::chunkSize();=item Archive::Zip::setErrorHandler( \&subroutine )Change the subroutine called with error strings. Thisdefaults to \&Carp::carp, but you may want to change it toget the error strings. This is not exportable, so you mustcall it like:    Archive::Zip::setErrorHandler( \&myErrorHandler );If myErrorHandler is undef, resets handler to default.Returns old error handler. Note that if you call Carp::carpor a similar routine or if you're chaining to the defaulterror handler from your error handler, you may want toincrement the number of caller levels that are skipped (donot just set it to a number):    $Carp::CarpLevel++;=item Archive::Zip::tempFile( [$tmpdir] )Create a uniquely named temp file. It will be returned openfor read/write. If C<$tmpdir> is given, it is used as thename of a directory to create the file in. If not given,creates the file using C<File::Spec::tmpdir()>. Generally, you canoverride this choice using the    $ENV{TMPDIR}environment variable. But see the L<File::Spec|File::Spec>documentation for your system. Note that on many systems, if you'rerunning in taint mode, then you must make sure that C<$ENV{TMPDIR}> isuntainted for it to be used.Will I<NOT> create C<$tmpdir> if it doesn't exist (this is a changefrom prior versions!). Returns file handle and name:    my ($fh, $name) = Archive::Zip::tempFile();    my ($fh, $name) = Archive::Zip::tempFile('myTempDir');    my $fh = Archive::Zip::tempFile();  # if you don't need the name=back=head2 Zip Archive Accessors=over 4=item members()Return a copy of the members array    my @members = $zip->members();=item numberOfMembers()Return the number of members I have=item memberNames()Return a list of the (internal) file names of the zip members=item memberNamed( $string )Return ref to member whose filename equals given filename orundef. C<$string> must be in Zip (Unix) filename format.=item membersMatching( $regex )Return array of members whose filenames match given regularexpression in list context. Returns number of matchingmembers in scalar context.    my @textFileMembers = $zip->membersMatching( '.*\.txt' );    # or    my $numberOfTextFiles = $zip->membersMatching( '.*\.txt' );=item diskNumber()Return the disk that I start on. Not used for writing zips,but might be interesting if you read a zip in. This should be0, as Archive::Zip does not handle multi-volume archives.=item diskNumberWithStartOfCentralDirectory()Return the disk number that holds the beginning of thecentral directory. Not used for writing zips, but might beinteresting if you read a zip in. This should be 0, asArchive::Zip does not handle multi-volume archives.=item numberOfCentralDirectoriesOnThisDisk()Return the number of CD structures in the zipfile last read in.Not used for writing zips, but might be interesting if you read a zipin.=item numberOfCentralDirectories()Return the number of CD structures in the zipfile last read in.Not used for writing zips, but might be interesting if you read a zipin.=item centralDirectorySize()Returns central directory size, as read from an external zipfile. Not used for writing zips, but might be interesting ifyou read a zip in.=item centralDirectoryOffsetWRTStartingDiskNumber()Returns the offset into the zip file where the CD begins. Notused for writing zips, but might be interesting if you read azip in.=item zipfileComment( [$string] )Get or set the zipfile comment. Returns the old comment.    print $zip->zipfileComment();    $zip->zipfileComment( 'New Comment' );=item eocdOffset()Returns the (unexpected) number of bytes between where theEOCD was found and where it expected to be. This is normally0, but would be positive if something (a virus, perhaps) hadadded bytes somewhere before the EOCD. Not used for writingzips, but might be interesting if you read a zip in. Here isan example of how you can diagnose this:  my $zip = Archive::Zip->new('somefile.zip');  if ($zip->eocdOffset())  {    warn "A virus has added ", $zip->eocdOffset, " bytes of garbage\n";  }The C<eocdOffset()> is used to adjust the starting position of memberheaders, if necessary.=item fileName()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?