1 package IO::Uncompress::AnyInflate ;
3 # for RFC1950, RFC1951 or RFC1952
9 use IO::Compress::Base::Common 2.003 qw(createSelfTiedObject);
11 use IO::Uncompress::Adapter::Inflate 2.003 ();
14 use IO::Uncompress::Base 2.003 ;
15 use IO::Uncompress::Gunzip 2.003 ;
16 use IO::Uncompress::Inflate 2.003 ;
17 use IO::Uncompress::RawInflate 2.003 ;
18 use IO::Uncompress::Unzip 2.003 ;
22 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyInflateError);
25 $AnyInflateError = '';
27 @ISA = qw( Exporter IO::Uncompress::Base );
28 @EXPORT_OK = qw( $AnyInflateError anyinflate ) ;
29 %EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
30 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
31 Exporter::export_ok_tags('all');
33 # TODO - allow the user to pick a set of the three formats to allow
34 # or just assume want to auto-detect any of the three formats.
39 my $obj = createSelfTiedObject($class, \$AnyInflateError);
40 $obj->_create(undef, 0, @_);
45 my $obj = createSelfTiedObject(undef, \$AnyInflateError);
46 return $obj->_inf(@_) ;
51 use IO::Compress::Base::Common 2.003 qw(:Parse);
52 return ( 'RawInflate' => [1, 1, Parse_boolean, 0] ) ;
60 # any always needs both crc32 and adler32
61 $got->value('CRC32' => 1);
62 $got->value('ADLER32' => 1);
73 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
75 return $self->saveErrorString(undef, $errstr, $errno)
78 *$self->{Uncomp} = $obj;
80 my @possible = qw( Inflate Gunzip Unzip );
81 unshift @possible, 'RawInflate'
82 if 1 || $got->value('RawInflate');
84 my $magic = $self->ckMagic( @possible );
87 *$self->{Info} = $self->readHeader($magic)
103 my $keep = ref $self ;
104 for my $class ( map { "IO::Uncompress::$_" } @names)
106 bless $self => $class;
107 my $magic = $self->ckMagic();
111 #bless $self => $class;
115 $self->pushBack(*$self->{HeaderPending}) ;
116 *$self->{HeaderPending} = '' ;
119 bless $self => $keep;
131 IO::Uncompress::AnyInflate - Uncompress zlib-based (zip, gzip) file/buffer
136 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
138 my $status = anyinflate $input => $output [,OPTS]
139 or die "anyinflate failed: $AnyInflateError\n";
141 my $z = new IO::Uncompress::AnyInflate $input [OPTS]
142 or die "anyinflate failed: $AnyInflateError\n";
144 $status = $z->read($buffer)
145 $status = $z->read($buffer, $length)
146 $status = $z->read($buffer, $length, $offset)
147 $line = $z->getline()
152 $status = $z->inflateSync()
154 $data = $z->trailingData()
155 $status = $z->nextStream()
156 $data = $z->getHeaderInfo()
158 $z->seek($position, $whence)
170 read($z, $buffer, $length);
171 read($z, $buffer, $length, $offset);
173 seek($z, $position, $whence)
183 This module provides a Perl interface that allows the reading of
184 files/buffers that have been compressed in a number of formats that use the
185 zlib compression library.
187 The formats supported are
193 =item RFC 1951 (optionally)
195 =item gzip (RFC 1952)
201 The module will auto-detect which, if any, of the supported
202 compression formats is being used.
208 =head1 Functional Interface
210 A top-level function, C<anyinflate>, is provided to carry out
211 "one-shot" uncompression between buffers and/or files. For finer
212 control over the uncompression process, see the L</"OO Interface">
215 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
217 anyinflate $input => $output [,OPTS]
218 or die "anyinflate failed: $AnyInflateError\n";
222 The functional interface needs Perl5.005 or better.
225 =head2 anyinflate $input => $output [, OPTS]
228 C<anyinflate> expects at least two parameters, C<$input> and C<$output>.
230 =head3 The C<$input> parameter
232 The parameter, C<$input>, is used to define the source of
235 It can take one of the following forms:
241 If the C<$input> parameter is a simple scalar, it is assumed to be a
242 filename. This file will be opened for reading and the input data
243 will be read from it.
247 If the C<$input> parameter is a filehandle, the input data will be
249 The string '-' can be used as an alias for standard input.
251 =item A scalar reference
253 If C<$input> is a scalar reference, the input data will be read
256 =item An array reference
258 If C<$input> is an array reference, each element in the array must be a
261 The input data will be read from each file in turn.
263 The complete array will be walked to ensure that it only
264 contains valid filenames before any data is uncompressed.
268 =item An Input FileGlob string
270 If C<$input> is a string that is delimited by the characters "<" and ">"
271 C<anyinflate> will assume that it is an I<input fileglob string>. The
272 input is the list of files that match the fileglob.
274 If the fileglob does not match any files ...
276 See L<File::GlobMapper|File::GlobMapper> for more details.
281 If the C<$input> parameter is any other type, C<undef> will be returned.
285 =head3 The C<$output> parameter
287 The parameter C<$output> is used to control the destination of the
288 uncompressed data. This parameter can take one of these forms.
294 If the C<$output> parameter is a simple scalar, it is assumed to be a
295 filename. This file will be opened for writing and the uncompressed
296 data will be written to it.
300 If the C<$output> parameter is a filehandle, the uncompressed data
301 will be written to it.
302 The string '-' can be used as an alias for standard output.
305 =item A scalar reference
307 If C<$output> is a scalar reference, the uncompressed data will be
308 stored in C<$$output>.
312 =item An Array Reference
314 If C<$output> is an array reference, the uncompressed data will be
315 pushed onto the array.
317 =item An Output FileGlob
319 If C<$output> is a string that is delimited by the characters "<" and ">"
320 C<anyinflate> will assume that it is an I<output fileglob string>. The
321 output is the list of files that match the fileglob.
323 When C<$output> is an fileglob string, C<$input> must also be a fileglob
324 string. Anything else is an error.
328 If the C<$output> parameter is any other type, C<undef> will be returned.
335 When C<$input> maps to multiple compressed files/buffers and C<$output> is
336 a single file/buffer, after uncompression C<$output> will contain a
337 concatenation of all the uncompressed data from each of the input
344 =head2 Optional Parameters
346 Unless specified below, the optional parameters for C<anyinflate>,
347 C<OPTS>, are the same as those used with the OO interface defined in the
348 L</"Constructor Options"> section below.
352 =item C<< AutoClose => 0|1 >>
354 This option applies to any input or output data streams to
355 C<anyinflate> that are filehandles.
357 If C<AutoClose> is specified, and the value is true, it will result in all
358 input and/or output filehandles being closed once C<anyinflate> has
361 This parameter defaults to 0.
364 =item C<< BinModeOut => 0|1 >>
366 When writing to a file or filehandle, set C<binmode> before writing to the
375 =item C<< Append => 0|1 >>
379 =item C<< MultiStream => 0|1 >>
382 If the input file/buffer contains multiple compressed data streams, this
383 option will uncompress the whole lot as a single data stream.
391 =item C<< TrailingData => $scalar >>
393 Returns the data, if any, that is present immediately after the compressed
394 data stream once uncompression is complete.
396 This option can be used when there is useful information immediately
397 following the compressed data stream, and you don't know the length of the
398 compressed data stream.
400 If the input is a buffer, C<trailingData> will return everything from the
401 end of the compressed data stream to the end of the buffer.
403 If the input is a filehandle, C<trailingData> will return the data that is
404 left in the filehandle input buffer once the end of the compressed data
405 stream has been reached. You can then use the filehandle to read the rest
408 Don't bother using C<trailingData> if the input is a filename.
412 If you know the length of the compressed data stream before you start
413 uncompressing, you can avoid having to use C<trailingData> by setting the
414 C<InputLength> option.
425 To read the contents of the file C<file1.txt.Compressed> and write the
426 compressed data to the file C<file1.txt>.
430 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
432 my $input = "file1.txt.Compressed";
433 my $output = "file1.txt";
434 anyinflate $input => $output
435 or die "anyinflate failed: $AnyInflateError\n";
438 To read from an existing Perl filehandle, C<$input>, and write the
439 uncompressed data to a buffer, C<$buffer>.
443 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
446 my $input = new IO::File "<file1.txt.Compressed"
447 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
449 anyinflate $input => \$buffer
450 or die "anyinflate failed: $AnyInflateError\n";
452 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
456 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
458 anyinflate '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
459 or die "anyinflate failed: $AnyInflateError\n";
461 and if you want to compress each file one at a time, this will do the trick
465 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
467 for my $input ( glob "/my/home/*.txt.Compressed" )
470 $output =~ s/.Compressed// ;
471 anyinflate $input => $output
472 or die "Error compressing '$input': $AnyInflateError\n";
479 The format of the constructor for IO::Uncompress::AnyInflate is shown below
482 my $z = new IO::Uncompress::AnyInflate $input [OPTS]
483 or die "IO::Uncompress::AnyInflate failed: $AnyInflateError\n";
485 Returns an C<IO::Uncompress::AnyInflate> object on success and undef on failure.
486 The variable C<$AnyInflateError> will contain an error message on failure.
488 If you are running Perl 5.005 or better the object, C<$z>, returned from
489 IO::Uncompress::AnyInflate can be used exactly like an L<IO::File|IO::File> filehandle.
490 This means that all normal input file operations can be carried out with
491 C<$z>. For example, to read a line from a compressed file/buffer you can
492 use either of these forms
494 $line = $z->getline();
497 The mandatory parameter C<$input> is used to determine the source of the
498 compressed data. This parameter can take one of three forms.
504 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
505 file will be opened for reading and the compressed data will be read from it.
509 If the C<$input> parameter is a filehandle, the compressed data will be
511 The string '-' can be used as an alias for standard input.
514 =item A scalar reference
516 If C<$input> is a scalar reference, the compressed data will be read from
521 =head2 Constructor Options
524 The option names defined below are case insensitive and can be optionally
525 prefixed by a '-'. So all of the following are valid
532 OPTS is a combination of the following options:
536 =item C<< AutoClose => 0|1 >>
538 This option is only valid when the C<$input> parameter is a filehandle. If
539 specified, and the value is true, it will result in the file being closed once
540 either the C<close> method is called or the IO::Uncompress::AnyInflate object is
543 This parameter defaults to 0.
545 =item C<< MultiStream => 0|1 >>
549 Allows multiple concatenated compressed streams to be treated as a single
550 compressed stream. Decompression will stop once either the end of the
551 file/buffer is reached, an error is encountered (premature eof, corrupt
552 compressed data) or the end of a stream is not immediately followed by the
553 start of another stream.
555 This parameter defaults to 0.
558 =item C<< Prime => $string >>
560 This option will uncompress the contents of C<$string> before processing the
563 This option can be useful when the compressed data is embedded in another
564 file/data structure and it is not possible to work out where the compressed
565 data begins without having to read the first few bytes. If this is the
566 case, the uncompression can be I<primed> with these bytes using this
569 =item C<< Transparent => 0|1 >>
571 If this option is set and the input file/buffer is not compressed data,
572 the module will allow reading of it anyway.
574 In addition, if the input file/buffer does contain compressed data and
575 there is non-compressed data immediately following it, setting this option
576 will make this module treat the whole file/bufffer as a single data stream.
578 This option defaults to 1.
580 =item C<< BlockSize => $num >>
582 When reading the compressed input data, IO::Uncompress::AnyInflate will read it in
583 blocks of C<$num> bytes.
585 This option defaults to 4096.
587 =item C<< InputLength => $size >>
589 When present this option will limit the number of compressed bytes read
590 from the input file/buffer to C<$size>. This option can be used in the
591 situation where there is useful data directly after the compressed data
592 stream and you know beforehand the exact length of the compressed data
595 This option is mostly used when reading from a filehandle, in which case
596 the file pointer will be left pointing to the first byte directly after the
597 compressed data stream.
601 This option defaults to off.
603 =item C<< Append => 0|1 >>
605 This option controls what the C<read> method does with uncompressed data.
607 If set to 1, all uncompressed data will be appended to the output parameter
608 of the C<read> method.
610 If set to 0, the contents of the output parameter of the C<read> method
611 will be overwritten by the uncompressed data.
615 =item C<< Strict => 0|1 >>
619 This option controls whether the extra checks defined below are used when
620 carrying out the decompression. When Strict is on, the extra tests are
621 carried out, when Strict is off they are not.
623 The default for this option is off.
626 If the input is an RFC 1950 data stream, the following will be checked:
635 The ADLER32 checksum field must be present.
639 The value of the ADLER32 field read must match the adler32 value of the
640 uncompressed data actually contained in the file.
646 If the input is a gzip (RFC 1952) data stream, the following will be checked:
655 If the FHCRC bit is set in the gzip FLG header byte, the CRC16 bytes in the
656 header must match the crc16 value of the gzip header actually read.
660 If the gzip header contains a name field (FNAME) it consists solely of ISO
665 If the gzip header contains a comment field (FCOMMENT) it consists solely
666 of ISO 8859-1 characters plus line-feed.
670 If the gzip FEXTRA header field is present it must conform to the sub-field
671 structure as defined in RFC 1952.
675 The CRC32 and ISIZE trailer fields must be present.
679 The value of the CRC32 field read must match the crc32 value of the
680 uncompressed data actually contained in the gzip file.
684 The value of the ISIZE fields read must match the length of the
685 uncompressed data actually read from the file.
693 =item C<< RawInflate => 0|1 >>
695 When auto-detecting the compressed format, try to test for raw-deflate (RFC
696 1951) content using the C<IO::Uncompress::RawInflate> module.
698 The reason this is not default behaviour is because RFC 1951 content can
699 only be detected by attempting to uncompress it. This process is error
700 prone and can result is false positives.
707 =item C<< ParseExtra => 0|1 >>
708 If the gzip FEXTRA header field is present and this option is set, it will
709 force the module to check that it conforms to the sub-field structure as
712 If the C<Strict> is on it will automatically enable this option.
731 $status = $z->read($buffer)
733 Reads a block of compressed data (the size the the compressed block is
734 determined by the C<Buffer> option in the constructor), uncompresses it and
735 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
736 set in the constructor, the uncompressed data will be appended to the
737 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
739 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
740 or a negative number on error.
746 $status = $z->read($buffer, $length)
747 $status = $z->read($buffer, $length, $offset)
749 $status = read($z, $buffer, $length)
750 $status = read($z, $buffer, $length, $offset)
752 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
754 The main difference between this form of the C<read> method and the
755 previous one, is that this one will attempt to return I<exactly> C<$length>
756 bytes. The only circumstances that this function will not is if end-of-file
757 or an IO error is encountered.
759 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
760 or a negative number on error.
767 $line = $z->getline()
772 This method fully supports the use of of the variable C<$/> (or
773 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
774 determine what constitutes an end of line. Paragraph mode, record mode and
775 file slurp mode are all supported.
784 Read a single character.
790 $char = $z->ungetc($string)
798 $status = $z->inflateSync()
807 $hdr = $z->getHeaderInfo();
808 @hdrs = $z->getHeaderInfo();
810 This method returns either a hash reference (in scalar context) or a list
811 or hash references (in array context) that contains information about each
812 of the header fields in the compressed data stream(s).
824 Returns the uncompressed file offset.
835 Returns true if the end of the compressed input stream has been reached.
841 $z->seek($position, $whence);
842 seek($z, $position, $whence);
847 Provides a sub-set of the C<seek> functionality, with the restriction
848 that it is only legal to seek forward in the input file/buffer.
849 It is a fatal error to attempt to seek backward.
853 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
854 SEEK_CUR or SEEK_END.
856 Returns 1 on success, 0 on failure.
865 This is a noop provided for completeness.
871 Returns true if the object currently refers to a opened file/buffer.
875 my $prev = $z->autoflush()
876 my $prev = $z->autoflush(EXPR)
878 If the C<$z> object is associated with a file or a filehandle, this method
879 returns the current autoflush setting for the underlying filehandle. If
880 C<EXPR> is present, and is non-zero, it will enable flushing after every
881 write/print operation.
883 If C<$z> is associated with a buffer, this method has no effect and always
886 B<Note> that the special variable C<$|> B<cannot> be used to set or
887 retrieve the autoflush setting.
889 =head2 input_line_number
891 $z->input_line_number()
892 $z->input_line_number(EXPR)
896 Returns the current uncompressed line number. If C<EXPR> is present it has
897 the effect of setting the line number. Note that setting the line number
898 does not change the current position within the file/buffer being read.
900 The contents of C<$/> are used to to determine what constitutes a line
910 If the C<$z> object is associated with a file or a filehandle, this method
911 will return the underlying file descriptor.
913 If the C<$z> object is is associated with a buffer, this method will
923 Closes the output file/buffer.
927 For most versions of Perl this method will be automatically invoked if
928 the IO::Uncompress::AnyInflate object is destroyed (either explicitly or by the
929 variable with the reference to the object going out of scope). The
930 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
931 these cases, the C<close> method will be called automatically, but
932 not until global destruction of all live objects when the program is
935 Therefore, if you want your scripts to be able to run on all versions
936 of Perl, you should call C<close> explicitly and not rely on automatic
939 Returns true on success, otherwise 0.
941 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyInflate
942 object was created, and the object is associated with a file, the
943 underlying file will also be closed.
952 my $status = $z->nextStream();
954 Skips to the next compressed data stream in the input file/buffer. If a new
955 compressed data stream is found, the eof marker will be cleared and C<$.>
958 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
959 error was encountered.
965 my $data = $z->trailingData();
967 Returns the data, if any, that is present immediately after the compressed
968 data stream once uncompression is complete. It only makes sense to call
969 this method once the end of the compressed data stream has been
972 This option can be used when there is useful information immediately
973 following the compressed data stream, and you don't know the length of the
974 compressed data stream.
976 If the input is a buffer, C<trailingData> will return everything from the
977 end of the compressed data stream to the end of the buffer.
979 If the input is a filehandle, C<trailingData> will return the data that is
980 left in the filehandle input buffer once the end of the compressed data
981 stream has been reached. You can then use the filehandle to read the rest
984 Don't bother using C<trailingData> if the input is a filename.
988 If you know the length of the compressed data stream before you start
989 uncompressing, you can avoid having to use C<trailingData> by setting the
990 C<InputLength> option in the constructor.
994 No symbolic constants are required by this IO::Uncompress::AnyInflate at present.
1000 Imports C<anyinflate> and C<$AnyInflateError>.
1003 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
1014 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::AnyUncompress>
1016 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1018 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1019 L<Archive::Tar|Archive::Tar>,
1020 L<IO::Zlib|IO::Zlib>
1023 For RFC 1950, 1951 and 1952 see
1024 F<http://www.faqs.org/rfcs/rfc1950.html>,
1025 F<http://www.faqs.org/rfcs/rfc1951.html> and
1026 F<http://www.faqs.org/rfcs/rfc1952.html>
1028 The I<zlib> compression library was written by Jean-loup Gailly
1029 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1031 The primary site for the I<zlib> compression library is
1032 F<http://www.zlib.org>.
1034 The primary site for gzip is F<http://www.gzip.org>.
1041 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1045 =head1 MODIFICATION HISTORY
1047 See the Changes file.
1049 =head1 COPYRIGHT AND LICENSE
1051 Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
1053 This program is free software; you can redistribute it and/or
1054 modify it under the same terms as Perl itself.