1 package IO::Uncompress::AnyInflate ;
3 # for RFC1950, RFC1951 or RFC1952
9 use IO::Compress::Base::Common 2.036 qw(createSelfTiedObject);
11 use IO::Uncompress::Adapter::Inflate 2.036 ();
14 use IO::Uncompress::Base 2.036 ;
15 use IO::Uncompress::Gunzip 2.036 ;
16 use IO::Uncompress::Inflate 2.036 ;
17 use IO::Uncompress::RawInflate 2.036 ;
18 use IO::Uncompress::Unzip 2.036 ;
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.036 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);
72 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
74 return $self->saveErrorString(undef, $errstr, $errno)
77 *$self->{Uncomp} = $obj;
79 my @possible = qw( Inflate Gunzip Unzip );
80 unshift @possible, 'RawInflate'
81 if 1 || $got->value('RawInflate');
83 my $magic = $self->ckMagic( @possible );
86 *$self->{Info} = $self->readHeader($magic)
102 my $keep = ref $self ;
103 for my $class ( map { "IO::Uncompress::$_" } @names)
105 bless $self => $class;
106 my $magic = $self->ckMagic();
110 #bless $self => $class;
114 $self->pushBack(*$self->{HeaderPending}) ;
115 *$self->{HeaderPending} = '' ;
118 bless $self => $keep;
129 IO::Uncompress::AnyInflate - Uncompress zlib-based (zip, gzip) file/buffer
133 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
135 my $status = anyinflate $input => $output [,OPTS]
136 or die "anyinflate failed: $AnyInflateError\n";
138 my $z = new IO::Uncompress::AnyInflate $input [OPTS]
139 or die "anyinflate failed: $AnyInflateError\n";
141 $status = $z->read($buffer)
142 $status = $z->read($buffer, $length)
143 $status = $z->read($buffer, $length, $offset)
144 $line = $z->getline()
149 $status = $z->inflateSync()
151 $data = $z->trailingData()
152 $status = $z->nextStream()
153 $data = $z->getHeaderInfo()
155 $z->seek($position, $whence)
167 read($z, $buffer, $length);
168 read($z, $buffer, $length, $offset);
170 seek($z, $position, $whence)
178 This module provides a Perl interface that allows the reading of
179 files/buffers that have been compressed in a number of formats that use the
180 zlib compression library.
182 The formats supported are
188 =item RFC 1951 (optionally)
190 =item gzip (RFC 1952)
196 The module will auto-detect which, if any, of the supported
197 compression formats is being used.
199 =head1 Functional Interface
201 A top-level function, C<anyinflate>, is provided to carry out
202 "one-shot" uncompression between buffers and/or files. For finer
203 control over the uncompression process, see the L</"OO Interface">
206 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
208 anyinflate $input => $output [,OPTS]
209 or die "anyinflate failed: $AnyInflateError\n";
211 The functional interface needs Perl5.005 or better.
213 =head2 anyinflate $input => $output [, OPTS]
215 C<anyinflate> expects at least two parameters, C<$input> and C<$output>.
217 =head3 The C<$input> parameter
219 The parameter, C<$input>, is used to define the source of
222 It can take one of the following forms:
228 If the C<$input> parameter is a simple scalar, it is assumed to be a
229 filename. This file will be opened for reading and the input data
230 will be read from it.
234 If the C<$input> parameter is a filehandle, the input data will be
236 The string '-' can be used as an alias for standard input.
238 =item A scalar reference
240 If C<$input> is a scalar reference, the input data will be read
243 =item An array reference
245 If C<$input> is an array reference, each element in the array must be a
248 The input data will be read from each file in turn.
250 The complete array will be walked to ensure that it only
251 contains valid filenames before any data is uncompressed.
253 =item An Input FileGlob string
255 If C<$input> is a string that is delimited by the characters "<" and ">"
256 C<anyinflate> will assume that it is an I<input fileglob string>. The
257 input is the list of files that match the fileglob.
259 See L<File::GlobMapper|File::GlobMapper> for more details.
263 If the C<$input> parameter is any other type, C<undef> will be returned.
265 =head3 The C<$output> parameter
267 The parameter C<$output> is used to control the destination of the
268 uncompressed data. This parameter can take one of these forms.
274 If the C<$output> parameter is a simple scalar, it is assumed to be a
275 filename. This file will be opened for writing and the uncompressed
276 data will be written to it.
280 If the C<$output> parameter is a filehandle, the uncompressed data
281 will be written to it.
282 The string '-' can be used as an alias for standard output.
284 =item A scalar reference
286 If C<$output> is a scalar reference, the uncompressed data will be
287 stored in C<$$output>.
289 =item An Array Reference
291 If C<$output> is an array reference, the uncompressed data will be
292 pushed onto the array.
294 =item An Output FileGlob
296 If C<$output> is a string that is delimited by the characters "<" and ">"
297 C<anyinflate> will assume that it is an I<output fileglob string>. The
298 output is the list of files that match the fileglob.
300 When C<$output> is an fileglob string, C<$input> must also be a fileglob
301 string. Anything else is an error.
303 See L<File::GlobMapper|File::GlobMapper> for more details.
307 If the C<$output> parameter is any other type, C<undef> will be returned.
311 When C<$input> maps to multiple compressed files/buffers and C<$output> is
312 a single file/buffer, after uncompression C<$output> will contain a
313 concatenation of all the uncompressed data from each of the input
316 =head2 Optional Parameters
318 Unless specified below, the optional parameters for C<anyinflate>,
319 C<OPTS>, are the same as those used with the OO interface defined in the
320 L</"Constructor Options"> section below.
324 =item C<< AutoClose => 0|1 >>
326 This option applies to any input or output data streams to
327 C<anyinflate> that are filehandles.
329 If C<AutoClose> is specified, and the value is true, it will result in all
330 input and/or output filehandles being closed once C<anyinflate> has
333 This parameter defaults to 0.
335 =item C<< BinModeOut => 0|1 >>
337 When writing to a file or filehandle, set C<binmode> before writing to the
342 =item C<< Append => 0|1 >>
344 The behaviour of this option is dependent on the type of output data
351 If C<Append> is enabled, all uncompressed data will be append to the end of
352 the output buffer. Otherwise the output buffer will be cleared before any
353 uncompressed data is written to it.
357 If C<Append> is enabled, the file will be opened in append mode. Otherwise
358 the contents of the file, if any, will be truncated before any uncompressed
359 data is written to it.
363 If C<Append> is enabled, the filehandle will be positioned to the end of
364 the file via a call to C<seek> before any uncompressed data is
365 written to it. Otherwise the file pointer will not be moved.
369 When C<Append> is specified, and set to true, it will I<append> all uncompressed
370 data to the output data stream.
372 So when the output is a filehandle it will carry out a seek to the eof
373 before writing any uncompressed data. If the output is a filename, it will be opened for
374 appending. If the output is a buffer, all uncompressed data will be
375 appended to the existing buffer.
377 Conversely when C<Append> is not specified, or it is present and is set to
378 false, it will operate as follows.
380 When the output is a filename, it will truncate the contents of the file
381 before writing any uncompressed data. If the output is a filehandle
382 its position will not be changed. If the output is a buffer, it will be
383 wiped before any uncompressed data is output.
387 =item C<< MultiStream => 0|1 >>
389 If the input file/buffer contains multiple compressed data streams, this
390 option will uncompress the whole lot as a single data stream.
394 =item C<< TrailingData => $scalar >>
396 Returns the data, if any, that is present immediately after the compressed
397 data stream once uncompression is complete.
399 This option can be used when there is useful information immediately
400 following the compressed data stream, and you don't know the length of the
401 compressed data stream.
403 If the input is a buffer, C<trailingData> will return everything from the
404 end of the compressed data stream to the end of the buffer.
406 If the input is a filehandle, C<trailingData> will return the data that is
407 left in the filehandle input buffer once the end of the compressed data
408 stream has been reached. You can then use the filehandle to read the rest
411 Don't bother using C<trailingData> if the input is a filename.
413 If you know the length of the compressed data stream before you start
414 uncompressing, you can avoid having to use C<trailingData> by setting the
415 C<InputLength> option.
421 To read the contents of the file C<file1.txt.Compressed> and write the
422 uncompressed data to the file C<file1.txt>.
426 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
428 my $input = "file1.txt.Compressed";
429 my $output = "file1.txt";
430 anyinflate $input => $output
431 or die "anyinflate failed: $AnyInflateError\n";
433 To read from an existing Perl filehandle, C<$input>, and write the
434 uncompressed data to a buffer, C<$buffer>.
438 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
441 my $input = new IO::File "<file1.txt.Compressed"
442 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
444 anyinflate $input => \$buffer
445 or die "anyinflate failed: $AnyInflateError\n";
447 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
451 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
453 anyinflate '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
454 or die "anyinflate failed: $AnyInflateError\n";
456 and if you want to compress each file one at a time, this will do the trick
460 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
462 for my $input ( glob "/my/home/*.txt.Compressed" )
465 $output =~ s/.Compressed// ;
466 anyinflate $input => $output
467 or die "Error compressing '$input': $AnyInflateError\n";
474 The format of the constructor for IO::Uncompress::AnyInflate is shown below
476 my $z = new IO::Uncompress::AnyInflate $input [OPTS]
477 or die "IO::Uncompress::AnyInflate failed: $AnyInflateError\n";
479 Returns an C<IO::Uncompress::AnyInflate> object on success and undef on failure.
480 The variable C<$AnyInflateError> will contain an error message on failure.
482 If you are running Perl 5.005 or better the object, C<$z>, returned from
483 IO::Uncompress::AnyInflate can be used exactly like an L<IO::File|IO::File> filehandle.
484 This means that all normal input file operations can be carried out with
485 C<$z>. For example, to read a line from a compressed file/buffer you can
486 use either of these forms
488 $line = $z->getline();
491 The mandatory parameter C<$input> is used to determine the source of the
492 compressed data. This parameter can take one of three forms.
498 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
499 file will be opened for reading and the compressed data will be read from it.
503 If the C<$input> parameter is a filehandle, the compressed data will be
505 The string '-' can be used as an alias for standard input.
507 =item A scalar reference
509 If C<$input> is a scalar reference, the compressed data will be read from
514 =head2 Constructor Options
516 The option names defined below are case insensitive and can be optionally
517 prefixed by a '-'. So all of the following are valid
524 OPTS is a combination of the following options:
528 =item C<< AutoClose => 0|1 >>
530 This option is only valid when the C<$input> parameter is a filehandle. If
531 specified, and the value is true, it will result in the file being closed once
532 either the C<close> method is called or the IO::Uncompress::AnyInflate object is
535 This parameter defaults to 0.
537 =item C<< MultiStream => 0|1 >>
539 Allows multiple concatenated compressed streams to be treated as a single
540 compressed stream. Decompression will stop once either the end of the
541 file/buffer is reached, an error is encountered (premature eof, corrupt
542 compressed data) or the end of a stream is not immediately followed by the
543 start of another stream.
545 This parameter defaults to 0.
547 =item C<< Prime => $string >>
549 This option will uncompress the contents of C<$string> before processing the
552 This option can be useful when the compressed data is embedded in another
553 file/data structure and it is not possible to work out where the compressed
554 data begins without having to read the first few bytes. If this is the
555 case, the uncompression can be I<primed> with these bytes using this
558 =item C<< Transparent => 0|1 >>
560 If this option is set and the input file/buffer is not compressed data,
561 the module will allow reading of it anyway.
563 In addition, if the input file/buffer does contain compressed data and
564 there is non-compressed data immediately following it, setting this option
565 will make this module treat the whole file/buffer as a single data stream.
567 This option defaults to 1.
569 =item C<< BlockSize => $num >>
571 When reading the compressed input data, IO::Uncompress::AnyInflate will read it in
572 blocks of C<$num> bytes.
574 This option defaults to 4096.
576 =item C<< InputLength => $size >>
578 When present this option will limit the number of compressed bytes read
579 from the input file/buffer to C<$size>. This option can be used in the
580 situation where there is useful data directly after the compressed data
581 stream and you know beforehand the exact length of the compressed data
584 This option is mostly used when reading from a filehandle, in which case
585 the file pointer will be left pointing to the first byte directly after the
586 compressed data stream.
588 This option defaults to off.
590 =item C<< Append => 0|1 >>
592 This option controls what the C<read> method does with uncompressed data.
594 If set to 1, all uncompressed data will be appended to the output parameter
595 of the C<read> method.
597 If set to 0, the contents of the output parameter of the C<read> method
598 will be overwritten by the uncompressed data.
602 =item C<< Strict => 0|1 >>
604 This option controls whether the extra checks defined below are used when
605 carrying out the decompression. When Strict is on, the extra tests are
606 carried out, when Strict is off they are not.
608 The default for this option is off.
610 If the input is an RFC 1950 data stream, the following will be checked:
616 The ADLER32 checksum field must be present.
620 The value of the ADLER32 field read must match the adler32 value of the
621 uncompressed data actually contained in the file.
625 If the input is a gzip (RFC 1952) data stream, the following will be checked:
631 If the FHCRC bit is set in the gzip FLG header byte, the CRC16 bytes in the
632 header must match the crc16 value of the gzip header actually read.
636 If the gzip header contains a name field (FNAME) it consists solely of ISO
641 If the gzip header contains a comment field (FCOMMENT) it consists solely
642 of ISO 8859-1 characters plus line-feed.
646 If the gzip FEXTRA header field is present it must conform to the sub-field
647 structure as defined in RFC 1952.
651 The CRC32 and ISIZE trailer fields must be present.
655 The value of the CRC32 field read must match the crc32 value of the
656 uncompressed data actually contained in the gzip file.
660 The value of the ISIZE fields read must match the length of the
661 uncompressed data actually read from the file.
665 =item C<< RawInflate => 0|1 >>
667 When auto-detecting the compressed format, try to test for raw-deflate (RFC
668 1951) content using the C<IO::Uncompress::RawInflate> module.
670 The reason this is not default behaviour is because RFC 1951 content can
671 only be detected by attempting to uncompress it. This process is error
672 prone and can result is false positives.
676 =item C<< ParseExtra => 0|1 >>
677 If the gzip FEXTRA header field is present and this option is set, it will
678 force the module to check that it conforms to the sub-field structure as
681 If the C<Strict> is on it will automatically enable this option.
697 $status = $z->read($buffer)
699 Reads a block of compressed data (the size the the compressed block is
700 determined by the C<Buffer> option in the constructor), uncompresses it and
701 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
702 set in the constructor, the uncompressed data will be appended to the
703 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
705 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
706 or a negative number on error.
712 $status = $z->read($buffer, $length)
713 $status = $z->read($buffer, $length, $offset)
715 $status = read($z, $buffer, $length)
716 $status = read($z, $buffer, $length, $offset)
718 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
720 The main difference between this form of the C<read> method and the
721 previous one, is that this one will attempt to return I<exactly> C<$length>
722 bytes. The only circumstances that this function will not is if end-of-file
723 or an IO error is encountered.
725 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
726 or a negative number on error.
732 $line = $z->getline()
737 This method fully supports the use of of the variable C<$/> (or
738 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
739 determine what constitutes an end of line. Paragraph mode, record mode and
740 file slurp mode are all supported.
748 Read a single character.
754 $char = $z->ungetc($string)
760 $status = $z->inflateSync()
768 $hdr = $z->getHeaderInfo();
769 @hdrs = $z->getHeaderInfo();
771 This method returns either a hash reference (in scalar context) or a list
772 or hash references (in array context) that contains information about each
773 of the header fields in the compressed data stream(s).
782 Returns the uncompressed file offset.
791 Returns true if the end of the compressed input stream has been reached.
795 $z->seek($position, $whence);
796 seek($z, $position, $whence);
798 Provides a sub-set of the C<seek> functionality, with the restriction
799 that it is only legal to seek forward in the input file/buffer.
800 It is a fatal error to attempt to seek backward.
802 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
803 SEEK_CUR or SEEK_END.
805 Returns 1 on success, 0 on failure.
814 This is a noop provided for completeness.
820 Returns true if the object currently refers to a opened file/buffer.
824 my $prev = $z->autoflush()
825 my $prev = $z->autoflush(EXPR)
827 If the C<$z> object is associated with a file or a filehandle, this method
828 returns the current autoflush setting for the underlying filehandle. If
829 C<EXPR> is present, and is non-zero, it will enable flushing after every
830 write/print operation.
832 If C<$z> is associated with a buffer, this method has no effect and always
835 B<Note> that the special variable C<$|> B<cannot> be used to set or
836 retrieve the autoflush setting.
838 =head2 input_line_number
840 $z->input_line_number()
841 $z->input_line_number(EXPR)
843 Returns the current uncompressed line number. If C<EXPR> is present it has
844 the effect of setting the line number. Note that setting the line number
845 does not change the current position within the file/buffer being read.
847 The contents of C<$/> are used to to determine what constitutes a line
855 If the C<$z> object is associated with a file or a filehandle, C<fileno>
856 will return the underlying file descriptor. Once the C<close> method is
857 called C<fileno> will return C<undef>.
859 If the C<$z> object is associated with a buffer, this method will return
867 Closes the output file/buffer.
869 For most versions of Perl this method will be automatically invoked if
870 the IO::Uncompress::AnyInflate object is destroyed (either explicitly or by the
871 variable with the reference to the object going out of scope). The
872 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
873 these cases, the C<close> method will be called automatically, but
874 not until global destruction of all live objects when the program is
877 Therefore, if you want your scripts to be able to run on all versions
878 of Perl, you should call C<close> explicitly and not rely on automatic
881 Returns true on success, otherwise 0.
883 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyInflate
884 object was created, and the object is associated with a file, the
885 underlying file will also be closed.
891 my $status = $z->nextStream();
893 Skips to the next compressed data stream in the input file/buffer. If a new
894 compressed data stream is found, the eof marker will be cleared and C<$.>
897 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
898 error was encountered.
904 my $data = $z->trailingData();
906 Returns the data, if any, that is present immediately after the compressed
907 data stream once uncompression is complete. It only makes sense to call
908 this method once the end of the compressed data stream has been
911 This option can be used when there is useful information immediately
912 following the compressed data stream, and you don't know the length of the
913 compressed data stream.
915 If the input is a buffer, C<trailingData> will return everything from the
916 end of the compressed data stream to the end of the buffer.
918 If the input is a filehandle, C<trailingData> will return the data that is
919 left in the filehandle input buffer once the end of the compressed data
920 stream has been reached. You can then use the filehandle to read the rest
923 Don't bother using C<trailingData> if the input is a filename.
925 If you know the length of the compressed data stream before you start
926 uncompressing, you can avoid having to use C<trailingData> by setting the
927 C<InputLength> option in the constructor.
931 No symbolic constants are required by this IO::Uncompress::AnyInflate at present.
937 Imports C<anyinflate> and C<$AnyInflateError>.
940 use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
946 =head2 Working with Net::FTP
948 See L<IO::Uncompress::AnyInflate::FAQ|IO::Uncompress::AnyInflate::FAQ/"Compressed files and Net::FTP">
952 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::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyUncompress>
954 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
956 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
957 L<Archive::Tar|Archive::Tar>,
960 For RFC 1950, 1951 and 1952 see
961 F<http://www.faqs.org/rfcs/rfc1950.html>,
962 F<http://www.faqs.org/rfcs/rfc1951.html> and
963 F<http://www.faqs.org/rfcs/rfc1952.html>
965 The I<zlib> compression library was written by Jean-loup Gailly
966 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
968 The primary site for the I<zlib> compression library is
969 F<http://www.zlib.org>.
971 The primary site for gzip is F<http://www.gzip.org>.
975 This module was written by Paul Marquess, F<pmqs@cpan.org>.
977 =head1 MODIFICATION HISTORY
979 See the Changes file.
981 =head1 COPYRIGHT AND LICENSE
983 Copyright (c) 2005-2011 Paul Marquess. All rights reserved.
985 This program is free software; you can redistribute it and/or
986 modify it under the same terms as Perl itself.