1 package IO::Uncompress::Unzip;
11 use IO::Uncompress::RawInflate ;
12 use IO::Compress::Base::Common qw(:Status createSelfTiedObject);
13 use IO::Uncompress::Adapter::Identity;
14 use IO::Compress::Zlib::Extra;
15 use IO::Compress::Zip::Constants;
17 use Compress::Raw::Zlib qw(crc32) ;
21 eval { require IO::Uncompress::Adapter::Bunzip2 ;
22 import IO::Uncompress::Adapter::Bunzip2 } ;
28 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $UnzipError, %headerLookup);
30 $VERSION = '2.000_13';
33 @ISA = qw(Exporter IO::Uncompress::RawInflate);
34 @EXPORT_OK = qw( $UnzipError unzip );
35 %EXPORT_TAGS = %IO::Uncompress::RawInflate::EXPORT_TAGS ;
36 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
37 Exporter::export_ok_tags('all');
40 ZIP_CENTRAL_HDR_SIG, \&skipCentralDirectory,
41 ZIP_END_CENTRAL_HDR_SIG, \&skipEndCentralDirectory,
42 ZIP64_END_CENTRAL_REC_HDR_SIG, \&skipCentralDirectory64Rec,
43 ZIP64_END_CENTRAL_LOC_HDR_SIG, \&skipCentralDirectory64Loc,
44 ZIP64_ARCHIVE_EXTRA_SIG, \&skipArchiveExtra,
45 ZIP64_DIGITAL_SIGNATURE_SIG, \&skipDigitalSignature,
51 my $obj = createSelfTiedObject($class, \$UnzipError);
52 $obj->_create(undef, 0, @_);
57 my $obj = createSelfTiedObject(undef, \$UnzipError);
58 return $obj->_inf(@_) ;
63 use IO::Compress::Base::Common qw(:Parse);
68 'Name' => [1, 1, Parse_any, undef],
70 # 'Streaming' => [1, 1, Parse_boolean, 1],
79 # unzip always needs crc32
80 $got->value('CRC32' => 1);
82 *$self->{UnzipData}{Name} = $got->value('Name');
93 $self->smartReadExact(\$magic, 4);
95 *$self->{HeaderPending} = $magic ;
97 return $self->HeaderError("Minimum header size is " .
99 if length $magic != 4 ;
101 return $self->HeaderError("Bad Magic")
102 if ! _isZipMagic($magic) ;
104 *$self->{Type} = 'zip';
116 my $name = *$self->{UnzipData}{Name} ;
117 my $hdr = $self->_readZipHeader($magic) ;
121 if (! defined $name || $hdr->{Name} eq $name)
128 if (*$self->{ZipData}{Streaming}) {
133 my $status = $self->smartRead(\$b, 1024 * 16);
139 $status = *$self->{Uncomp}->uncompr(\$b, \$temp_buf, 0, $out);
141 return $self->saveErrorString(undef, *$self->{Uncomp}{Error},
142 *$self->{Uncomp}{ErrorNo})
143 if $self->saveStatus($status) == STATUS_ERROR;
145 if ($status == STATUS_ENDSTREAM) {
146 *$self->{Uncomp}->reset();
147 $self->pushBack($b) ;
153 $self->smartReadExact(\$buffer, $hdr->{TrailerLength})
154 or return $self->saveErrorString(undef, "Truncated file");
157 my $c = $hdr->{CompressedLength}->get32bit();
158 $self->smartReadExact(\$buffer, $c)
159 or return $self->saveErrorString(undef, "Truncated file");
163 $self->chkTrailer($buffer) == STATUS_OK
164 or return $self->saveErrorString(undef, "Truncated file");
166 $hdr = $self->_readFullZipHeader();
168 return $self->saveErrorString(undef, "Cannot find '$name'")
169 if $self->smartEof();
180 my ($sig, $CRC32, $cSize, $uSize) ;
181 my ($cSizeHi, $uSizeHi) = (0, 0);
182 if (*$self->{ZipData}{Streaming}) {
183 $sig = unpack ("V", substr($trailer, 0, 4));
184 $CRC32 = unpack ("V", substr($trailer, 4, 4));
186 if (*$self->{ZipData}{Zip64} ) {
187 $cSize = U64::newUnpack_V64 substr($trailer, 8, 8);
188 $uSize = U64::newUnpack_V64 substr($trailer, 16, 8);
191 $cSize = U64::newUnpack_V32 substr($trailer, 8, 4);
192 $uSize = U64::newUnpack_V32 substr($trailer, 12, 4);
195 return $self->TrailerError("Data Descriptor signature, got $sig")
196 if $sig != ZIP_DATA_HDR_SIG;
199 ($CRC32, $cSize, $uSize) =
200 (*$self->{ZipData}{Crc32},
201 *$self->{ZipData}{CompressedLen},
202 *$self->{ZipData}{UnCompressedLen});
205 if (*$self->{Strict}) {
206 return $self->TrailerError("CRC mismatch")
207 if $CRC32 != *$self->{ZipData}{CRC32} ;
209 return $self->TrailerError("CSIZE mismatch.")
210 if ! $cSize->equal(*$self->{CompSize});
212 return $self->TrailerError("USIZE mismatch.")
213 if ! $uSize->equal(*$self->{UnCompSize});
216 my $reachedEnd = STATUS_ERROR ;
217 # check for central directory or end of central directory
221 my $got = $self->smartRead(\$magic, 4);
223 return $self->saveErrorString(STATUS_ERROR, "Truncated file")
224 if $got != 4 && *$self->{Strict};
230 return STATUS_ERROR ;
233 $self->pushBack($magic) ;
237 my $sig = unpack("V", $magic) ;
239 if ($headerLookup{$sig})
241 if ($headerLookup{$sig}($self, $magic) != STATUS_OK ) {
242 if (*$self->{Strict}) {
243 return STATUS_ERROR ;
251 if ($sig == ZIP_END_CENTRAL_HDR_SIG)
257 elsif ($sig == ZIP_LOCAL_HDR_SIG)
259 $self->pushBack($magic) ;
265 $self->pushBack($magic) ;
273 sub skipCentralDirectory
279 $self->smartReadExact(\$buffer, 46 - 4)
280 or return $self->TrailerError("Minimum header size is " .
283 my $keep = $magic . $buffer ;
284 *$self->{HeaderPending} = $keep ;
286 #my $versionMadeBy = unpack ("v", substr($buffer, 4-4, 2));
287 #my $extractVersion = unpack ("v", substr($buffer, 6-4, 2));
288 #my $gpFlag = unpack ("v", substr($buffer, 8-4, 2));
289 #my $compressedMethod = unpack ("v", substr($buffer, 10-4, 2));
290 #my $lastModTime = unpack ("V", substr($buffer, 12-4, 4));
291 #my $crc32 = unpack ("V", substr($buffer, 16-4, 4));
292 my $compressedLength = unpack ("V", substr($buffer, 20-4, 4));
293 my $uncompressedLength = unpack ("V", substr($buffer, 24-4, 4));
294 my $filename_length = unpack ("v", substr($buffer, 28-4, 2));
295 my $extra_length = unpack ("v", substr($buffer, 30-4, 2));
296 my $comment_length = unpack ("v", substr($buffer, 32-4, 2));
297 #my $disk_start = unpack ("v", substr($buffer, 34-4, 2));
298 #my $int_file_attrib = unpack ("v", substr($buffer, 36-4, 2));
299 #my $ext_file_attrib = unpack ("V", substr($buffer, 38-4, 2));
300 #my $lcl_hdr_offset = unpack ("V", substr($buffer, 42-4, 2));
306 if ($filename_length)
308 $self->smartReadExact(\$filename, $filename_length)
309 or return $self->TruncatedTrailer("filename");
315 $self->smartReadExact(\$extraField, $extra_length)
316 or return $self->TruncatedTrailer("extra");
317 $keep .= $extraField ;
322 $self->smartReadExact(\$comment, $comment_length)
323 or return $self->TruncatedTrailer("comment");
336 $self->smartReadExact(\$buffer, 4)
337 or return $self->TrailerError("Minimum header size is " .
340 my $keep = $magic . $buffer ;
342 my $size = unpack ("V", $buffer);
344 $self->smartReadExact(\$buffer, $size)
345 or return $self->TrailerError("Minimum header size is " .
349 *$self->{HeaderPending} = $keep ;
355 sub skipCentralDirectory64Rec
361 $self->smartReadExact(\$buffer, 8)
362 or return $self->TrailerError("Minimum header size is " .
365 my $keep = $magic . $buffer ;
367 my ($sizeLo, $sizeHi) = unpack ("V V", $buffer);
369 # TODO - take SizeHi into account
370 $self->smartReadExact(\$buffer, $sizeLo)
371 or return $self->TrailerError("Minimum header size is " .
372 $sizeLo . " bytes") ;
375 *$self->{HeaderPending} = $keep ;
377 #my $versionMadeBy = unpack ("v", substr($buffer, 0, 2));
378 #my $extractVersion = unpack ("v", substr($buffer, 2, 2));
379 #my $diskNumber = unpack ("V", substr($buffer, 4, 4));
380 #my $cntrlDirDiskNo = unpack ("V", substr($buffer, 8, 4));
381 #my $entriesInThisCD = unpack ("V V", substr($buffer, 12, 8));
382 #my $entriesInCD = unpack ("V V", substr($buffer, 20, 8));
383 #my $sizeOfCD = unpack ("V V", substr($buffer, 28, 8));
384 #my $offsetToCD = unpack ("V V", substr($buffer, 36, 8));
389 sub skipCentralDirectory64Loc
395 $self->smartReadExact(\$buffer, 20 - 4)
396 or return $self->TrailerError("Minimum header size is " .
399 my $keep = $magic . $buffer ;
400 *$self->{HeaderPending} = $keep ;
402 #my $startCdDisk = unpack ("V", substr($buffer, 4-4, 4));
403 #my $offsetToCD = unpack ("V V", substr($buffer, 8-4, 8));
404 #my $diskCount = unpack ("V", substr($buffer, 16-4, 4));
409 sub skipEndCentralDirectory
415 $self->smartReadExact(\$buffer, 22 - 4)
416 or return $self->TrailerError("Minimum header size is " .
419 my $keep = $magic . $buffer ;
420 *$self->{HeaderPending} = $keep ;
422 #my $diskNumber = unpack ("v", substr($buffer, 4-4, 2));
423 #my $cntrlDirDiskNo = unpack ("v", substr($buffer, 6-4, 2));
424 #my $entriesInThisCD = unpack ("v", substr($buffer, 8-4, 2));
425 #my $entriesInCD = unpack ("v", substr($buffer, 10-4, 2));
426 #my $sizeOfCD = unpack ("V", substr($buffer, 12-4, 2));
427 #my $offsetToCD = unpack ("V", substr($buffer, 16-4, 2));
428 my $comment_length = unpack ("v", substr($buffer, 20-4, 2));
434 $self->smartReadExact(\$comment, $comment_length)
435 or return $self->TruncatedTrailer("comment");
446 return 0 if length $buffer < 4 ;
447 my $sig = unpack("V", $buffer) ;
448 return $sig == ZIP_LOCAL_HDR_SIG ;
452 sub _readFullZipHeader($)
457 $self->smartReadExact(\$magic, 4);
459 *$self->{HeaderPending} = $magic ;
461 return $self->HeaderError("Minimum header size is " .
463 if length $magic != 4 ;
466 return $self->HeaderError("Bad Magic")
467 if ! _isZipMagic($magic) ;
469 my $status = $self->_readZipHeader($magic);
470 delete *$self->{Transparent} if ! defined $status ;
474 sub _readZipHeader($)
476 my ($self, $magic) = @_ ;
480 $self->smartReadExact(\$buffer, 30 - 4)
481 or return $self->HeaderError("Minimum header size is " .
484 my $keep = $magic . $buffer ;
485 *$self->{HeaderPending} = $keep ;
487 my $extractVersion = unpack ("v", substr($buffer, 4-4, 2));
488 my $gpFlag = unpack ("v", substr($buffer, 6-4, 2));
489 my $compressedMethod = unpack ("v", substr($buffer, 8-4, 2));
490 my $lastModTime = unpack ("V", substr($buffer, 10-4, 4));
491 my $crc32 = unpack ("V", substr($buffer, 14-4, 4));
492 my $compressedLength = new U64 unpack ("V", substr($buffer, 18-4, 4));
493 my $uncompressedLength = new U64 unpack ("V", substr($buffer, 22-4, 4));
494 my $filename_length = unpack ("v", substr($buffer, 26-4, 2));
495 my $extra_length = unpack ("v", substr($buffer, 28-4, 2));
500 my $streamingMode = ($gpFlag & ZIP_GP_FLAG_STREAMING_MASK) ? 1 : 0 ;
502 return $self->HeaderError("Streamed Stored content not supported")
503 if $streamingMode && $compressedMethod == 0 ;
505 *$self->{ZipData}{Streaming} = $streamingMode;
508 if ($filename_length)
510 $self->smartReadExact(\$filename, $filename_length)
511 or return $self->TruncatedHeader("Filename");
519 $self->smartReadExact(\$extraField, $extra_length)
520 or return $self->TruncatedHeader("Extra Field");
522 my $bad = IO::Compress::Zlib::Extra::parseRawExtra($extraField,
524 return $self->HeaderError($bad)
527 $keep .= $extraField ;
532 $Extra{$_->[0]} = \$_->[1];
535 if (defined $Extra{ZIP_EXTRA_ID_ZIP64()})
539 my $buff = ${ $Extra{ZIP_EXTRA_ID_ZIP64()} };
541 $uncompressedLength = U64::newUnpack_V64 substr($buff, 0, 8);
542 $compressedLength = U64::newUnpack_V64 substr($buff, 8, 8);
543 #my $cheaderOffset = U64::newUnpack_V64 substr($buff, 16, 8);
544 #my $diskNumber = unpack ("V", substr($buff, 24, 4));
548 *$self->{ZipData}{Zip64} = $zip64;
550 if (! $streamingMode) {
551 *$self->{ZipData}{Streaming} = 0;
552 *$self->{ZipData}{Crc32} = $crc32;
553 *$self->{ZipData}{CompressedLen} = $compressedLength;
554 *$self->{ZipData}{UnCompressedLen} = $uncompressedLength;
555 *$self->{CompressedInputLengthRemaining} =
556 *$self->{CompressedInputLength} = $compressedLength->get32bit();
559 *$self->{ZipData}{Method} = $compressedMethod;
560 if ($compressedMethod == ZIP_CM_DEFLATE)
562 *$self->{Type} = 'zip-deflate';
564 elsif ($compressedMethod == ZIP_CM_BZIP2)
566 #if (! defined $IO::Uncompress::Adapter::Bunzip2::VERSION)
568 *$self->{Type} = 'zip-bzip2';
570 my $obj = IO::Uncompress::Adapter::Bunzip2::mkUncompObject(
573 *$self->{Uncomp} = $obj;
574 *$self->{ZipData}{CRC32} = crc32(undef);
577 elsif ($compressedMethod == ZIP_CM_STORE)
579 # TODO -- add support for reading uncompressed
581 *$self->{Type} = 'zip-stored';
583 my $obj = IO::Uncompress::Adapter::Identity::mkUncompObject(# $got->value('CRC32'),
584 # $got->value('ADLER32'),
587 *$self->{Uncomp} = $obj;
592 return $self->HeaderError("Unsupported Compression format $compressedMethod");
597 'FingerprintLength' => 4,
598 #'HeaderLength' => $compressedMethod == 8 ? length $keep : 0,
599 'HeaderLength' => length $keep,
601 'TrailerLength' => ! $streamingMode ? 0 : $zip64 ? 24 : 16,
603 'CompressedLength' => $compressedLength ,
604 'UncompressedLength' => $uncompressedLength ,
607 'Time' => _dosToUnixTime($lastModTime),
608 'Stream' => $streamingMode,
610 'MethodID' => $compressedMethod,
611 'MethodName' => $compressedMethod == ZIP_CM_DEFLATE
613 : $compressedMethod == ZIP_CM_BZIP2
615 : $compressedMethod == ZIP_CM_STORE
619 # 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0,
620 # 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0,
621 # 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0,
622 # 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0,
623 # 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0,
624 # 'Comment' => $comment,
626 # 'OsName' => defined $GZIP_OS_Names{$os}
627 # ? $GZIP_OS_Names{$os} : "Unknown",
628 # 'HeaderCRC' => $HeaderCRC,
630 # 'ExtraFlags' => $xfl,
631 'ExtraFieldRaw' => $extraField,
632 'ExtraField' => [ @EXTRA ],
638 sub filterUncompressed
642 if (*$self->{ZipData}{Method} == 12) {
643 *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32});
646 *$self->{ZipData}{CRC32} = *$self->{Uncomp}->crc32() ;
654 #use Time::Local 'timelocal_nocheck';
655 use Time::Local 'timelocal';
659 my $year = ( ( $dt >> 25 ) & 0x7f ) + 80;
660 my $mon = ( ( $dt >> 21 ) & 0x0f ) - 1;
661 my $mday = ( ( $dt >> 16 ) & 0x1f );
663 my $hour = ( ( $dt >> 11 ) & 0x1f );
664 my $min = ( ( $dt >> 5 ) & 0x3f );
665 my $sec = ( ( $dt << 1 ) & 0x3e );
669 eval { timelocal( $sec, $min, $hour, $mday, $mon, $year ); };
685 IO::Uncompress::Unzip - Read zip files/buffers
691 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
693 my $status = unzip $input => $output [,OPTS]
694 or die "unzip failed: $UnzipError\n";
696 my $z = new IO::Uncompress::Unzip $input [OPTS]
697 or die "unzip failed: $UnzipError\n";
699 $status = $z->read($buffer)
700 $status = $z->read($buffer, $length)
701 $status = $z->read($buffer, $length, $offset)
702 $line = $z->getline()
707 $status = $z->inflateSync()
709 $data = $z->trailingData()
710 $status = $z->nextStream()
711 $data = $z->getHeaderInfo()
713 $z->seek($position, $whence)
725 read($z, $buffer, $length);
726 read($z, $buffer, $length, $offset);
728 seek($z, $position, $whence)
739 B<WARNING -- This is a Beta release>.
743 =item * DO NOT use in production code.
745 =item * The documentation is incomplete in places.
747 =item * Parts of the interface defined here are tentative.
749 =item * Please report any problems you find.
757 This module provides a Perl interface that allows the reading of
760 For writing zip files/buffers, see the companion module IO::Compress::Zip.
767 =head1 Functional Interface
769 A top-level function, C<unzip>, is provided to carry out
770 "one-shot" uncompression between buffers and/or files. For finer
771 control over the uncompression process, see the L</"OO Interface">
774 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
776 unzip $input => $output [,OPTS]
777 or die "unzip failed: $UnzipError\n";
781 The functional interface needs Perl5.005 or better.
784 =head2 unzip $input => $output [, OPTS]
787 C<unzip> expects at least two parameters, C<$input> and C<$output>.
789 =head3 The C<$input> parameter
791 The parameter, C<$input>, is used to define the source of
794 It can take one of the following forms:
800 If the C<$input> parameter is a simple scalar, it is assumed to be a
801 filename. This file will be opened for reading and the input data
802 will be read from it.
806 If the C<$input> parameter is a filehandle, the input data will be
808 The string '-' can be used as an alias for standard input.
810 =item A scalar reference
812 If C<$input> is a scalar reference, the input data will be read
815 =item An array reference
817 If C<$input> is an array reference, each element in the array must be a
820 The input data will be read from each file in turn.
822 The complete array will be walked to ensure that it only
823 contains valid filenames before any data is uncompressed.
827 =item An Input FileGlob string
829 If C<$input> is a string that is delimited by the characters "<" and ">"
830 C<unzip> will assume that it is an I<input fileglob string>. The
831 input is the list of files that match the fileglob.
833 If the fileglob does not match any files ...
835 See L<File::GlobMapper|File::GlobMapper> for more details.
840 If the C<$input> parameter is any other type, C<undef> will be returned.
844 =head3 The C<$output> parameter
846 The parameter C<$output> is used to control the destination of the
847 uncompressed data. This parameter can take one of these forms.
853 If the C<$output> parameter is a simple scalar, it is assumed to be a
854 filename. This file will be opened for writing and the uncompressed
855 data will be written to it.
859 If the C<$output> parameter is a filehandle, the uncompressed data
860 will be written to it.
861 The string '-' can be used as an alias for standard output.
864 =item A scalar reference
866 If C<$output> is a scalar reference, the uncompressed data will be
867 stored in C<$$output>.
871 =item An Array Reference
873 If C<$output> is an array reference, the uncompressed data will be
874 pushed onto the array.
876 =item An Output FileGlob
878 If C<$output> is a string that is delimited by the characters "<" and ">"
879 C<unzip> will assume that it is an I<output fileglob string>. The
880 output is the list of files that match the fileglob.
882 When C<$output> is an fileglob string, C<$input> must also be a fileglob
883 string. Anything else is an error.
887 If the C<$output> parameter is any other type, C<undef> will be returned.
894 When C<$input> maps to multiple compressed files/buffers and C<$output> is
895 a single file/buffer, after uncompression C<$output> will contain a
896 concatenation of all the uncompressed data from each of the input
903 =head2 Optional Parameters
905 Unless specified below, the optional parameters for C<unzip>,
906 C<OPTS>, are the same as those used with the OO interface defined in the
907 L</"Constructor Options"> section below.
911 =item C<< AutoClose => 0|1 >>
913 This option applies to any input or output data streams to
914 C<unzip> that are filehandles.
916 If C<AutoClose> is specified, and the value is true, it will result in all
917 input and/or output filehandles being closed once C<unzip> has
920 This parameter defaults to 0.
923 =item C<< BinModeOut => 0|1 >>
925 When writing to a file or filehandle, set C<binmode> before writing to the
934 =item C<< Append => 0|1 >>
938 =item C<< MultiStream => 0|1 >>
940 If the input file/buffer contains multiple compressed data streams, this
941 option will uncompress the whole lot as a single data stream.
954 To read the contents of the file C<file1.txt.zip> and write the
955 compressed data to the file C<file1.txt>.
959 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
961 my $input = "file1.txt.zip";
962 my $output = "file1.txt";
963 unzip $input => $output
964 or die "unzip failed: $UnzipError\n";
967 To read from an existing Perl filehandle, C<$input>, and write the
968 uncompressed data to a buffer, C<$buffer>.
972 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
975 my $input = new IO::File "<file1.txt.zip"
976 or die "Cannot open 'file1.txt.zip': $!\n" ;
978 unzip $input => \$buffer
979 or die "unzip failed: $UnzipError\n";
981 To uncompress all files in the directory "/my/home" that match "*.txt.zip" and store the compressed data in the same directory
985 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
987 unzip '</my/home/*.txt.zip>' => '</my/home/#1.txt>'
988 or die "unzip failed: $UnzipError\n";
990 and if you want to compress each file one at a time, this will do the trick
994 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
996 for my $input ( glob "/my/home/*.txt.zip" )
999 $output =~ s/.zip// ;
1000 unzip $input => $output
1001 or die "Error compressing '$input': $UnzipError\n";
1008 The format of the constructor for IO::Uncompress::Unzip is shown below
1011 my $z = new IO::Uncompress::Unzip $input [OPTS]
1012 or die "IO::Uncompress::Unzip failed: $UnzipError\n";
1014 Returns an C<IO::Uncompress::Unzip> object on success and undef on failure.
1015 The variable C<$UnzipError> will contain an error message on failure.
1017 If you are running Perl 5.005 or better the object, C<$z>, returned from
1018 IO::Uncompress::Unzip can be used exactly like an L<IO::File|IO::File> filehandle.
1019 This means that all normal input file operations can be carried out with
1020 C<$z>. For example, to read a line from a compressed file/buffer you can
1021 use either of these forms
1023 $line = $z->getline();
1026 The mandatory parameter C<$input> is used to determine the source of the
1027 compressed data. This parameter can take one of three forms.
1033 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
1034 file will be opened for reading and the compressed data will be read from it.
1038 If the C<$input> parameter is a filehandle, the compressed data will be
1040 The string '-' can be used as an alias for standard input.
1043 =item A scalar reference
1045 If C<$input> is a scalar reference, the compressed data will be read from
1050 =head2 Constructor Options
1053 The option names defined below are case insensitive and can be optionally
1054 prefixed by a '-'. So all of the following are valid
1061 OPTS is a combination of the following options:
1065 =item C<< AutoClose => 0|1 >>
1067 This option is only valid when the C<$input> parameter is a filehandle. If
1068 specified, and the value is true, it will result in the file being closed once
1069 either the C<close> method is called or the IO::Uncompress::Unzip object is
1072 This parameter defaults to 0.
1074 =item C<< MultiStream => 0|1 >>
1078 Treats the complete zip file/buffer as a single compressed data
1079 stream. When reading in multi-stream mode each member of the zip
1080 file/buffer will be uncompressed in turn until the end of the file/buffer
1083 This parameter defaults to 0.
1086 =item C<< Prime => $string >>
1088 This option will uncompress the contents of C<$string> before processing the
1091 This option can be useful when the compressed data is embedded in another
1092 file/data structure and it is not possible to work out where the compressed
1093 data begins without having to read the first few bytes. If this is the
1094 case, the uncompression can be I<primed> with these bytes using this
1097 =item C<< Transparent => 0|1 >>
1099 If this option is set and the input file or buffer is not compressed data,
1100 the module will allow reading of it anyway.
1102 This option defaults to 1.
1104 =item C<< BlockSize => $num >>
1106 When reading the compressed input data, IO::Uncompress::Unzip will read it in
1107 blocks of C<$num> bytes.
1109 This option defaults to 4096.
1111 =item C<< InputLength => $size >>
1113 When present this option will limit the number of compressed bytes read
1114 from the input file/buffer to C<$size>. This option can be used in the
1115 situation where there is useful data directly after the compressed data
1116 stream and you know beforehand the exact length of the compressed data
1119 This option is mostly used when reading from a filehandle, in which case
1120 the file pointer will be left pointing to the first byte directly after the
1121 compressed data stream.
1125 This option defaults to off.
1127 =item C<< Append => 0|1 >>
1129 This option controls what the C<read> method does with uncompressed data.
1131 If set to 1, all uncompressed data will be appended to the output parameter
1132 of the C<read> method.
1134 If set to 0, the contents of the output parameter of the C<read> method
1135 will be overwritten by the uncompressed data.
1139 =item C<< Strict => 0|1 >>
1143 This option controls whether the extra checks defined below are used when
1144 carrying out the decompression. When Strict is on, the extra tests are
1145 carried out, when Strict is off they are not.
1147 The default for this option is off.
1174 $status = $z->read($buffer)
1176 Reads a block of compressed data (the size the the compressed block is
1177 determined by the C<Buffer> option in the constructor), uncompresses it and
1178 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
1179 set in the constructor, the uncompressed data will be appended to the
1180 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
1182 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
1183 or a negative number on error.
1189 $status = $z->read($buffer, $length)
1190 $status = $z->read($buffer, $length, $offset)
1192 $status = read($z, $buffer, $length)
1193 $status = read($z, $buffer, $length, $offset)
1195 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
1197 The main difference between this form of the C<read> method and the
1198 previous one, is that this one will attempt to return I<exactly> C<$length>
1199 bytes. The only circumstances that this function will not is if end-of-file
1200 or an IO error is encountered.
1202 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
1203 or a negative number on error.
1210 $line = $z->getline()
1213 Reads a single line.
1215 This method fully supports the use of of the variable C<$/>
1216 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
1217 determine what constitutes an end of line. Both paragraph mode and file
1218 slurp mode are supported.
1227 Read a single character.
1233 $char = $z->ungetc($string)
1241 $status = $z->inflateSync()
1246 =head2 getHeaderInfo
1250 $hdr = $z->getHeaderInfo();
1251 @hdrs = $z->getHeaderInfo();
1253 This method returns either a hash reference (in scalar context) or a list
1254 or hash references (in array context) that contains information about each
1255 of the header fields in the compressed data stream(s).
1267 Returns the uncompressed file offset.
1278 Returns true if the end of the compressed input stream has been reached.
1284 $z->seek($position, $whence);
1285 seek($z, $position, $whence);
1290 Provides a sub-set of the C<seek> functionality, with the restriction
1291 that it is only legal to seek forward in the input file/buffer.
1292 It is a fatal error to attempt to seek backward.
1296 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1297 SEEK_CUR or SEEK_END.
1299 Returns 1 on success, 0 on failure.
1308 This is a noop provided for completeness.
1314 Returns true if the object currently refers to a opened file/buffer.
1318 my $prev = $z->autoflush()
1319 my $prev = $z->autoflush(EXPR)
1321 If the C<$z> object is associated with a file or a filehandle, this method
1322 returns the current autoflush setting for the underlying filehandle. If
1323 C<EXPR> is present, and is non-zero, it will enable flushing after every
1324 write/print operation.
1326 If C<$z> is associated with a buffer, this method has no effect and always
1329 B<Note> that the special variable C<$|> B<cannot> be used to set or
1330 retrieve the autoflush setting.
1332 =head2 input_line_number
1334 $z->input_line_number()
1335 $z->input_line_number(EXPR)
1339 Returns the current uncompressed line number. If C<EXPR> is present it has
1340 the effect of setting the line number. Note that setting the line number
1341 does not change the current position within the file/buffer being read.
1343 The contents of C<$/> are used to to determine what constitutes a line
1353 If the C<$z> object is associated with a file or a filehandle, this method
1354 will return the underlying file descriptor.
1356 If the C<$z> object is is associated with a buffer, this method will
1366 Closes the output file/buffer.
1370 For most versions of Perl this method will be automatically invoked if
1371 the IO::Uncompress::Unzip object is destroyed (either explicitly or by the
1372 variable with the reference to the object going out of scope). The
1373 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1374 these cases, the C<close> method will be called automatically, but
1375 not until global destruction of all live objects when the program is
1378 Therefore, if you want your scripts to be able to run on all versions
1379 of Perl, you should call C<close> explicitly and not rely on automatic
1382 Returns true on success, otherwise 0.
1384 If the C<AutoClose> option has been enabled when the IO::Uncompress::Unzip
1385 object was created, and the object is associated with a file, the
1386 underlying file will also be closed.
1395 my $status = $z->nextStream();
1397 Skips to the next compressed data stream in the input file/buffer. If a new
1398 compressed data stream is found, the eof marker will be cleared, C<$.> will
1401 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
1402 error was encountered.
1408 my $data = $z->trailingData();
1410 Returns any data that
1414 No symbolic constants are required by this IO::Uncompress::Unzip at present.
1420 Imports C<unzip> and C<$UnzipError>.
1423 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
1434 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::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1436 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1438 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1439 L<Archive::Tar|Archive::Tar>,
1440 L<IO::Zlib|IO::Zlib>
1443 For RFC 1950, 1951 and 1952 see
1444 F<http://www.faqs.org/rfcs/rfc1950.html>,
1445 F<http://www.faqs.org/rfcs/rfc1951.html> and
1446 F<http://www.faqs.org/rfcs/rfc1952.html>
1448 The I<zlib> compression library was written by Jean-loup Gailly
1449 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1451 The primary site for the I<zlib> compression library is
1452 F<http://www.zlib.org>.
1454 The primary site for gzip is F<http://www.gzip.org>.
1461 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1465 =head1 MODIFICATION HISTORY
1467 See the Changes file.
1469 =head1 COPYRIGHT AND LICENSE
1471 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1473 This program is free software; you can redistribute it and/or
1474 modify it under the same terms as Perl itself.