1 package IO::Uncompress::AnyUncompress ;
7 use IO::Compress::Base::Common 2.012 qw(createSelfTiedObject);
9 use IO::Uncompress::Base 2.012 ;
14 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
17 $AnyUncompressError = '';
19 @ISA = qw( Exporter IO::Uncompress::Base );
20 @EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
21 %EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
22 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
23 Exporter::export_ok_tags('all');
25 # TODO - allow the user to pick a set of the three formats to allow
26 # or just assume want to auto-detect any of the three formats.
30 eval ' use IO::Uncompress::Adapter::Inflate 2.012 ;';
31 eval ' use IO::Uncompress::Adapter::Bunzip2 2.012 ;';
32 eval ' use IO::Uncompress::Adapter::LZO 2.012 ;';
33 eval ' use IO::Uncompress::Adapter::Lzf 2.012 ;';
35 eval ' use IO::Uncompress::Bunzip2 2.012 ;';
36 eval ' use IO::Uncompress::UnLzop 2.012 ;';
37 eval ' use IO::Uncompress::Gunzip 2.012 ;';
38 eval ' use IO::Uncompress::Inflate 2.012 ;';
39 eval ' use IO::Uncompress::RawInflate 2.012 ;';
40 eval ' use IO::Uncompress::Unzip 2.012 ;';
41 eval ' use IO::Uncompress::UnLzf 2.012 ;';
47 my $obj = createSelfTiedObject($class, \$AnyUncompressError);
48 $obj->_create(undef, 0, @_);
53 my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
54 return $obj->_inf(@_) ;
59 use IO::Compress::Base::Common 2.012 qw(:Parse);
60 return ( 'RawInflate' => [1, 1, Parse_boolean, 0] ) ;
68 # any always needs both crc32 and adler32
69 $got->value('CRC32' => 1);
70 $got->value('ADLER32' => 1);
83 if (defined $IO::Uncompress::RawInflate::VERSION )
85 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
87 return $self->saveErrorString(undef, $errstr, $errno)
90 *$self->{Uncomp} = $obj;
92 my @possible = qw( Inflate Gunzip Unzip );
93 unshift @possible, 'RawInflate'
94 if $got->value('RawInflate');
96 $magic = $self->ckMagic( @possible );
99 *$self->{Info} = $self->readHeader($magic)
106 if (defined $IO::Uncompress::Bunzip2::VERSION and
107 $magic = $self->ckMagic('Bunzip2')) {
108 *$self->{Info} = $self->readHeader($magic)
111 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
113 return $self->saveErrorString(undef, $errstr, $errno)
116 *$self->{Uncomp} = $obj;
121 if (defined $IO::Uncompress::UnLzop::VERSION and
122 $magic = $self->ckMagic('UnLzop')) {
124 *$self->{Info} = $self->readHeader($magic)
127 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
129 return $self->saveErrorString(undef, $errstr, $errno)
132 *$self->{Uncomp} = $obj;
137 if (defined $IO::Uncompress::UnLzf::VERSION and
138 $magic = $self->ckMagic('UnLzf')) {
140 *$self->{Info} = $self->readHeader($magic)
143 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Lzf::mkUncompObject();
145 return $self->saveErrorString(undef, $errstr, $errno)
148 *$self->{Uncomp} = $obj;
163 my $keep = ref $self ;
164 for my $class ( map { "IO::Uncompress::$_" } @names)
166 bless $self => $class;
167 my $magic = $self->ckMagic();
171 #bless $self => $class;
175 $self->pushBack(*$self->{HeaderPending}) ;
176 *$self->{HeaderPending} = '' ;
179 bless $self => $keep;
190 IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop file/buffer
194 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
196 my $status = anyuncompress $input => $output [,OPTS]
197 or die "anyuncompress failed: $AnyUncompressError\n";
199 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
200 or die "anyuncompress failed: $AnyUncompressError\n";
202 $status = $z->read($buffer)
203 $status = $z->read($buffer, $length)
204 $status = $z->read($buffer, $length, $offset)
205 $line = $z->getline()
210 $data = $z->trailingData()
211 $status = $z->nextStream()
212 $data = $z->getHeaderInfo()
214 $z->seek($position, $whence)
220 $AnyUncompressError ;
226 read($z, $buffer, $length);
227 read($z, $buffer, $length, $offset);
229 seek($z, $position, $whence)
237 This module provides a Perl interface that allows the reading of
238 files/buffers that have been compressed with a variety of compression
241 The formats supported are:
247 =item RFC 1951 (optionally)
249 =item gzip (RFC 1952)
261 The module will auto-detect which, if any, of the supported
262 compression formats is being used.
264 =head1 Functional Interface
266 A top-level function, C<anyuncompress>, is provided to carry out
267 "one-shot" uncompression between buffers and/or files. For finer
268 control over the uncompression process, see the L</"OO Interface">
271 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
273 anyuncompress $input => $output [,OPTS]
274 or die "anyuncompress failed: $AnyUncompressError\n";
276 The functional interface needs Perl5.005 or better.
278 =head2 anyuncompress $input => $output [, OPTS]
280 C<anyuncompress> expects at least two parameters, C<$input> and C<$output>.
282 =head3 The C<$input> parameter
284 The parameter, C<$input>, is used to define the source of
287 It can take one of the following forms:
293 If the C<$input> parameter is a simple scalar, it is assumed to be a
294 filename. This file will be opened for reading and the input data
295 will be read from it.
299 If the C<$input> parameter is a filehandle, the input data will be
301 The string '-' can be used as an alias for standard input.
303 =item A scalar reference
305 If C<$input> is a scalar reference, the input data will be read
308 =item An array reference
310 If C<$input> is an array reference, each element in the array must be a
313 The input data will be read from each file in turn.
315 The complete array will be walked to ensure that it only
316 contains valid filenames before any data is uncompressed.
318 =item An Input FileGlob string
320 If C<$input> is a string that is delimited by the characters "<" and ">"
321 C<anyuncompress> will assume that it is an I<input fileglob string>. The
322 input is the list of files that match the fileglob.
324 If the fileglob does not match any files ...
326 See L<File::GlobMapper|File::GlobMapper> for more details.
330 If the C<$input> parameter is any other type, C<undef> will be returned.
332 =head3 The C<$output> parameter
334 The parameter C<$output> is used to control the destination of the
335 uncompressed data. This parameter can take one of these forms.
341 If the C<$output> parameter is a simple scalar, it is assumed to be a
342 filename. This file will be opened for writing and the uncompressed
343 data will be written to it.
347 If the C<$output> parameter is a filehandle, the uncompressed data
348 will be written to it.
349 The string '-' can be used as an alias for standard output.
351 =item A scalar reference
353 If C<$output> is a scalar reference, the uncompressed data will be
354 stored in C<$$output>.
356 =item An Array Reference
358 If C<$output> is an array reference, the uncompressed data will be
359 pushed onto the array.
361 =item An Output FileGlob
363 If C<$output> is a string that is delimited by the characters "<" and ">"
364 C<anyuncompress> will assume that it is an I<output fileglob string>. The
365 output is the list of files that match the fileglob.
367 When C<$output> is an fileglob string, C<$input> must also be a fileglob
368 string. Anything else is an error.
372 If the C<$output> parameter is any other type, C<undef> will be returned.
376 When C<$input> maps to multiple compressed files/buffers and C<$output> is
377 a single file/buffer, after uncompression C<$output> will contain a
378 concatenation of all the uncompressed data from each of the input
381 =head2 Optional Parameters
383 Unless specified below, the optional parameters for C<anyuncompress>,
384 C<OPTS>, are the same as those used with the OO interface defined in the
385 L</"Constructor Options"> section below.
389 =item C<< AutoClose => 0|1 >>
391 This option applies to any input or output data streams to
392 C<anyuncompress> that are filehandles.
394 If C<AutoClose> is specified, and the value is true, it will result in all
395 input and/or output filehandles being closed once C<anyuncompress> has
398 This parameter defaults to 0.
400 =item C<< BinModeOut => 0|1 >>
402 When writing to a file or filehandle, set C<binmode> before writing to the
407 =item C<< Append => 0|1 >>
411 =item C<< MultiStream => 0|1 >>
413 If the input file/buffer contains multiple compressed data streams, this
414 option will uncompress the whole lot as a single data stream.
418 =item C<< TrailingData => $scalar >>
420 Returns the data, if any, that is present immediately after the compressed
421 data stream once uncompression is complete.
423 This option can be used when there is useful information immediately
424 following the compressed data stream, and you don't know the length of the
425 compressed data stream.
427 If the input is a buffer, C<trailingData> will return everything from the
428 end of the compressed data stream to the end of the buffer.
430 If the input is a filehandle, C<trailingData> will return the data that is
431 left in the filehandle input buffer once the end of the compressed data
432 stream has been reached. You can then use the filehandle to read the rest
435 Don't bother using C<trailingData> if the input is a filename.
437 If you know the length of the compressed data stream before you start
438 uncompressing, you can avoid having to use C<trailingData> by setting the
439 C<InputLength> option.
445 To read the contents of the file C<file1.txt.Compressed> and write the
446 compressed data to the file C<file1.txt>.
450 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
452 my $input = "file1.txt.Compressed";
453 my $output = "file1.txt";
454 anyuncompress $input => $output
455 or die "anyuncompress failed: $AnyUncompressError\n";
457 To read from an existing Perl filehandle, C<$input>, and write the
458 uncompressed data to a buffer, C<$buffer>.
462 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
465 my $input = new IO::File "<file1.txt.Compressed"
466 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
468 anyuncompress $input => \$buffer
469 or die "anyuncompress failed: $AnyUncompressError\n";
471 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
475 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
477 anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
478 or die "anyuncompress failed: $AnyUncompressError\n";
480 and if you want to compress each file one at a time, this will do the trick
484 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
486 for my $input ( glob "/my/home/*.txt.Compressed" )
489 $output =~ s/.Compressed// ;
490 anyuncompress $input => $output
491 or die "Error compressing '$input': $AnyUncompressError\n";
498 The format of the constructor for IO::Uncompress::AnyUncompress is shown below
500 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
501 or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
503 Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
504 The variable C<$AnyUncompressError> will contain an error message on failure.
506 If you are running Perl 5.005 or better the object, C<$z>, returned from
507 IO::Uncompress::AnyUncompress can be used exactly like an L<IO::File|IO::File> filehandle.
508 This means that all normal input file operations can be carried out with
509 C<$z>. For example, to read a line from a compressed file/buffer you can
510 use either of these forms
512 $line = $z->getline();
515 The mandatory parameter C<$input> is used to determine the source of the
516 compressed data. This parameter can take one of three forms.
522 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
523 file will be opened for reading and the compressed data will be read from it.
527 If the C<$input> parameter is a filehandle, the compressed data will be
529 The string '-' can be used as an alias for standard input.
531 =item A scalar reference
533 If C<$input> is a scalar reference, the compressed data will be read from
538 =head2 Constructor Options
540 The option names defined below are case insensitive and can be optionally
541 prefixed by a '-'. So all of the following are valid
548 OPTS is a combination of the following options:
552 =item C<< AutoClose => 0|1 >>
554 This option is only valid when the C<$input> parameter is a filehandle. If
555 specified, and the value is true, it will result in the file being closed once
556 either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
559 This parameter defaults to 0.
561 =item C<< MultiStream => 0|1 >>
563 Allows multiple concatenated compressed streams to be treated as a single
564 compressed stream. Decompression will stop once either the end of the
565 file/buffer is reached, an error is encountered (premature eof, corrupt
566 compressed data) or the end of a stream is not immediately followed by the
567 start of another stream.
569 This parameter defaults to 0.
571 =item C<< Prime => $string >>
573 This option will uncompress the contents of C<$string> before processing the
576 This option can be useful when the compressed data is embedded in another
577 file/data structure and it is not possible to work out where the compressed
578 data begins without having to read the first few bytes. If this is the
579 case, the uncompression can be I<primed> with these bytes using this
582 =item C<< Transparent => 0|1 >>
584 If this option is set and the input file/buffer is not compressed data,
585 the module will allow reading of it anyway.
587 In addition, if the input file/buffer does contain compressed data and
588 there is non-compressed data immediately following it, setting this option
589 will make this module treat the whole file/bufffer as a single data stream.
591 This option defaults to 1.
593 =item C<< BlockSize => $num >>
595 When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
596 blocks of C<$num> bytes.
598 This option defaults to 4096.
600 =item C<< InputLength => $size >>
602 When present this option will limit the number of compressed bytes read
603 from the input file/buffer to C<$size>. This option can be used in the
604 situation where there is useful data directly after the compressed data
605 stream and you know beforehand the exact length of the compressed data
608 This option is mostly used when reading from a filehandle, in which case
609 the file pointer will be left pointing to the first byte directly after the
610 compressed data stream.
612 This option defaults to off.
614 =item C<< Append => 0|1 >>
616 This option controls what the C<read> method does with uncompressed data.
618 If set to 1, all uncompressed data will be appended to the output parameter
619 of the C<read> method.
621 If set to 0, the contents of the output parameter of the C<read> method
622 will be overwritten by the uncompressed data.
626 =item C<< Strict => 0|1 >>
628 This option controls whether the extra checks defined below are used when
629 carrying out the decompression. When Strict is on, the extra tests are
630 carried out, when Strict is off they are not.
632 The default for this option is off.
634 =item C<< RawInflate => 0|1 >>
636 When auto-detecting the compressed format, try to test for raw-deflate (RFC
637 1951) content using the C<IO::Uncompress::RawInflate> module.
639 The reason this is not default behaviour is because RFC 1951 content can
640 only be detected by attempting to uncompress it. This process is error
641 prone and can result is false positives.
657 $status = $z->read($buffer)
659 Reads a block of compressed data (the size the the compressed block is
660 determined by the C<Buffer> option in the constructor), uncompresses it and
661 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
662 set in the constructor, the uncompressed data will be appended to the
663 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
665 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
666 or a negative number on error.
672 $status = $z->read($buffer, $length)
673 $status = $z->read($buffer, $length, $offset)
675 $status = read($z, $buffer, $length)
676 $status = read($z, $buffer, $length, $offset)
678 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
680 The main difference between this form of the C<read> method and the
681 previous one, is that this one will attempt to return I<exactly> C<$length>
682 bytes. The only circumstances that this function will not is if end-of-file
683 or an IO error is encountered.
685 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
686 or a negative number on error.
692 $line = $z->getline()
697 This method fully supports the use of of the variable C<$/> (or
698 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
699 determine what constitutes an end of line. Paragraph mode, record mode and
700 file slurp mode are all supported.
708 Read a single character.
714 $char = $z->ungetc($string)
720 $hdr = $z->getHeaderInfo();
721 @hdrs = $z->getHeaderInfo();
723 This method returns either a hash reference (in scalar context) or a list
724 or hash references (in array context) that contains information about each
725 of the header fields in the compressed data stream(s).
734 Returns the uncompressed file offset.
743 Returns true if the end of the compressed input stream has been reached.
747 $z->seek($position, $whence);
748 seek($z, $position, $whence);
750 Provides a sub-set of the C<seek> functionality, with the restriction
751 that it is only legal to seek forward in the input file/buffer.
752 It is a fatal error to attempt to seek backward.
754 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
755 SEEK_CUR or SEEK_END.
757 Returns 1 on success, 0 on failure.
766 This is a noop provided for completeness.
772 Returns true if the object currently refers to a opened file/buffer.
776 my $prev = $z->autoflush()
777 my $prev = $z->autoflush(EXPR)
779 If the C<$z> object is associated with a file or a filehandle, this method
780 returns the current autoflush setting for the underlying filehandle. If
781 C<EXPR> is present, and is non-zero, it will enable flushing after every
782 write/print operation.
784 If C<$z> is associated with a buffer, this method has no effect and always
787 B<Note> that the special variable C<$|> B<cannot> be used to set or
788 retrieve the autoflush setting.
790 =head2 input_line_number
792 $z->input_line_number()
793 $z->input_line_number(EXPR)
795 Returns the current uncompressed line number. If C<EXPR> is present it has
796 the effect of setting the line number. Note that setting the line number
797 does not change the current position within the file/buffer being read.
799 The contents of C<$/> are used to to determine what constitutes a line
807 If the C<$z> object is associated with a file or a filehandle, C<fileno>
808 will return the underlying file descriptor. Once the C<close> method is
809 called C<fileno> will return C<undef>.
811 If the C<$z> object is is associated with a buffer, this method will return
819 Closes the output file/buffer.
821 For most versions of Perl this method will be automatically invoked if
822 the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
823 variable with the reference to the object going out of scope). The
824 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
825 these cases, the C<close> method will be called automatically, but
826 not until global destruction of all live objects when the program is
829 Therefore, if you want your scripts to be able to run on all versions
830 of Perl, you should call C<close> explicitly and not rely on automatic
833 Returns true on success, otherwise 0.
835 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
836 object was created, and the object is associated with a file, the
837 underlying file will also be closed.
843 my $status = $z->nextStream();
845 Skips to the next compressed data stream in the input file/buffer. If a new
846 compressed data stream is found, the eof marker will be cleared and C<$.>
849 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
850 error was encountered.
856 my $data = $z->trailingData();
858 Returns the data, if any, that is present immediately after the compressed
859 data stream once uncompression is complete. It only makes sense to call
860 this method once the end of the compressed data stream has been
863 This option can be used when there is useful information immediately
864 following the compressed data stream, and you don't know the length of the
865 compressed data stream.
867 If the input is a buffer, C<trailingData> will return everything from the
868 end of the compressed data stream to the end of the buffer.
870 If the input is a filehandle, C<trailingData> will return the data that is
871 left in the filehandle input buffer once the end of the compressed data
872 stream has been reached. You can then use the filehandle to read the rest
875 Don't bother using C<trailingData> if the input is a filename.
877 If you know the length of the compressed data stream before you start
878 uncompressing, you can avoid having to use C<trailingData> by setting the
879 C<InputLength> option in the constructor.
883 No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
889 Imports C<anyuncompress> and C<$AnyUncompressError>.
892 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
900 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>
902 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
904 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
905 L<Archive::Tar|Archive::Tar>,
910 This module was written by Paul Marquess, F<pmqs@cpan.org>.
912 =head1 MODIFICATION HISTORY
914 See the Changes file.
916 =head1 COPYRIGHT AND LICENSE
918 Copyright (c) 2005-2008 Paul Marquess. All rights reserved.
920 This program is free software; you can redistribute it and/or
921 modify it under the same terms as Perl itself.