1 package IO::Uncompress::Unzip;
11 use IO::Uncompress::RawInflate 2.020 ;
12 use IO::Compress::Base::Common 2.020 qw(:Status createSelfTiedObject);
13 use IO::Uncompress::Adapter::Inflate 2.020 ;
14 use IO::Uncompress::Adapter::Identity 2.020 ;
15 use IO::Compress::Zlib::Extra 2.020 ;
16 use IO::Compress::Zip::Constants 2.020 ;
18 use Compress::Raw::Zlib 2.020 qw(crc32) ;
22 eval { require IO::Uncompress::Adapter::Bunzip2 ;
23 import IO::Uncompress::Adapter::Bunzip2 } ;
24 #eval { require IO::Uncompress::Adapter::UnLzma ;
25 # import IO::Uncompress::Adapter::UnLzma } ;
31 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $UnzipError, %headerLookup);
36 @ISA = qw(Exporter IO::Uncompress::RawInflate);
37 @EXPORT_OK = qw( $UnzipError unzip );
38 %EXPORT_TAGS = %IO::Uncompress::RawInflate::EXPORT_TAGS ;
39 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
40 Exporter::export_ok_tags('all');
43 ZIP_CENTRAL_HDR_SIG, \&skipCentralDirectory,
44 ZIP_END_CENTRAL_HDR_SIG, \&skipEndCentralDirectory,
45 ZIP64_END_CENTRAL_REC_HDR_SIG, \&skipCentralDirectory64Rec,
46 ZIP64_END_CENTRAL_LOC_HDR_SIG, \&skipCentralDirectory64Loc,
47 ZIP64_ARCHIVE_EXTRA_SIG, \&skipArchiveExtra,
48 ZIP64_DIGITAL_SIGNATURE_SIG, \&skipDigitalSignature,
54 my $obj = createSelfTiedObject($class, \$UnzipError);
55 $obj->_create(undef, 0, @_);
60 my $obj = createSelfTiedObject(undef, \$UnzipError);
61 return $obj->_inf(@_) ;
66 use IO::Compress::Base::Common 2.020 qw(:Parse);
71 'Name' => [1, 1, Parse_any, undef],
73 # 'Streaming' => [1, 1, Parse_boolean, 1],
82 # unzip always needs crc32
83 $got->value('CRC32' => 1);
85 *$self->{UnzipData}{Name} = $got->value('Name');
95 my $magic = $self->ckMagic()
98 *$self->{Info} = $self->readHeader($magic)
110 $self->smartReadExact(\$magic, 4);
112 *$self->{HeaderPending} = $magic ;
114 return $self->HeaderError("Minimum header size is " .
116 if length $magic != 4 ;
118 return $self->HeaderError("Bad Magic")
119 if ! _isZipMagic($magic) ;
121 *$self->{Type} = 'zip';
133 my $name = *$self->{UnzipData}{Name} ;
134 my $hdr = $self->_readZipHeader($magic) ;
138 if (! defined $name || $hdr->{Name} eq $name)
145 if (*$self->{ZipData}{Streaming}) {
150 my $status = $self->smartRead(\$b, 1024 * 16);
156 $status = *$self->{Uncomp}->uncompr(\$b, \$temp_buf, 0, $out);
158 return $self->saveErrorString(undef, *$self->{Uncomp}{Error},
159 *$self->{Uncomp}{ErrorNo})
160 if $self->saveStatus($status) == STATUS_ERROR;
162 if ($status == STATUS_ENDSTREAM) {
163 *$self->{Uncomp}->reset();
164 $self->pushBack($b) ;
170 $self->smartReadExact(\$buffer, $hdr->{TrailerLength})
171 or return $self->saveErrorString(undef, "Truncated file");
174 my $c = $hdr->{CompressedLength}->get32bit();
175 $self->smartReadExact(\$buffer, $c)
176 or return $self->saveErrorString(undef, "Truncated file");
180 $self->chkTrailer($buffer) == STATUS_OK
181 or return $self->saveErrorString(undef, "Truncated file");
183 $hdr = $self->_readFullZipHeader();
185 return $self->saveErrorString(undef, "Cannot find '$name'")
186 if $self->smartEof();
197 my ($sig, $CRC32, $cSize, $uSize) ;
198 my ($cSizeHi, $uSizeHi) = (0, 0);
199 if (*$self->{ZipData}{Streaming}) {
200 $sig = unpack ("V", substr($trailer, 0, 4));
201 $CRC32 = unpack ("V", substr($trailer, 4, 4));
203 if (*$self->{ZipData}{Zip64} ) {
204 $cSize = U64::newUnpack_V64 substr($trailer, 8, 8);
205 $uSize = U64::newUnpack_V64 substr($trailer, 16, 8);
208 $cSize = U64::newUnpack_V32 substr($trailer, 8, 4);
209 $uSize = U64::newUnpack_V32 substr($trailer, 12, 4);
212 return $self->TrailerError("Data Descriptor signature, got $sig")
213 if $sig != ZIP_DATA_HDR_SIG;
216 ($CRC32, $cSize, $uSize) =
217 (*$self->{ZipData}{Crc32},
218 *$self->{ZipData}{CompressedLen},
219 *$self->{ZipData}{UnCompressedLen});
222 if (*$self->{Strict}) {
223 return $self->TrailerError("CRC mismatch")
224 if $CRC32 != *$self->{ZipData}{CRC32} ;
226 return $self->TrailerError("CSIZE mismatch.")
227 if ! $cSize->equal(*$self->{CompSize});
229 return $self->TrailerError("USIZE mismatch.")
230 if ! $uSize->equal(*$self->{UnCompSize});
233 my $reachedEnd = STATUS_ERROR ;
234 # check for central directory or end of central directory
238 my $got = $self->smartRead(\$magic, 4);
240 return $self->saveErrorString(STATUS_ERROR, "Truncated file")
241 if $got != 4 && *$self->{Strict};
247 return STATUS_ERROR ;
250 $self->pushBack($magic) ;
254 my $sig = unpack("V", $magic) ;
257 if ($hdr = $headerLookup{$sig})
259 if (&$hdr($self, $magic) != STATUS_OK ) {
260 if (*$self->{Strict}) {
261 return STATUS_ERROR ;
269 if ($sig == ZIP_END_CENTRAL_HDR_SIG)
275 elsif ($sig == ZIP_LOCAL_HDR_SIG)
277 $self->pushBack($magic) ;
283 $self->pushBack($magic) ;
291 sub skipCentralDirectory
297 $self->smartReadExact(\$buffer, 46 - 4)
298 or return $self->TrailerError("Minimum header size is " .
301 my $keep = $magic . $buffer ;
302 *$self->{HeaderPending} = $keep ;
304 #my $versionMadeBy = unpack ("v", substr($buffer, 4-4, 2));
305 #my $extractVersion = unpack ("v", substr($buffer, 6-4, 2));
306 #my $gpFlag = unpack ("v", substr($buffer, 8-4, 2));
307 #my $compressedMethod = unpack ("v", substr($buffer, 10-4, 2));
308 #my $lastModTime = unpack ("V", substr($buffer, 12-4, 4));
309 #my $crc32 = unpack ("V", substr($buffer, 16-4, 4));
310 my $compressedLength = unpack ("V", substr($buffer, 20-4, 4));
311 my $uncompressedLength = unpack ("V", substr($buffer, 24-4, 4));
312 my $filename_length = unpack ("v", substr($buffer, 28-4, 2));
313 my $extra_length = unpack ("v", substr($buffer, 30-4, 2));
314 my $comment_length = unpack ("v", substr($buffer, 32-4, 2));
315 #my $disk_start = unpack ("v", substr($buffer, 34-4, 2));
316 #my $int_file_attrib = unpack ("v", substr($buffer, 36-4, 2));
317 #my $ext_file_attrib = unpack ("V", substr($buffer, 38-4, 2));
318 #my $lcl_hdr_offset = unpack ("V", substr($buffer, 42-4, 2));
324 if ($filename_length)
326 $self->smartReadExact(\$filename, $filename_length)
327 or return $self->TruncatedTrailer("filename");
333 $self->smartReadExact(\$extraField, $extra_length)
334 or return $self->TruncatedTrailer("extra");
335 $keep .= $extraField ;
340 $self->smartReadExact(\$comment, $comment_length)
341 or return $self->TruncatedTrailer("comment");
354 $self->smartReadExact(\$buffer, 4)
355 or return $self->TrailerError("Minimum header size is " .
358 my $keep = $magic . $buffer ;
360 my $size = unpack ("V", $buffer);
362 $self->smartReadExact(\$buffer, $size)
363 or return $self->TrailerError("Minimum header size is " .
367 *$self->{HeaderPending} = $keep ;
373 sub skipCentralDirectory64Rec
379 $self->smartReadExact(\$buffer, 8)
380 or return $self->TrailerError("Minimum header size is " .
383 my $keep = $magic . $buffer ;
385 my ($sizeLo, $sizeHi) = unpack ("V V", $buffer);
387 # TODO - take SizeHi into account
388 $self->smartReadExact(\$buffer, $sizeLo)
389 or return $self->TrailerError("Minimum header size is " .
390 $sizeLo . " bytes") ;
393 *$self->{HeaderPending} = $keep ;
395 #my $versionMadeBy = unpack ("v", substr($buffer, 0, 2));
396 #my $extractVersion = unpack ("v", substr($buffer, 2, 2));
397 #my $diskNumber = unpack ("V", substr($buffer, 4, 4));
398 #my $cntrlDirDiskNo = unpack ("V", substr($buffer, 8, 4));
399 #my $entriesInThisCD = unpack ("V V", substr($buffer, 12, 8));
400 #my $entriesInCD = unpack ("V V", substr($buffer, 20, 8));
401 #my $sizeOfCD = unpack ("V V", substr($buffer, 28, 8));
402 #my $offsetToCD = unpack ("V V", substr($buffer, 36, 8));
407 sub skipCentralDirectory64Loc
413 $self->smartReadExact(\$buffer, 20 - 4)
414 or return $self->TrailerError("Minimum header size is " .
417 my $keep = $magic . $buffer ;
418 *$self->{HeaderPending} = $keep ;
420 #my $startCdDisk = unpack ("V", substr($buffer, 4-4, 4));
421 #my $offsetToCD = unpack ("V V", substr($buffer, 8-4, 8));
422 #my $diskCount = unpack ("V", substr($buffer, 16-4, 4));
427 sub skipEndCentralDirectory
433 $self->smartReadExact(\$buffer, 22 - 4)
434 or return $self->TrailerError("Minimum header size is " .
437 my $keep = $magic . $buffer ;
438 *$self->{HeaderPending} = $keep ;
440 #my $diskNumber = unpack ("v", substr($buffer, 4-4, 2));
441 #my $cntrlDirDiskNo = unpack ("v", substr($buffer, 6-4, 2));
442 #my $entriesInThisCD = unpack ("v", substr($buffer, 8-4, 2));
443 #my $entriesInCD = unpack ("v", substr($buffer, 10-4, 2));
444 #my $sizeOfCD = unpack ("V", substr($buffer, 12-4, 2));
445 #my $offsetToCD = unpack ("V", substr($buffer, 16-4, 2));
446 my $comment_length = unpack ("v", substr($buffer, 20-4, 2));
452 $self->smartReadExact(\$comment, $comment_length)
453 or return $self->TruncatedTrailer("comment");
464 return 0 if length $buffer < 4 ;
465 my $sig = unpack("V", $buffer) ;
466 return $sig == ZIP_LOCAL_HDR_SIG ;
470 sub _readFullZipHeader($)
475 $self->smartReadExact(\$magic, 4);
477 *$self->{HeaderPending} = $magic ;
479 return $self->HeaderError("Minimum header size is " .
481 if length $magic != 4 ;
484 return $self->HeaderError("Bad Magic")
485 if ! _isZipMagic($magic) ;
487 my $status = $self->_readZipHeader($magic);
488 delete *$self->{Transparent} if ! defined $status ;
492 sub _readZipHeader($)
494 my ($self, $magic) = @_ ;
498 $self->smartReadExact(\$buffer, 30 - 4)
499 or return $self->HeaderError("Minimum header size is " .
502 my $keep = $magic . $buffer ;
503 *$self->{HeaderPending} = $keep ;
505 my $extractVersion = unpack ("v", substr($buffer, 4-4, 2));
506 my $gpFlag = unpack ("v", substr($buffer, 6-4, 2));
507 my $compressedMethod = unpack ("v", substr($buffer, 8-4, 2));
508 my $lastModTime = unpack ("V", substr($buffer, 10-4, 4));
509 my $crc32 = unpack ("V", substr($buffer, 14-4, 4));
510 my $compressedLength = new U64 unpack ("V", substr($buffer, 18-4, 4));
511 my $uncompressedLength = new U64 unpack ("V", substr($buffer, 22-4, 4));
512 my $filename_length = unpack ("v", substr($buffer, 26-4, 2));
513 my $extra_length = unpack ("v", substr($buffer, 28-4, 2));
518 my $streamingMode = ($gpFlag & ZIP_GP_FLAG_STREAMING_MASK) ? 1 : 0 ;
520 return $self->HeaderError("Streamed Stored content not supported")
521 if $streamingMode && $compressedMethod == 0 ;
523 return $self->HeaderError("Encrypted content not supported")
524 if $gpFlag & (ZIP_GP_FLAG_ENCRYPTED_MASK|ZIP_GP_FLAG_STRONG_ENCRYPTED_MASK);
526 return $self->HeaderError("Patch content not supported")
527 if $gpFlag & ZIP_GP_FLAG_PATCHED_MASK;
529 *$self->{ZipData}{Streaming} = $streamingMode;
532 if ($filename_length)
534 $self->smartReadExact(\$filename, $filename_length)
535 or return $self->TruncatedHeader("Filename");
543 $self->smartReadExact(\$extraField, $extra_length)
544 or return $self->TruncatedHeader("Extra Field");
546 my $bad = IO::Compress::Zlib::Extra::parseRawExtra($extraField,
548 return $self->HeaderError($bad)
551 $keep .= $extraField ;
556 $Extra{$_->[0]} = \$_->[1];
559 if (defined $Extra{ZIP_EXTRA_ID_ZIP64()})
563 my $buff = ${ $Extra{ZIP_EXTRA_ID_ZIP64()} };
565 # TODO - This code assumes that all the fields in the Zip64
566 # extra field aren't necessarily present. The spec says that
567 # they only exist if the equivalent local headers are -1.
568 # Need to check that info-zip fills out -1 in the local header
571 if (! $streamingMode) {
574 $uncompressedLength = U64::newUnpack_V64 substr($buff, 0, 8)
575 if $uncompressedLength == 0xFFFF ;
579 $compressedLength = U64::newUnpack_V64 substr($buff, $offset, 8)
580 if $compressedLength == 0xFFFF ;
584 #my $cheaderOffset = U64::newUnpack_V64 substr($buff, 16, 8);
585 #my $diskNumber = unpack ("V", substr($buff, 24, 4));
590 *$self->{ZipData}{Zip64} = $zip64;
592 if (! $streamingMode) {
593 *$self->{ZipData}{Streaming} = 0;
594 *$self->{ZipData}{Crc32} = $crc32;
595 *$self->{ZipData}{CompressedLen} = $compressedLength;
596 *$self->{ZipData}{UnCompressedLen} = $uncompressedLength;
597 *$self->{CompressedInputLengthRemaining} =
598 *$self->{CompressedInputLength} = $compressedLength->get32bit();
601 *$self->{ZipData}{Method} = $compressedMethod;
602 if ($compressedMethod == ZIP_CM_DEFLATE)
604 *$self->{Type} = 'zip-deflate';
605 my $obj = IO::Uncompress::Adapter::Inflate::mkUncompObject(1,0,0);
607 *$self->{Uncomp} = $obj;
608 *$self->{ZipData}{CRC32} = crc32(undef);
610 elsif ($compressedMethod == ZIP_CM_BZIP2)
612 return $self->HeaderError("Unsupported Compression format $compressedMethod")
613 if ! defined $IO::Uncompress::Adapter::Bunzip2::VERSION ;
615 *$self->{Type} = 'zip-bzip2';
617 my $obj = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
619 *$self->{Uncomp} = $obj;
620 *$self->{ZipData}{CRC32} = crc32(undef);
622 # elsif ($compressedMethod == ZIP_CM_LZMA)
624 # return $self->HeaderError("Unsupported Compression format $compressedMethod")
625 # if ! defined $IO::Uncompress::Adapter::UnLzma::VERSION ;
627 # *$self->{Type} = 'zip-lzma';
629 # my $obj = IO::Uncompress::Adapter::UnLzma::mkUncompObject();
631 # *$self->{Uncomp} = $obj;
632 # *$self->{ZipData}{CRC32} = crc32(undef);
634 elsif ($compressedMethod == ZIP_CM_STORE)
636 # TODO -- add support for reading uncompressed
638 *$self->{Type} = 'zip-stored';
640 my $obj = IO::Uncompress::Adapter::Identity::mkUncompObject();
642 *$self->{Uncomp} = $obj;
646 return $self->HeaderError("Unsupported Compression format $compressedMethod");
651 'FingerprintLength' => 4,
652 #'HeaderLength' => $compressedMethod == 8 ? length $keep : 0,
653 'HeaderLength' => length $keep,
655 'TrailerLength' => ! $streamingMode ? 0 : $zip64 ? 24 : 16,
657 'CompressedLength' => $compressedLength ,
658 'UncompressedLength' => $uncompressedLength ,
661 'Time' => _dosToUnixTime($lastModTime),
662 'Stream' => $streamingMode,
664 'MethodID' => $compressedMethod,
665 'MethodName' => $compressedMethod == ZIP_CM_DEFLATE
667 : $compressedMethod == ZIP_CM_BZIP2
669 : $compressedMethod == ZIP_CM_STORE
673 # 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0,
674 # 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0,
675 # 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0,
676 # 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0,
677 # 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0,
678 # 'Comment' => $comment,
680 # 'OsName' => defined $GZIP_OS_Names{$os}
681 # ? $GZIP_OS_Names{$os} : "Unknown",
682 # 'HeaderCRC' => $HeaderCRC,
684 # 'ExtraFlags' => $xfl,
685 'ExtraFieldRaw' => $extraField,
686 'ExtraField' => [ @EXTRA ],
692 sub filterUncompressed
696 if (*$self->{ZipData}{Method} == 12) {
697 *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32});
700 *$self->{ZipData}{CRC32} = *$self->{Uncomp}->crc32() ;
705 # from Archive::Zip & info-zip
710 my $year = ( ( $dt >> 25 ) & 0x7f ) + 80;
711 my $mon = ( ( $dt >> 21 ) & 0x0f ) - 1;
712 my $mday = ( ( $dt >> 16 ) & 0x1f );
714 my $hour = ( ( $dt >> 11 ) & 0x1f );
715 my $min = ( ( $dt >> 5 ) & 0x3f );
716 my $sec = ( ( $dt << 1 ) & 0x3e );
721 my $time_t = mktime( $sec, $min, $hour, $mday, $mon, $year, 0, 0, -1 );
722 return 0 if ! defined $time_t;
734 IO::Uncompress::Unzip - Read zip files/buffers
738 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
740 my $status = unzip $input => $output [,OPTS]
741 or die "unzip failed: $UnzipError\n";
743 my $z = new IO::Uncompress::Unzip $input [OPTS]
744 or die "unzip failed: $UnzipError\n";
746 $status = $z->read($buffer)
747 $status = $z->read($buffer, $length)
748 $status = $z->read($buffer, $length, $offset)
749 $line = $z->getline()
754 $status = $z->inflateSync()
756 $data = $z->trailingData()
757 $status = $z->nextStream()
758 $data = $z->getHeaderInfo()
760 $z->seek($position, $whence)
772 read($z, $buffer, $length);
773 read($z, $buffer, $length, $offset);
775 seek($z, $position, $whence)
783 This module provides a Perl interface that allows the reading of
786 For writing zip files/buffers, see the companion module IO::Compress::Zip.
788 =head1 Functional Interface
790 A top-level function, C<unzip>, is provided to carry out
791 "one-shot" uncompression between buffers and/or files. For finer
792 control over the uncompression process, see the L</"OO Interface">
795 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
797 unzip $input => $output [,OPTS]
798 or die "unzip failed: $UnzipError\n";
800 The functional interface needs Perl5.005 or better.
802 =head2 unzip $input => $output [, OPTS]
804 C<unzip> expects at least two parameters, C<$input> and C<$output>.
806 =head3 The C<$input> parameter
808 The parameter, C<$input>, is used to define the source of
811 It can take one of the following forms:
817 If the C<$input> parameter is a simple scalar, it is assumed to be a
818 filename. This file will be opened for reading and the input data
819 will be read from it.
823 If the C<$input> parameter is a filehandle, the input data will be
825 The string '-' can be used as an alias for standard input.
827 =item A scalar reference
829 If C<$input> is a scalar reference, the input data will be read
832 =item An array reference
834 If C<$input> is an array reference, each element in the array must be a
837 The input data will be read from each file in turn.
839 The complete array will be walked to ensure that it only
840 contains valid filenames before any data is uncompressed.
842 =item An Input FileGlob string
844 If C<$input> is a string that is delimited by the characters "<" and ">"
845 C<unzip> will assume that it is an I<input fileglob string>. The
846 input is the list of files that match the fileglob.
848 If the fileglob does not match any files ...
850 See L<File::GlobMapper|File::GlobMapper> for more details.
854 If the C<$input> parameter is any other type, C<undef> will be returned.
856 =head3 The C<$output> parameter
858 The parameter C<$output> is used to control the destination of the
859 uncompressed data. This parameter can take one of these forms.
865 If the C<$output> parameter is a simple scalar, it is assumed to be a
866 filename. This file will be opened for writing and the uncompressed
867 data will be written to it.
871 If the C<$output> parameter is a filehandle, the uncompressed data
872 will be written to it.
873 The string '-' can be used as an alias for standard output.
875 =item A scalar reference
877 If C<$output> is a scalar reference, the uncompressed data will be
878 stored in C<$$output>.
880 =item An Array Reference
882 If C<$output> is an array reference, the uncompressed data will be
883 pushed onto the array.
885 =item An Output FileGlob
887 If C<$output> is a string that is delimited by the characters "<" and ">"
888 C<unzip> will assume that it is an I<output fileglob string>. The
889 output is the list of files that match the fileglob.
891 When C<$output> is an fileglob string, C<$input> must also be a fileglob
892 string. Anything else is an error.
896 If the C<$output> parameter is any other type, C<undef> will be returned.
900 When C<$input> maps to multiple compressed files/buffers and C<$output> is
901 a single file/buffer, after uncompression C<$output> will contain a
902 concatenation of all the uncompressed data from each of the input
905 =head2 Optional Parameters
907 Unless specified below, the optional parameters for C<unzip>,
908 C<OPTS>, are the same as those used with the OO interface defined in the
909 L</"Constructor Options"> section below.
913 =item C<< AutoClose => 0|1 >>
915 This option applies to any input or output data streams to
916 C<unzip> that are filehandles.
918 If C<AutoClose> is specified, and the value is true, it will result in all
919 input and/or output filehandles being closed once C<unzip> has
922 This parameter defaults to 0.
924 =item C<< BinModeOut => 0|1 >>
926 When writing to a file or filehandle, set C<binmode> before writing to the
931 =item C<< Append => 0|1 >>
935 =item C<< MultiStream => 0|1 >>
937 If the input file/buffer contains multiple compressed data streams, this
938 option will uncompress the whole lot as a single data stream.
942 =item C<< TrailingData => $scalar >>
944 Returns the data, if any, that is present immediately after the compressed
945 data stream once uncompression is complete.
947 This option can be used when there is useful information immediately
948 following the compressed data stream, and you don't know the length of the
949 compressed data stream.
951 If the input is a buffer, C<trailingData> will return everything from the
952 end of the compressed data stream to the end of the buffer.
954 If the input is a filehandle, C<trailingData> will return the data that is
955 left in the filehandle input buffer once the end of the compressed data
956 stream has been reached. You can then use the filehandle to read the rest
959 Don't bother using C<trailingData> if the input is a filename.
961 If you know the length of the compressed data stream before you start
962 uncompressing, you can avoid having to use C<trailingData> by setting the
963 C<InputLength> option.
969 To read the contents of the file C<file1.txt.zip> and write the
970 compressed data to the file C<file1.txt>.
974 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
976 my $input = "file1.txt.zip";
977 my $output = "file1.txt";
978 unzip $input => $output
979 or die "unzip failed: $UnzipError\n";
981 To read from an existing Perl filehandle, C<$input>, and write the
982 uncompressed data to a buffer, C<$buffer>.
986 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
989 my $input = new IO::File "<file1.txt.zip"
990 or die "Cannot open 'file1.txt.zip': $!\n" ;
992 unzip $input => \$buffer
993 or die "unzip failed: $UnzipError\n";
995 To uncompress all files in the directory "/my/home" that match "*.txt.zip" and store the compressed data in the same directory
999 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
1001 unzip '</my/home/*.txt.zip>' => '</my/home/#1.txt>'
1002 or die "unzip failed: $UnzipError\n";
1004 and if you want to compress each file one at a time, this will do the trick
1008 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
1010 for my $input ( glob "/my/home/*.txt.zip" )
1012 my $output = $input;
1013 $output =~ s/.zip// ;
1014 unzip $input => $output
1015 or die "Error compressing '$input': $UnzipError\n";
1022 The format of the constructor for IO::Uncompress::Unzip is shown below
1024 my $z = new IO::Uncompress::Unzip $input [OPTS]
1025 or die "IO::Uncompress::Unzip failed: $UnzipError\n";
1027 Returns an C<IO::Uncompress::Unzip> object on success and undef on failure.
1028 The variable C<$UnzipError> will contain an error message on failure.
1030 If you are running Perl 5.005 or better the object, C<$z>, returned from
1031 IO::Uncompress::Unzip can be used exactly like an L<IO::File|IO::File> filehandle.
1032 This means that all normal input file operations can be carried out with
1033 C<$z>. For example, to read a line from a compressed file/buffer you can
1034 use either of these forms
1036 $line = $z->getline();
1039 The mandatory parameter C<$input> is used to determine the source of the
1040 compressed data. This parameter can take one of three forms.
1046 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
1047 file will be opened for reading and the compressed data will be read from it.
1051 If the C<$input> parameter is a filehandle, the compressed data will be
1053 The string '-' can be used as an alias for standard input.
1055 =item A scalar reference
1057 If C<$input> is a scalar reference, the compressed data will be read from
1062 =head2 Constructor Options
1064 The option names defined below are case insensitive and can be optionally
1065 prefixed by a '-'. So all of the following are valid
1072 OPTS is a combination of the following options:
1076 =item C<< AutoClose => 0|1 >>
1078 This option is only valid when the C<$input> parameter is a filehandle. If
1079 specified, and the value is true, it will result in the file being closed once
1080 either the C<close> method is called or the IO::Uncompress::Unzip object is
1083 This parameter defaults to 0.
1085 =item C<< MultiStream => 0|1 >>
1087 Treats the complete zip file/buffer as a single compressed data
1088 stream. When reading in multi-stream mode each member of the zip
1089 file/buffer will be uncompressed in turn until the end of the file/buffer
1092 This parameter defaults to 0.
1094 =item C<< Prime => $string >>
1096 This option will uncompress the contents of C<$string> before processing the
1099 This option can be useful when the compressed data is embedded in another
1100 file/data structure and it is not possible to work out where the compressed
1101 data begins without having to read the first few bytes. If this is the
1102 case, the uncompression can be I<primed> with these bytes using this
1105 =item C<< Transparent => 0|1 >>
1107 If this option is set and the input file/buffer is not compressed data,
1108 the module will allow reading of it anyway.
1110 In addition, if the input file/buffer does contain compressed data and
1111 there is non-compressed data immediately following it, setting this option
1112 will make this module treat the whole file/bufffer as a single data stream.
1114 This option defaults to 1.
1116 =item C<< BlockSize => $num >>
1118 When reading the compressed input data, IO::Uncompress::Unzip will read it in
1119 blocks of C<$num> bytes.
1121 This option defaults to 4096.
1123 =item C<< InputLength => $size >>
1125 When present this option will limit the number of compressed bytes read
1126 from the input file/buffer to C<$size>. This option can be used in the
1127 situation where there is useful data directly after the compressed data
1128 stream and you know beforehand the exact length of the compressed data
1131 This option is mostly used when reading from a filehandle, in which case
1132 the file pointer will be left pointing to the first byte directly after the
1133 compressed data stream.
1135 This option defaults to off.
1137 =item C<< Append => 0|1 >>
1139 This option controls what the C<read> method does with uncompressed data.
1141 If set to 1, all uncompressed data will be appended to the output parameter
1142 of the C<read> method.
1144 If set to 0, the contents of the output parameter of the C<read> method
1145 will be overwritten by the uncompressed data.
1149 =item C<< Strict => 0|1 >>
1151 This option controls whether the extra checks defined below are used when
1152 carrying out the decompression. When Strict is on, the extra tests are
1153 carried out, when Strict is off they are not.
1155 The default for this option is off.
1169 $status = $z->read($buffer)
1171 Reads a block of compressed data (the size the the compressed block is
1172 determined by the C<Buffer> option in the constructor), uncompresses it and
1173 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
1174 set in the constructor, the uncompressed data will be appended to the
1175 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
1177 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
1178 or a negative number on error.
1184 $status = $z->read($buffer, $length)
1185 $status = $z->read($buffer, $length, $offset)
1187 $status = read($z, $buffer, $length)
1188 $status = read($z, $buffer, $length, $offset)
1190 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
1192 The main difference between this form of the C<read> method and the
1193 previous one, is that this one will attempt to return I<exactly> C<$length>
1194 bytes. The only circumstances that this function will not is if end-of-file
1195 or an IO error is encountered.
1197 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
1198 or a negative number on error.
1204 $line = $z->getline()
1207 Reads a single line.
1209 This method fully supports the use of of the variable C<$/> (or
1210 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
1211 determine what constitutes an end of line. Paragraph mode, record mode and
1212 file slurp mode are all supported.
1220 Read a single character.
1226 $char = $z->ungetc($string)
1232 $status = $z->inflateSync()
1236 =head2 getHeaderInfo
1240 $hdr = $z->getHeaderInfo();
1241 @hdrs = $z->getHeaderInfo();
1243 This method returns either a hash reference (in scalar context) or a list
1244 or hash references (in array context) that contains information about each
1245 of the header fields in the compressed data stream(s).
1254 Returns the uncompressed file offset.
1263 Returns true if the end of the compressed input stream has been reached.
1267 $z->seek($position, $whence);
1268 seek($z, $position, $whence);
1270 Provides a sub-set of the C<seek> functionality, with the restriction
1271 that it is only legal to seek forward in the input file/buffer.
1272 It is a fatal error to attempt to seek backward.
1274 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1275 SEEK_CUR or SEEK_END.
1277 Returns 1 on success, 0 on failure.
1286 This is a noop provided for completeness.
1292 Returns true if the object currently refers to a opened file/buffer.
1296 my $prev = $z->autoflush()
1297 my $prev = $z->autoflush(EXPR)
1299 If the C<$z> object is associated with a file or a filehandle, this method
1300 returns the current autoflush setting for the underlying filehandle. If
1301 C<EXPR> is present, and is non-zero, it will enable flushing after every
1302 write/print operation.
1304 If C<$z> is associated with a buffer, this method has no effect and always
1307 B<Note> that the special variable C<$|> B<cannot> be used to set or
1308 retrieve the autoflush setting.
1310 =head2 input_line_number
1312 $z->input_line_number()
1313 $z->input_line_number(EXPR)
1315 Returns the current uncompressed line number. If C<EXPR> is present it has
1316 the effect of setting the line number. Note that setting the line number
1317 does not change the current position within the file/buffer being read.
1319 The contents of C<$/> are used to to determine what constitutes a line
1327 If the C<$z> object is associated with a file or a filehandle, C<fileno>
1328 will return the underlying file descriptor. Once the C<close> method is
1329 called C<fileno> will return C<undef>.
1331 If the C<$z> object is is associated with a buffer, this method will return
1339 Closes the output file/buffer.
1341 For most versions of Perl this method will be automatically invoked if
1342 the IO::Uncompress::Unzip object is destroyed (either explicitly or by the
1343 variable with the reference to the object going out of scope). The
1344 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1345 these cases, the C<close> method will be called automatically, but
1346 not until global destruction of all live objects when the program is
1349 Therefore, if you want your scripts to be able to run on all versions
1350 of Perl, you should call C<close> explicitly and not rely on automatic
1353 Returns true on success, otherwise 0.
1355 If the C<AutoClose> option has been enabled when the IO::Uncompress::Unzip
1356 object was created, and the object is associated with a file, the
1357 underlying file will also be closed.
1363 my $status = $z->nextStream();
1365 Skips to the next compressed data stream in the input file/buffer. If a new
1366 compressed data stream is found, the eof marker will be cleared and C<$.>
1369 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
1370 error was encountered.
1376 my $data = $z->trailingData();
1378 Returns the data, if any, that is present immediately after the compressed
1379 data stream once uncompression is complete. It only makes sense to call
1380 this method once the end of the compressed data stream has been
1383 This option can be used when there is useful information immediately
1384 following the compressed data stream, and you don't know the length of the
1385 compressed data stream.
1387 If the input is a buffer, C<trailingData> will return everything from the
1388 end of the compressed data stream to the end of the buffer.
1390 If the input is a filehandle, C<trailingData> will return the data that is
1391 left in the filehandle input buffer once the end of the compressed data
1392 stream has been reached. You can then use the filehandle to read the rest
1395 Don't bother using C<trailingData> if the input is a filename.
1397 If you know the length of the compressed data stream before you start
1398 uncompressing, you can avoid having to use C<trailingData> by setting the
1399 C<InputLength> option in the constructor.
1403 No symbolic constants are required by this IO::Uncompress::Unzip at present.
1409 Imports C<unzip> and C<$UnzipError>.
1412 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
1418 =head2 Working with Net::FTP
1420 See L<IO::Uncompress::Unzip::FAQ|IO::Uncompress::Unzip::FAQ/"Compressed files and Net::FTP">
1424 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1426 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1428 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1429 L<Archive::Tar|Archive::Tar>,
1430 L<IO::Zlib|IO::Zlib>
1432 For RFC 1950, 1951 and 1952 see
1433 F<http://www.faqs.org/rfcs/rfc1950.html>,
1434 F<http://www.faqs.org/rfcs/rfc1951.html> and
1435 F<http://www.faqs.org/rfcs/rfc1952.html>
1437 The I<zlib> compression library was written by Jean-loup Gailly
1438 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1440 The primary site for the I<zlib> compression library is
1441 F<http://www.zlib.org>.
1443 The primary site for gzip is F<http://www.gzip.org>.
1447 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1449 =head1 MODIFICATION HISTORY
1451 See the Changes file.
1453 =head1 COPYRIGHT AND LICENSE
1455 Copyright (c) 2005-2009 Paul Marquess. All rights reserved.
1457 This program is free software; you can redistribute it and/or
1458 modify it under the same terms as Perl itself.