1Archive::Zip(3)       User Contributed Perl Documentation      Archive::Zip(3)
2
3
4

NAME

6       Archive::Zip - Provide an interface to ZIP archive files.
7

SYNOPSIS

9          # Create a Zip file
10          use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
11          my $zip = Archive::Zip->new();
12
13          # Add a directory
14          my $dir_member = $zip->addDirectory( 'dirname/' );
15
16          # Add a file from a string with compression
17          my $string_member = $zip->addString( 'This is a test', 'stringMember.txt' );
18          $string_member->desiredCompressionMethod( COMPRESSION_DEFLATED );
19
20          # Add a file from disk
21          my $file_member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' );
22
23          # Save the Zip file
24          unless ( $zip->writeToFileNamed('someZip.zip') == AZ_OK ) {
25              die 'write error';
26          }
27
28          # Read a Zip file
29          my $somezip = Archive::Zip->new();
30          unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {
31              die 'read error';
32          }
33
34          # Change the compression type for a file in the Zip
35          my $member = $somezip->memberNamed( 'stringMember.txt' );
36          $member->desiredCompressionMethod( COMPRESSION_STORED );
37          unless ( $zip->writeToFileNamed( 'someOtherZip.zip' ) == AZ_OK ) {
38              die 'write error';
39          }
40

DESCRIPTION

42       The Archive::Zip module allows a Perl program to create, manipulate,
43       read, and write Zip archive files.
44
45       Zip archives can be created, or you can read from existing zip files.
46
47       Once created, they can be written to files, streams, or strings.
48       Members can be added, removed, extracted, replaced, rearranged, and
49       enumerated.  They can also be renamed or have their dates, comments, or
50       other attributes queried or modified. Their data can be compressed or
51       uncompressed as needed.
52
53       Members can be created from members in existing Zip files, or from
54       existing directories, files, or strings.
55
56       This module uses the Compress::Raw::Zlib library to read and write the
57       compressed streams inside the files.
58
59       One can use Archive::Zip::MemberRead to read the zip file archive
60       members as if they were files.
61
62   File Naming
63       Regardless of what your local file system uses for file naming, names
64       in a Zip file are in Unix format (forward slashes (/) separating
65       directory names, etc.).
66
67       "Archive::Zip" tries to be consistent with file naming conventions, and
68       will translate back and forth between native and Zip file names.
69
70       However, it can't guess which format names are in. So two rules control
71       what kind of file name you must pass various routines:
72
73       Names of files are in local format.
74           "File::Spec" and "File::Basename" are used for various file
75           operations. When you're referring to a file on your system, use its
76           file naming conventions.
77
78       Names of archive members are in Unix format.
79           This applies to every method that refers to an archive member, or
80           provides a name for new archive members. The "extract()" methods
81           that can take one or two names will convert from local to zip names
82           if you call them with a single name.
83
84   Archive::Zip Object Model
85   Overview
86       Archive::Zip::Archive objects are what you ordinarily deal with.  These
87       maintain the structure of a zip file, without necessarily holding data.
88       When a zip is read from a disk file, the (possibly compressed) data
89       still lives in the file, not in memory. Archive members hold
90       information about the individual members, but not (usually) the actual
91       member data. When the zip is written to a (different) file, the member
92       data is compressed or copied as needed.  It is possible to make archive
93       members whose data is held in a string in memory, but this is not done
94       when a zip file is read. Directory members don't have any data.
95
96   Inheritance
97         Exporter
98          Archive::Zip                            Common base class, has defs.
99              Archive::Zip::Archive               A Zip archive.
100              Archive::Zip::Member                Abstract superclass for all members.
101                  Archive::Zip::StringMember      Member made from a string
102                  Archive::Zip::FileMember        Member made from an external file
103                      Archive::Zip::ZipFileMember Member that lives in a zip file
104                      Archive::Zip::NewFileMember Member whose data is in a file
105                  Archive::Zip::DirectoryMember   Member that is a directory
106

EXPORTS

108       :CONSTANTS
109           Exports the following constants:
110
111           FA_MSDOS FA_UNIX GPBF_ENCRYPTED_MASK
112           GPBF_DEFLATING_COMPRESSION_MASK GPBF_HAS_DATA_DESCRIPTOR_MASK
113           COMPRESSION_STORED COMPRESSION_DEFLATED IFA_TEXT_FILE_MASK
114           IFA_TEXT_FILE IFA_BINARY_FILE COMPRESSION_LEVEL_NONE
115           COMPRESSION_LEVEL_DEFAULT COMPRESSION_LEVEL_FASTEST
116           COMPRESSION_LEVEL_BEST_COMPRESSION
117
118       :MISC_CONSTANTS
119           Exports the following constants (only necessary for extending the
120           module):
121
122           FA_AMIGA FA_VAX_VMS FA_VM_CMS FA_ATARI_ST FA_OS2_HPFS FA_MACINTOSH
123           FA_Z_SYSTEM FA_CPM FA_WINDOWS_NTFS
124           GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK
125           GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK
126           GPBF_IS_COMPRESSED_PATCHED_DATA_MASK COMPRESSION_SHRUNK
127           DEFLATING_COMPRESSION_NORMAL DEFLATING_COMPRESSION_MAXIMUM
128           DEFLATING_COMPRESSION_FAST DEFLATING_COMPRESSION_SUPER_FAST
129           COMPRESSION_REDUCED_1 COMPRESSION_REDUCED_2 COMPRESSION_REDUCED_3
130           COMPRESSION_REDUCED_4 COMPRESSION_IMPLODED COMPRESSION_TOKENIZED
131           COMPRESSION_DEFLATED_ENHANCED
132           COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED
133
134       :ERROR_CODES
135           Explained below. Returned from most methods.
136
137           AZ_OK AZ_STREAM_END AZ_ERROR AZ_FORMAT_ERROR AZ_IO_ERROR
138

ERROR CODES

140       Many of the methods in Archive::Zip return error codes. These are
141       implemented as inline subroutines, using the "use constant" pragma.
142       They can be imported into your namespace using the ":ERROR_CODES" tag:
143
144         use Archive::Zip qw( :ERROR_CODES );
145
146         ...
147
148         unless ( $zip->read( 'myfile.zip' ) == AZ_OK ) {
149             die "whoops!";
150         }
151
152       AZ_OK s0(0)
153           Everything is fine.
154
155       AZ_STREAM_END s0(1)
156           The read stream (or central directory) ended normally.
157
158       AZ_ERROR s0(2)
159           There was some generic kind of error.
160
161       AZ_FORMAT_ERROR s0(3)
162           There is a format error in a ZIP file being read.
163
164       AZ_IO_ERROR s0(4)
165           There was an IO error.
166
167   Compression
168       Archive::Zip allows each member of a ZIP file to be compressed (using
169       the Deflate algorithm) or uncompressed.
170
171       Other compression algorithms that some versions of ZIP have been able
172       to produce are not supported. Each member has two compression methods:
173       the one it's stored as (this is always COMPRESSION_STORED for string
174       and external file members), and the one you desire for the member in
175       the zip file.
176
177       These can be different, of course, so you can make a zip member that is
178       not compressed out of one that is, and vice versa.
179
180       You can inquire about the current compression and set the desired
181       compression method:
182
183         my $member = $zip->memberNamed( 'xyz.txt' );
184         $member->compressionMethod();    # return current compression
185
186         # set to read uncompressed
187         $member->desiredCompressionMethod( COMPRESSION_STORED );
188
189         # set to read compressed
190         $member->desiredCompressionMethod( COMPRESSION_DEFLATED );
191
192       There are two different compression methods:
193
194       COMPRESSION_STORED
195           File is stored (no compression)
196
197       COMPRESSION_DEFLATED
198           File is Deflated
199
200   Compression Levels
201       If a member's desiredCompressionMethod is COMPRESSION_DEFLATED, you can
202       choose different compression levels. This choice may affect the speed
203       of compression and decompression, as well as the size of the compressed
204       member data.
205
206         $member->desiredCompressionLevel( 9 );
207
208       The levels given can be:
209
210       0 or COMPRESSION_LEVEL_NONE
211           This is the same as saying
212
213             $member->desiredCompressionMethod( COMPRESSION_STORED );
214
215       1 .. 9
216           1 gives the best speed and worst compression, and 9 gives the best
217           compression and worst speed.
218
219       COMPRESSION_LEVEL_FASTEST
220           This is a synonym for level 1.
221
222       COMPRESSION_LEVEL_BEST_COMPRESSION
223           This is a synonym for level 9.
224
225       COMPRESSION_LEVEL_DEFAULT
226           This gives a good compromise between speed and compression, and is
227           currently equivalent to 6 (this is in the zlib code).  This is the
228           level that will be used if not specified.
229

Archive::Zip Methods

231       The Archive::Zip class (and its invisible subclass
232       Archive::Zip::Archive) implement generic zip file functionality.
233       Creating a new Archive::Zip object actually makes an
234       Archive::Zip::Archive object, but you don't have to worry about this
235       unless you're subclassing.
236
237   Constructor
238       new( [$fileName] )
239           Make a new, empty zip archive.
240
241               my $zip = Archive::Zip->new();
242
243           If an additional argument is passed, new() will call read() to read
244           the contents of an archive:
245
246               my $zip = Archive::Zip->new( 'xyz.zip' );
247
248           If a filename argument is passed and the read fails for any reason,
249           new will return undef. For this reason, it may be better to call
250           read separately.
251
252   Zip Archive Utility Methods
253       These Archive::Zip methods may be called as functions or as object
254       methods. Do not call them as class methods:
255
256           $zip = Archive::Zip->new();
257           $crc = Archive::Zip::computeCRC32( 'ghijkl' );    # OK
258           $crc = $zip->computeCRC32( 'ghijkl' );            # also OK
259           $crc = Archive::Zip->computeCRC32( 'ghijkl' );    # NOT OK
260
261       Archive::Zip::computeCRC32( $string [, $crc] )
262           This is a utility function that uses the Compress::Raw::Zlib CRC
263           routine to compute a CRC-32. You can get the CRC of a string:
264
265               $crc = Archive::Zip::computeCRC32( $string );
266
267           Or you can compute the running CRC:
268
269               $crc = 0;
270               $crc = Archive::Zip::computeCRC32( 'abcdef', $crc );
271               $crc = Archive::Zip::computeCRC32( 'ghijkl', $crc );
272
273       Archive::Zip::setChunkSize( $number )
274           Report or change chunk size used for reading and writing.  This can
275           make big differences in dealing with large files.  Currently, this
276           defaults to 32K. This also changes the chunk size used for
277           Compress::Raw::Zlib. You must call setChunkSize() before reading or
278           writing. This is not exportable, so you must call it like:
279
280               Archive::Zip::setChunkSize( 4096 );
281
282           or as a method on a zip (though this is a global setting).  Returns
283           old chunk size.
284
285       Archive::Zip::chunkSize()
286           Returns the current chunk size:
287
288               my $chunkSize = Archive::Zip::chunkSize();
289
290       Archive::Zip::setErrorHandler( \&subroutine )
291           Change the subroutine called with error strings. This defaults to
292           \&Carp::carp, but you may want to change it to get the error
293           strings. This is not exportable, so you must call it like:
294
295               Archive::Zip::setErrorHandler( \&myErrorHandler );
296
297           If myErrorHandler is undef, resets handler to default.  Returns old
298           error handler. Note that if you call Carp::carp or a similar
299           routine or if you're chaining to the default error handler from
300           your error handler, you may want to increment the number of caller
301           levels that are skipped (do not just set it to a number):
302
303               $Carp::CarpLevel++;
304
305       Archive::Zip::tempFile( [$tmpdir] )
306           Create a uniquely named temp file. It will be returned open for
307           read/write. If $tmpdir is given, it is used as the name of a
308           directory to create the file in. If not given, creates the file
309           using "File::Spec::tmpdir()". Generally, you can override this
310           choice using the
311
312               $ENV{TMPDIR}
313
314           environment variable. But see the File::Spec documentation for your
315           system. Note that on many systems, if you're running in taint mode,
316           then you must make sure that $ENV{TMPDIR} is untainted for it to be
317           used.  Will NOT create $tmpdir if it doesn't exist (this is a
318           change from prior versions!). Returns file handle and name:
319
320               my ($fh, $name) = Archive::Zip::tempFile();
321               my ($fh, $name) = Archive::Zip::tempFile('myTempDir');
322               my $fh = Archive::Zip::tempFile();  # if you don't need the name
323
324   Zip Archive Accessors
325       members()
326           Return a copy of the members array
327
328               my @members = $zip->members();
329
330       numberOfMembers()
331           Return the number of members I have
332
333       memberNames()
334           Return a list of the (internal) file names of the zip members
335
336       memberNamed( $string )
337           Return ref to member whose filename equals given filename or undef.
338           $string must be in Zip (Unix) filename format.
339
340       membersMatching( $regex )
341           Return array of members whose filenames match given regular
342           expression in list context. Returns number of matching members in
343           scalar context.
344
345               my @textFileMembers = $zip->membersMatching( '.*\.txt' );
346               # or
347               my $numberOfTextFiles = $zip->membersMatching( '.*\.txt' );
348
349       diskNumber()
350           Return the disk that I start on. Not used for writing zips, but
351           might be interesting if you read a zip in. This should be 0, as
352           Archive::Zip does not handle multi-volume archives.
353
354       diskNumberWithStartOfCentralDirectory()
355           Return the disk number that holds the beginning of the central
356           directory. Not used for writing zips, but might be interesting if
357           you read a zip in. This should be 0, as Archive::Zip does not
358           handle multi-volume archives.
359
360       numberOfCentralDirectoriesOnThisDisk()
361           Return the number of CD structures in the zipfile last read in.
362           Not used for writing zips, but might be interesting if you read a
363           zip in.
364
365       numberOfCentralDirectories()
366           Return the number of CD structures in the zipfile last read in.
367           Not used for writing zips, but might be interesting if you read a
368           zip in.
369
370       centralDirectorySize()
371           Returns central directory size, as read from an external zip file.
372           Not used for writing zips, but might be interesting if you read a
373           zip in.
374
375       centralDirectoryOffsetWRTStartingDiskNumber()
376           Returns the offset into the zip file where the CD begins. Not used
377           for writing zips, but might be interesting if you read a zip in.
378
379       zipfileComment( [$string] )
380           Get or set the zipfile comment. Returns the old comment.
381
382               print $zip->zipfileComment();
383               $zip->zipfileComment( 'New Comment' );
384
385       eocdOffset()
386           Returns the (unexpected) number of bytes between where the EOCD was
387           found and where it expected to be. This is normally 0, but would be
388           positive if something (a virus, perhaps) had added bytes somewhere
389           before the EOCD. Not used for writing zips, but might be
390           interesting if you read a zip in. Here is an example of how you can
391           diagnose this:
392
393             my $zip = Archive::Zip->new('somefile.zip');
394             if ($zip->eocdOffset())
395             {
396               warn "A virus has added ", $zip->eocdOffset, " bytes of garbage\n";
397             }
398
399           The "eocdOffset()" is used to adjust the starting position of
400           member headers, if necessary.
401
402       fileName()
403           Returns the name of the file last read from. If nothing has been
404           read yet, returns an empty string; if read from a file handle,
405           returns the handle in string form.
406
407   Zip Archive Member Operations
408       Various operations on a zip file modify members. When a member is
409       passed as an argument, you can either use a reference to the member
410       itself, or the name of a member. Of course, using the name requires
411       that names be unique within a zip (this is not enforced).
412
413       removeMember( $memberOrName )
414           Remove and return the given member, or match its name and remove
415           it. Returns undef if member or name doesn't exist in this Zip. No-
416           op if member does not belong to this zip.
417
418       replaceMember( $memberOrName, $newMember )
419           Remove and return the given member, or match its name and remove
420           it. Replace with new member. Returns undef if member or name
421           doesn't exist in this Zip, or if $newMember is undefined.
422
423           It is an (undiagnosed) error to provide a $newMember that is a
424           member of the zip being modified.
425
426               my $member1 = $zip->removeMember( 'xyz' );
427               my $member2 = $zip->replaceMember( 'abc', $member1 );
428               # now, $member2 (named 'abc') is not in $zip,
429               # and $member1 (named 'xyz') is, having taken $member2's place.
430
431       extractMember( $memberOrName [, $extractedName ] )
432           Extract the given member, or match its name and extract it.
433           Returns undef if member doesn't exist in this Zip. If optional
434           second arg is given, use it as the name of the extracted member.
435           Otherwise, the internal filename of the member is used as the name
436           of the extracted file or directory.  If you pass $extractedName, it
437           should be in the local file system's format.  All necessary
438           directories will be created. Returns "AZ_OK" on success.
439
440       extractMemberWithoutPaths( $memberOrName [, $extractedName ] )
441           Extract the given member, or match its name and extract it.  Does
442           not use path information (extracts into the current directory).
443           Returns undef if member doesn't exist in this Zip.  If optional
444           second arg is given, use it as the name of the extracted member
445           (its paths will be deleted too). Otherwise, the internal filename
446           of the member (minus paths) is used as the name of the extracted
447           file or directory. Returns "AZ_OK" on success.
448
449       addMember( $member )
450           Append a member (possibly from another zip file) to the zip file.
451           Returns the new member. Generally, you will use addFile(),
452           addDirectory(), addFileOrDirectory(), addString(), or read() to add
453           members.
454
455               # Move member named 'abc' to end of zip:
456               my $member = $zip->removeMember( 'abc' );
457               $zip->addMember( $member );
458
459       updateMember( $memberOrName, $fileName )
460           Update a single member from the file or directory named $fileName.
461           Returns the (possibly added or updated) member, if any; "undef" on
462           errors.  The comparison is based on "lastModTime()" and (in the
463           case of a non-directory) the size of the file.
464
465       addFile( $fileName [, $newName ] )
466           Append a member whose data comes from an external file, returning
467           the member or undef. The member will have its file name set to the
468           name of the external file, and its desiredCompressionMethod set to
469           COMPRESSION_DEFLATED. The file attributes and last modification
470           time will be set from the file.  If the name given does not
471           represent a readable plain file or symbolic link, undef will be
472           returned. $fileName must be in the format required for the local
473           file system.  The optional $newName argument sets the internal file
474           name to something different than the given $fileName. $newName, if
475           given, must be in Zip name format (i.e. Unix).  The text mode bit
476           will be set if the contents appears to be text (as returned by the
477           "-T" perl operator).
478
479           NOTE that you shouldn't (generally) use absolute path names in zip
480           member names, as this will cause problems with some zip tools as
481           well as introduce a security hole and make the zip harder to use.
482
483       addDirectory( $directoryName [, $fileName ] )
484           Append a member created from the given directory name. The
485           directory name does not have to name an existing directory.  If the
486           named directory exists, the file modification time and permissions
487           are set from the existing directory, otherwise they are set to now
488           and permissive default permissions.  $directoryName must be in
489           local file system format.  The optional second argument sets the
490           name of the archive member (which defaults to $directoryName). If
491           given, it must be in Zip (Unix) format.  Returns the new member.
492
493       addFileOrDirectory( $name [, $newName ] )
494           Append a member from the file or directory named $name. If $newName
495           is given, use it for the name of the new member.  Will add or
496           remove trailing slashes from $newName as needed.  $name must be in
497           local file system format.  The optional second argument sets the
498           name of the archive member (which defaults to $name). If given, it
499           must be in Zip (Unix) format.
500
501       addString( $stringOrStringRef, $name )
502           Append a member created from the given string or string reference.
503           The name is given by the second argument.  Returns the new member.
504           The last modification time will be set to now, and the file
505           attributes will be set to permissive defaults.
506
507               my $member = $zip->addString( 'This is a test', 'test.txt' );
508
509       contents( $memberOrMemberName [, $newContents ] )
510           Returns the uncompressed data for a particular member, or undef.
511
512               print "xyz.txt contains " . $zip->contents( 'xyz.txt' );
513
514           Also can change the contents of a member:
515
516               $zip->contents( 'xyz.txt', 'This is the new contents' );
517
518           If called expecting an array as the return value, it will include
519           the status as the second value in the array.
520
521               ($content, $status) = $zip->contents( 'xyz.txt');
522
523   Zip Archive I/O operations
524       A Zip archive can be written to a file or file handle, or read from
525       one.
526
527       writeToFileNamed( $fileName )
528           Write a zip archive to named file. Returns "AZ_OK" on success.
529
530               my $status = $zip->writeToFileNamed( 'xx.zip' );
531               die "error somewhere" if $status != AZ_OK;
532
533           Note that if you use the same name as an existing zip file that you
534           read in, you will clobber ZipFileMembers. So instead, write to a
535           different file name, then delete the original.  If you use the
536           "overwrite()" or "overwriteAs()" methods, you can re-write the
537           original zip in this way.  $fileName should be a valid file name on
538           your system.
539
540       writeToFileHandle( $fileHandle [, $seekable] )
541           Write a zip archive to a file handle. Return AZ_OK on success. The
542           optional second arg tells whether or not to try to seek backwards
543           to re-write headers. If not provided, it is set if the Perl "-f"
544           test returns true. This could fail on some operating systems,
545           though.
546
547               my $fh = IO::File->new( 'someFile.zip', 'w' );
548               unless ( $zip->writeToFileHandle( $fh ) == AZ_OK ) {
549                   # error handling
550               }
551
552           If you pass a file handle that is not seekable (like if you're
553           writing to a pipe or a socket), pass a false second argument:
554
555               my $fh = IO::File->new( '| cat > somefile.zip', 'w' );
556               $zip->writeToFileHandle( $fh, 0 );   # fh is not seekable
557
558           If this method fails during the write of a member, that member and
559           all following it will return false from "wasWritten()". See
560           writeCentralDirectory() for a way to deal with this.  If you want,
561           you can write data to the file handle before passing it to
562           writeToFileHandle(); this could be used (for instance) for making
563           self-extracting archives. However, this only works reliably when
564           writing to a real file (as opposed to STDOUT or some other possible
565           non-file).
566
567           See examples/selfex.pl for how to write a self-extracting archive.
568
569       writeCentralDirectory( $fileHandle [, $offset ] )
570           Writes the central directory structure to the given file handle.
571
572           Returns AZ_OK on success. If given an $offset, will seek to that
573           point before writing. This can be used for recovery in cases where
574           writeToFileHandle or writeToFileNamed returns an IO error because
575           of running out of space on the destination file.
576
577           You can truncate the zip by seeking backwards and then writing the
578           directory:
579
580               my $fh = IO::File->new( 'someFile.zip', 'w' );
581                   my $retval = $zip->writeToFileHandle( $fh );
582               if ( $retval == AZ_IO_ERROR ) {
583                   my @unwritten = grep { not $_->wasWritten() } $zip->members();
584                   if (@unwritten) {
585                       $zip->removeMember( $member ) foreach my $member ( @unwritten );
586                       $zip->writeCentralDirectory( $fh,
587                       $unwritten[0]->writeLocalHeaderRelativeOffset());
588                   }
589               }
590
591       overwriteAs( $newName )
592           Write the zip to the specified file, as safely as possible.  This
593           is done by first writing to a temp file, then renaming the original
594           if it exists, then renaming the temp file, then deleting the
595           renamed original if it exists. Returns AZ_OK if successful.
596
597       overwrite()
598           Write back to the original zip file. See overwriteAs() above.  If
599           the zip was not ever read from a file, this generates an error.
600
601       read( $fileName )
602           Read zipfile headers from a zip file, appending new members.
603           Returns "AZ_OK" or error code.
604
605               my $zipFile = Archive::Zip->new();
606               my $status = $zipFile->read( '/some/FileName.zip' );
607
608       readFromFileHandle( $fileHandle, $filename )
609           Read zipfile headers from an already-opened file handle, appending
610           new members. Does not close the file handle.  Returns "AZ_OK" or
611           error code. Note that this requires a seekable file handle; reading
612           from a stream is not yet supported.
613
614               my $fh = IO::File->new( '/some/FileName.zip', 'r' );
615               my $zip1 = Archive::Zip->new();
616               my $status = $zip1->readFromFileHandle( $fh );
617               my $zip2 = Archive::Zip->new();
618               $status = $zip2->readFromFileHandle( $fh );
619
620   Zip Archive Tree operations
621       These used to be in Archive::Zip::Tree but got moved into Archive::Zip.
622       They enable operation on an entire tree of members or files.  A usage
623       example:
624
625         use Archive::Zip;
626         my $zip = Archive::Zip->new();
627
628         # add all readable files and directories below . as xyz/*
629         $zip->addTree( '.', 'xyz' );
630
631         # add all readable plain files below /abc as def/*
632         $zip->addTree( '/abc', 'def', sub { -f && -r } );
633
634         # add all .c files below /tmp as stuff/*
635         $zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );
636
637         # add all .o files below /tmp as stuff/* if they aren't writable
638         $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );
639
640         # add all .so files below /tmp that are smaller than 200 bytes as stuff/*
641         $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );
642
643         # and write them into a file
644         $zip->writeToFileNamed('xxx.zip');
645
646         # now extract the same files into /tmpx
647         $zip->extractTree( 'stuff', '/tmpx' );
648
649       $zip->addTree( $root, $dest [,$pred] ) -- Add tree of files to a zip
650           $root is the root of the tree of files and directories to be added.
651           It is a valid directory name on your system. $dest is the name for
652           the root in the zip file (undef or blank means to use relative
653           pathnames). It is a valid ZIP directory name (that is, it uses
654           forward slashes (/) for separating directory components). $pred is
655           an optional subroutine reference to select files: it is passed the
656           name of the prospective file or directory using $_, and if it
657           returns true, the file or directory will be included. The default
658           is to add all readable files and directories. For instance, using
659
660             my $pred = sub { /\.txt/ };
661             $zip->addTree( '.', '', $pred );
662
663           will add all the .txt files in and below the current directory,
664           using relative names, and making the names identical in the
665           zipfile:
666
667             original name           zip member name
668             ./xyz                   xyz
669             ./a/                    a/
670             ./a/b                   a/b
671
672           To translate absolute to relative pathnames, just pass them in:
673           $zip->addTree( '/c/d', 'a' );
674
675             original name           zip member name
676             /c/d/xyz                a/xyz
677             /c/d/a/                 a/a/
678             /c/d/a/b                a/a/b
679
680           Returns AZ_OK on success. Note that this will not follow symbolic
681           links to directories. Note also that this does not check for the
682           validity of filenames.
683
684           Note that you generally don't want to make zip archive member names
685           absolute.
686
687       $zip->addTreeMatching( $root, $dest, $pattern [,$pred] )
688           $root is the root of the tree of files and directories to be added
689           $dest is the name for the root in the zip file (undef means to use
690           relative pathnames) $pattern is a (non-anchored) regular expression
691           for filenames to match $pred is an optional subroutine reference to
692           select files: it is passed the name of the prospective file or
693           directory in $_, and if it returns true, the file or directory will
694           be included.  The default is to add all readable files and
695           directories. To add all files in and below the current dirctory
696           whose names end in ".pl", and make them extract into a subdirectory
697           named "xyz", do this:
698
699             $zip->addTreeMatching( '.', 'xyz', '\.pl$' )
700
701           To add all writable files in and below the dirctory named "/abc"
702           whose names end in ".pl", and make them extract into a subdirectory
703           named "xyz", do this:
704
705             $zip->addTreeMatching( '/abc', 'xyz', '\.pl$', sub { -w } )
706
707           Returns AZ_OK on success. Note that this will not follow symbolic
708           links to directories.
709
710       $zip->updateTree( $root, [ $dest, [ $pred [, $mirror]]] );
711           Update a zip file from a directory tree.
712
713           "updateTree()" takes the same arguments as "addTree()", but first
714           checks to see whether the file or directory already exists in the
715           zip file, and whether it has been changed.
716
717           If the fourth argument $mirror is true, then delete all my members
718           if corresponding files weren't found.
719
720           Returns an error code or AZ_OK if all is well.
721
722       $zip->extractTree()
723       $zip->extractTree( $root )
724       $zip->extractTree( $root, $dest )
725       $zip->extractTree( $root, $dest, $volume )
726           If you don't give any arguments at all, will extract all the files
727           in the zip with their original names.
728
729           If you supply one argument for $root, "extractTree" will extract
730           all the members whose names start with $root into the current
731           directory, stripping off $root first.  $root is in Zip (Unix)
732           format.  For instance,
733
734             $zip->extractTree( 'a' );
735
736           when applied to a zip containing the files: a/x a/b/c ax/d/e d/e
737           will extract:
738
739           a/x as ./x
740
741           a/b/c as ./b/c
742
743           If you give two arguments, "extractTree" extracts all the members
744           whose names start with $root. It will translate $root into $dest to
745           construct the destination file name.  $root and $dest are in Zip
746           (Unix) format.  For instance,
747
748              $zip->extractTree( 'a', 'd/e' );
749
750           when applied to a zip containing the files: a/x a/b/c ax/d/e d/e
751           will extract:
752
753           a/x to d/e/x
754
755           a/b/c to d/e/b/c and ignore ax/d/e and d/e
756
757           If you give three arguments, "extractTree" extracts all the members
758           whose names start with $root. It will translate $root into $dest to
759           construct the destination file name, and then it will convert to
760           local file system format, using $volume as the name of the
761           destination volume.
762
763           $root and $dest are in Zip (Unix) format.
764
765           $volume is in local file system format.
766
767           For instance, under Windows,
768
769              $zip->extractTree( 'a', 'd/e', 'f:' );
770
771           when applied to a zip containing the files: a/x a/b/c ax/d/e d/e
772           will extract:
773
774           a/x to f:d/e/x
775
776           a/b/c to f:d/e/b/c and ignore ax/d/e and d/e
777
778           If you want absolute paths (the prior example used paths relative
779           to the current directory on the destination volume, you can specify
780           these in $dest:
781
782              $zip->extractTree( 'a', '/d/e', 'f:' );
783
784           when applied to a zip containing the files: a/x a/b/c ax/d/e d/e
785           will extract:
786
787           a/x to f:\d\e\x
788
789           a/b/c to f:\d\e\b\c and ignore ax/d/e and d/e
790
791           Returns an error code or AZ_OK if everything worked OK.
792

MEMBER OPERATIONS

794   Member Class Methods
795       Several constructors allow you to construct members without adding them
796       to a zip archive. These work the same as the addFile(), addDirectory(),
797       and addString() zip instance methods described above, but they don't
798       add the new members to a zip.
799
800       Archive::Zip::Member->newFromString( $stringOrStringRef [, $fileName] )
801           Construct a new member from the given string. Returns undef on
802           error.
803
804               my $member = Archive::Zip::Member->newFromString( 'This is a test',
805                                                            'xyz.txt' );
806
807       newFromFile( $fileName )
808           Construct a new member from the given file. Returns undef on error.
809
810               my $member = Archive::Zip::Member->newFromFile( 'xyz.txt' );
811
812       newDirectoryNamed( $directoryName [, $zipname ] )
813           Construct a new member from the given directory.  $directoryName
814           must be a valid name on your file system; it doesn't have to exist.
815
816           If given, $zipname will be the name of the zip member; it must be a
817           valid Zip (Unix) name. If not given, it will be converted from
818           $directoryName.
819
820           Returns undef on error.
821
822               my $member = Archive::Zip::Member->newDirectoryNamed( 'CVS/' );
823
824   Member Simple accessors
825       These methods get (and/or set) member attribute values.
826
827       versionMadeBy()
828           Gets the field from the member header.
829
830       fileAttributeFormat( [$format] )
831           Gets or sets the field from the member header. These are "FA_*"
832           values.
833
834       versionNeededToExtract()
835           Gets the field from the member header.
836
837       bitFlag()
838           Gets the general purpose bit field from the member header.  This is
839           where the "GPBF_*" bits live.
840
841       compressionMethod()
842           Returns the member compression method. This is the method that is
843           currently being used to compress the member data.  This will be
844           COMPRESSION_STORED for added string or file members, or any of the
845           "COMPRESSION_*" values for members from a zip file. However, this
846           module can only handle members whose data is in COMPRESSION_STORED
847           or COMPRESSION_DEFLATED format.
848
849       desiredCompressionMethod( [$method] )
850           Get or set the member's "desiredCompressionMethod". This is the
851           compression method that will be used when the member is written.
852           Returns prior desiredCompressionMethod. Only COMPRESSION_DEFLATED
853           or COMPRESSION_STORED are valid arguments. Changing to
854           COMPRESSION_STORED will change the member desiredCompressionLevel
855           to 0; changing to COMPRESSION_DEFLATED will change the member
856           desiredCompressionLevel to COMPRESSION_LEVEL_DEFAULT.
857
858       desiredCompressionLevel( [$method] )
859           Get or set the member's desiredCompressionLevel This is the method
860           that will be used to write. Returns prior desiredCompressionLevel.
861           Valid arguments are 0 through 9, COMPRESSION_LEVEL_NONE,
862           COMPRESSION_LEVEL_DEFAULT, COMPRESSION_LEVEL_BEST_COMPRESSION, and
863           COMPRESSION_LEVEL_FASTEST. 0 or COMPRESSION_LEVEL_NONE will change
864           the desiredCompressionMethod to COMPRESSION_STORED.  All other
865           arguments will change the desiredCompressionMethod to
866           COMPRESSION_DEFLATED.
867
868       externalFileName()
869           Return the member's external file name, if any, or undef.
870
871       fileName()
872           Get or set the member's internal filename. Returns the (possibly
873           new) filename. Names will have backslashes converted to forward
874           slashes, and will have multiple consecutive slashes converted to
875           single ones.
876
877       lastModFileDateTime()
878           Return the member's last modification date/time stamp in MS-DOS
879           format.
880
881       lastModTime()
882           Return the member's last modification date/time stamp, converted to
883           unix localtime format.
884
885               print "Mod Time: " . scalar( localtime( $member->lastModTime() ) );
886
887       setLastModFileDateTimeFromUnix()
888           Set the member's lastModFileDateTime from the given unix time.
889
890               $member->setLastModFileDateTimeFromUnix( time() );
891
892       internalFileAttributes()
893           Return the internal file attributes field from the zip header. This
894           is only set for members read from a zip file.
895
896       externalFileAttributes()
897           Return member attributes as read from the ZIP file. Note that these
898           are NOT UNIX!
899
900       unixFileAttributes( [$newAttributes] )
901           Get or set the member's file attributes using UNIX file attributes.
902           Returns old attributes.
903
904               my $oldAttribs = $member->unixFileAttributes( 0666 );
905
906           Note that the return value has more than just the file permissions,
907           so you will have to mask off the lowest bits for comparisions.
908
909       localExtraField( [$newField] )
910           Gets or sets the extra field that was read from the local header.
911           This is not set for a member from a zip file until after the member
912           has been written out. The extra field must be in the proper format.
913
914       cdExtraField( [$newField] )
915           Gets or sets the extra field that was read from the central
916           directory header. The extra field must be in the proper format.
917
918       extraFields()
919           Return both local and CD extra fields, concatenated.
920
921       fileComment( [$newComment] )
922           Get or set the member's file comment.
923
924       hasDataDescriptor()
925           Get or set the data descriptor flag. If this is set, the local
926           header will not necessarily have the correct data sizes. Instead, a
927           small structure will be stored at the end of the member data with
928           these values. This should be transparent in normal operation.
929
930       crc32()
931           Return the CRC-32 value for this member. This will not be set for
932           members that were constructed from strings or external files until
933           after the member has been written.
934
935       crc32String()
936           Return the CRC-32 value for this member as an 8 character printable
937           hex string. This will not be set for members that were constructed
938           from strings or external files until after the member has been
939           written.
940
941       compressedSize()
942           Return the compressed size for this member. This will not be set
943           for members that were constructed from strings or external files
944           until after the member has been written.
945
946       uncompressedSize()
947           Return the uncompressed size for this member.
948
949       isEncrypted()
950           Return true if this member is encrypted. The Archive::Zip module
951           does not currently create or extract encrypted members.
952
953       isTextFile( [$flag] )
954           Returns true if I am a text file. Also can set the status if given
955           an argument (then returns old state). Note that this module does
956           not currently do anything with this flag upon extraction or
957           storage. That is, bytes are stored in native format whether or not
958           they came from a text file.
959
960       isBinaryFile()
961           Returns true if I am a binary file. Also can set the status if
962           given an argument (then returns old state). Note that this module
963           does not currently do anything with this flag upon extraction or
964           storage. That is, bytes are stored in native format whether or not
965           they came from a text file.
966
967       extractToFileNamed( $fileName )
968           Extract me to a file with the given name. The file will be created
969           with default modes. Directories will be created as needed.  The
970           $fileName argument should be a valid file name on your file system.
971           Returns AZ_OK on success.
972
973       isDirectory()
974           Returns true if I am a directory.
975
976       writeLocalHeaderRelativeOffset()
977           Returns the file offset in bytes the last time I was written.
978
979       wasWritten()
980           Returns true if I was successfully written. Reset at the beginning
981           of a write attempt.
982
983   Low-level member data reading
984       It is possible to use lower-level routines to access member data
985       streams, rather than the extract* methods and contents(). For instance,
986       here is how to print the uncompressed contents of a member in chunks
987       using these methods:
988
989           my ( $member, $status, $bufferRef );
990           $member = $zip->memberNamed( 'xyz.txt' );
991           $member->desiredCompressionMethod( COMPRESSION_STORED );
992           $status = $member->rewindData();
993           die "error $status" unless $status == AZ_OK;
994           while ( ! $member->readIsDone() )
995           {
996           ( $bufferRef, $status ) = $member->readChunk();
997           die "error $status"
998                       if $status != AZ_OK && $status != AZ_STREAM_END;
999           # do something with $bufferRef:
1000           print $$bufferRef;
1001           }
1002           $member->endRead();
1003
1004       readChunk( [$chunkSize] )
1005           This reads the next chunk of given size from the member's data
1006           stream and compresses or uncompresses it as necessary, returning a
1007           reference to the bytes read and a status. If size argument is not
1008           given, defaults to global set by Archive::Zip::setChunkSize. Status
1009           is AZ_OK on success until the last chunk, where it returns
1010           AZ_STREAM_END. Returns "( \$bytes, $status)".
1011
1012               my ( $outRef, $status ) = $self->readChunk();
1013               print $$outRef if $status != AZ_OK && $status != AZ_STREAM_END;
1014
1015       rewindData()
1016           Rewind data and set up for reading data streams or writing zip
1017           files. Can take options for "inflateInit()" or "deflateInit()", but
1018           this isn't likely to be necessary.  Subclass overrides should call
1019           this method. Returns "AZ_OK" on success.
1020
1021       endRead()
1022           Reset the read variables and free the inflater or deflater.  Must
1023           be called to close files, etc. Returns AZ_OK on success.
1024
1025       readIsDone()
1026           Return true if the read has run out of data or errored out.
1027
1028       contents()
1029           Return the entire uncompressed member data or undef in scalar
1030           context. When called in array context, returns "( $string, $status
1031           )"; status will be AZ_OK on success:
1032
1033               my $string = $member->contents();
1034               # or
1035               my ( $string, $status ) = $member->contents();
1036               die "error $status" unless $status == AZ_OK;
1037
1038           Can also be used to set the contents of a member (this may change
1039           the class of the member):
1040
1041               $member->contents( "this is my new contents" );
1042
1043       extractToFileHandle( $fh )
1044           Extract (and uncompress, if necessary) the member's contents to the
1045           given file handle. Return AZ_OK on success.
1046

Archive::Zip::FileMember methods

1048       The Archive::Zip::FileMember class extends Archive::Zip::Member. It is
1049       the base class for both ZipFileMember and NewFileMember classes. This
1050       class adds an "externalFileName" and an "fh" member to keep track of
1051       the external file.
1052
1053       externalFileName()
1054           Return the member's external filename.
1055
1056       fh()
1057           Return the member's read file handle. Automatically opens file if
1058           necessary.
1059

Archive::Zip::ZipFileMember methods

1061       The Archive::Zip::ZipFileMember class represents members that have been
1062       read from external zip files.
1063
1064       diskNumberStart()
1065           Returns the disk number that the member's local header resides in.
1066           Should be 0.
1067
1068       localHeaderRelativeOffset()
1069           Returns the offset into the zip file where the member's local
1070           header is.
1071
1072       dataOffset()
1073           Returns the offset from the beginning of the zip file to the
1074           member's data.
1075

REQUIRED MODULES

1077       Archive::Zip requires several other modules:
1078
1079       Carp
1080
1081       Compress::Raw::Zlib
1082
1083       Cwd
1084
1085       File::Basename
1086
1087       File::Copy
1088
1089       File::Find
1090
1091       File::Path
1092
1093       File::Spec
1094
1095       IO::File
1096
1097       IO::Seekable
1098
1099       Time::Local
1100

BUGS AND CAVEATS

1102   When not to use Archive::Zip
1103       If you are just going to be extracting zips (and/or other archives) you
1104       are recommended to look at using Archive::Extract instead, as it is
1105       much easier to use and factors out archive-specific functionality.
1106
1107   Try to avoid IO::Scalar
1108       One of the most common ways to use Archive::Zip is to generate Zip
1109       files in-memory. Most people have use IO::Scalar for this purpose.
1110
1111       Unfortunately, as of 1.11 this module no longer works with IO::Scalar
1112       as it incorrectly implements seeking.
1113
1114       Anybody using IO::Scalar should consider porting to IO::String, which
1115       is smaller, lighter, and is implemented to be perfectly compatible with
1116       regular seekable filehandles.
1117
1118       Support for IO::Scalar most likely will not be restored in the future,
1119       as IO::Scalar itself cannot change the way it is implemented due to
1120       back-compatibility issues.
1121

TO DO

1123       * auto-choosing storing vs compression
1124
1125       * extra field hooks (see notes.txt)
1126
1127       * check for dups on addition/renaming?
1128
1129       * Text file extraction (line end translation)
1130
1131       * Reading zip files from non-seekable inputs
1132         (Perhaps by proxying through IO::String?)
1133
1134       * separate unused constants into separate module
1135
1136       * cookbook style docs
1137
1138       * Handle tainted paths correctly
1139
1140       * Work on better compatability with other IO:: modules
1141

SUPPORT

1143       Bugs should be reported via the CPAN bug tracker
1144
1145       <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Archive-Zip>
1146
1147       For other issues contact the maintainer
1148

AUTHOR

1150       Adam Kennedy <adamk@cpan.org>
1151
1152       Previously maintained by Steve Peters <steve@fisharerojo.org>.
1153
1154       File attributes code by Maurice Aubrey <maurice@lovelyfilth.com>.
1155
1156       Originally by Ned Konz <nedkonz@cpan.org>.
1157
1159       Some parts copyright 2006 - 2009 Adam Kennedy.
1160
1161       Some parts copyright 2005 Steve Peters.
1162
1163       Original work copyright 2000 - 2004 Ned Konz.
1164
1165       This program is free software; you can redistribute it and/or modify it
1166       under the same terms as Perl itself.
1167

SEE ALSO

1169       Look at Archive::Zip::MemberRead which is a wrapper that allows one to
1170       read Zip archive members as if they were files.
1171
1172       Compress::Raw::Zlib, Archive::Tar, Archive::Extract
1173
1174       There is a Japanese translation of this document at
1175       <http://www.memb.jp/~deq/perl/doc-ja/Archive-Zip.html> that was done by
1176       DEQ <deq@oct.zaq.ne.jp> . Thanks!
1177
1178
1179
1180perl v5.16.3                      2009-06-30                   Archive::Zip(3)
Impressum