1 package IO::Uncompress::AnyUncompress ;
7 use IO::Compress::Base::Common qw(createSelfTiedObject);
9 #use IO::Uncompress::Base ;
10 use IO::Uncompress::Gunzip ;
11 use IO::Uncompress::Inflate ;
12 use IO::Uncompress::RawInflate ;
13 use IO::Uncompress::Unzip ;
17 eval { require IO::Uncompress::Adapter::Bunzip2; import IO::Uncompress::Adapter::Bunzip2 };
18 eval { require IO::Uncompress::Adapter::LZO; import IO::Uncompress::Adapter::LZO };
19 eval { require IO::Uncompress::Bunzip2; import IO::Uncompress::Bunzip2 };
20 eval { require IO::Uncompress::UnLzop; import IO::Uncompress::UnLzop };
25 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
27 $VERSION = '2.000_08';
28 $AnyUncompressError = '';
30 @ISA = qw( Exporter IO::Uncompress::Base );
31 @EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
32 %EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
33 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
34 Exporter::export_ok_tags('all');
36 # TODO - allow the user to pick a set of the three formats to allow
37 # or just assume want to auto-detect any of the three formats.
42 my $obj = createSelfTiedObject($class, \$AnyUncompressError);
43 $obj->_create(undef, 0, @_);
48 my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
49 return $obj->_inf(@_) ;
62 # any always needs both crc32 and adler32
63 $got->value('CRC32' => 1);
64 $got->value('ADLER32' => 1);
76 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
78 return $self->saveErrorString(undef, $errstr, $errno)
81 *$self->{Uncomp} = $obj;
83 my $magic = $self->ckMagic( qw( RawInflate Inflate Gunzip Unzip ) );
86 *$self->{Info} = $self->readHeader($magic)
92 #foreach my $type ( qw( Bunzip2 UnLzop ) ) {
93 if (defined $IO::Uncompress::Bunzip2::VERSION and
94 $magic = $self->ckMagic('Bunzip2')) {
95 *$self->{Info} = $self->readHeader($magic)
98 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
100 return $self->saveErrorString(undef, $errstr, $errno)
103 *$self->{Uncomp} = $obj;
107 elsif (defined $IO::Uncompress::UnLzop::VERSION and
108 $magic = $self->ckMagic('UnLzop')) {
110 *$self->{Info} = $self->readHeader($magic)
113 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
115 return $self->saveErrorString(undef, $errstr, $errno)
118 *$self->{Uncomp} = $obj;
133 my $keep = ref $self ;
134 for my $class ( map { "IO::Uncompress::$_" } @names)
136 bless $self => $class;
137 my $magic = $self->ckMagic();
141 #bless $self => $class;
145 $self->pushBack(*$self->{HeaderPending}) ;
146 *$self->{HeaderPending} = '' ;
149 bless $self => $keep;
161 IO::Uncompress::AnyUncompress - Perl interface to read 1950, 1951 & 1952 files/buffers
166 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
168 my $status = anyuncompress $input => $output [,OPTS]
169 or die "anyuncompress failed: $AnyUncompressError\n";
171 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
172 or die "anyuncompress failed: $AnyUncompressError\n";
174 $status = $z->read($buffer)
175 $status = $z->read($buffer, $length)
176 $status = $z->read($buffer, $length, $offset)
177 $line = $z->getline()
183 $data = $z->getHeaderInfo()
185 $z->seek($position, $whence)
191 $AnyUncompressError ;
197 read($z, $buffer, $length);
198 read($z, $buffer, $length, $offset);
200 seek($z, $position, $whence)
211 B<WARNING -- This is a Beta release>.
215 =item * DO NOT use in production code.
217 =item * The documentation is incomplete in places.
219 =item * Parts of the interface defined here are tentative.
221 =item * Please report any problems you find.
229 This module provides a Perl interface that allows the reading of
232 For writing 1950, 1951 & 1952 files/buffers, see the companion module IO::Compress::RawDeflate.
236 =head1 Functional Interface
238 A top-level function, C<anyuncompress>, is provided to carry out
239 "one-shot" uncompression between buffers and/or files. For finer
240 control over the uncompression process, see the L</"OO Interface">
243 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
245 anyuncompress $input => $output [,OPTS]
246 or die "anyuncompress failed: $AnyUncompressError\n";
250 The functional interface needs Perl5.005 or better.
253 =head2 anyuncompress $input => $output [, OPTS]
256 C<anyuncompress> expects at least two parameters, C<$input> and C<$output>.
258 =head3 The C<$input> parameter
260 The parameter, C<$input>, is used to define the source of
263 It can take one of the following forms:
269 If the C<$input> parameter is a simple scalar, it is assumed to be a
270 filename. This file will be opened for reading and the input data
271 will be read from it.
275 If the C<$input> parameter is a filehandle, the input data will be
277 The string '-' can be used as an alias for standard input.
279 =item A scalar reference
281 If C<$input> is a scalar reference, the input data will be read
284 =item An array reference
286 If C<$input> is an array reference, each element in the array must be a
289 The input data will be read from each file in turn.
291 The complete array will be walked to ensure that it only
292 contains valid filenames before any data is uncompressed.
296 =item An Input FileGlob string
298 If C<$input> is a string that is delimited by the characters "<" and ">"
299 C<anyuncompress> will assume that it is an I<input fileglob string>. The
300 input is the list of files that match the fileglob.
302 If the fileglob does not match any files ...
304 See L<File::GlobMapper|File::GlobMapper> for more details.
309 If the C<$input> parameter is any other type, C<undef> will be returned.
313 =head3 The C<$output> parameter
315 The parameter C<$output> is used to control the destination of the
316 uncompressed data. This parameter can take one of these forms.
322 If the C<$output> parameter is a simple scalar, it is assumed to be a
323 filename. This file will be opened for writing and the uncompressed
324 data will be written to it.
328 If the C<$output> parameter is a filehandle, the uncompressed data
329 will be written to it.
330 The string '-' can be used as an alias for standard output.
333 =item A scalar reference
335 If C<$output> is a scalar reference, the uncompressed data will be
336 stored in C<$$output>.
340 =item An Array Reference
342 If C<$output> is an array reference, the uncompressed data will be
343 pushed onto the array.
345 =item An Output FileGlob
347 If C<$output> is a string that is delimited by the characters "<" and ">"
348 C<anyuncompress> will assume that it is an I<output fileglob string>. The
349 output is the list of files that match the fileglob.
351 When C<$output> is an fileglob string, C<$input> must also be a fileglob
352 string. Anything else is an error.
356 If the C<$output> parameter is any other type, C<undef> will be returned.
362 When C<$input> maps to multiple files/buffers and C<$output> is a single
363 file/buffer the uncompressed input files/buffers will all be stored
364 in C<$output> as a single uncompressed stream.
368 =head2 Optional Parameters
370 Unless specified below, the optional parameters for C<anyuncompress>,
371 C<OPTS>, are the same as those used with the OO interface defined in the
372 L</"Constructor Options"> section below.
376 =item AutoClose =E<gt> 0|1
378 This option applies to any input or output data streams to
379 C<anyuncompress> that are filehandles.
381 If C<AutoClose> is specified, and the value is true, it will result in all
382 input and/or output filehandles being closed once C<anyuncompress> has
385 This parameter defaults to 0.
389 =item BinModeOut =E<gt> 0|1
391 When writing to a file or filehandle, set C<binmode> before writing to the
400 =item -Append =E<gt> 0|1
404 =item -MultiStream =E<gt> 0|1
406 Creates a new stream after each file.
419 To read the contents of the file C<file1.txt.Compressed> and write the
420 compressed data to the file C<file1.txt>.
424 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
426 my $input = "file1.txt.Compressed";
427 my $output = "file1.txt";
428 anyuncompress $input => $output
429 or die "anyuncompress failed: $AnyUncompressError\n";
432 To read from an existing Perl filehandle, C<$input>, and write the
433 uncompressed data to a buffer, C<$buffer>.
437 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
440 my $input = new IO::File "<file1.txt.Compressed"
441 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
443 anyuncompress $input => \$buffer
444 or die "anyuncompress failed: $AnyUncompressError\n";
446 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
450 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
452 anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
453 or die "anyuncompress failed: $AnyUncompressError\n";
455 and if you want to compress each file one at a time, this will do the trick
459 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
461 for my $input ( glob "/my/home/*.txt.Compressed" )
464 $output =~ s/.Compressed// ;
465 anyuncompress $input => $output
466 or die "Error compressing '$input': $AnyUncompressError\n";
473 The format of the constructor for IO::Uncompress::AnyUncompress is shown below
476 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
477 or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
479 Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
480 The variable C<$AnyUncompressError> 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::AnyUncompress 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.
508 =item A scalar reference
510 If C<$input> is a scalar reference, the compressed data will be read from
515 =head2 Constructor Options
518 The option names defined below are case insensitive and can be optionally
519 prefixed by a '-'. So all of the following are valid
526 OPTS is a combination of the following options:
530 =item -AutoClose =E<gt> 0|1
532 This option is only valid when the C<$input> parameter is a filehandle. If
533 specified, and the value is true, it will result in the file being closed once
534 either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
537 This parameter defaults to 0.
539 =item -MultiStream =E<gt> 0|1
543 Allows multiple concatenated compressed streams to be treated as a single
544 compressed stream. Decompression will stop once either the end of the
545 file/buffer is reached, an error is encountered (premature eof, corrupt
546 compressed data) or the end of a stream is not immediately followed by the
547 start of another stream.
549 This parameter defaults to 0.
553 =item -Prime =E<gt> $string
555 This option will uncompress the contents of C<$string> before processing the
558 This option can be useful when the compressed data is embedded in another
559 file/data structure and it is not possible to work out where the compressed
560 data begins without having to read the first few bytes. If this is the
561 case, the uncompression can be I<primed> with these bytes using this
564 =item -Transparent =E<gt> 0|1
566 If this option is set and the input file or buffer is not compressed data,
567 the module will allow reading of it anyway.
569 This option defaults to 1.
571 =item -BlockSize =E<gt> $num
573 When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
574 blocks of C<$num> bytes.
576 This option defaults to 4096.
578 =item -InputLength =E<gt> $size
580 When present this option will limit the number of compressed bytes read
581 from the input file/buffer to C<$size>. This option can be used in the
582 situation where there is useful data directly after the compressed data
583 stream and you know beforehand the exact length of the compressed data
586 This option is mostly used when reading from a filehandle, in which case
587 the file pointer will be left pointing to the first byte directly after the
588 compressed data stream.
592 This option defaults to off.
594 =item -Append =E<gt> 0|1
596 This option controls what the C<read> method does with uncompressed data.
598 If set to 1, all uncompressed data will be appended to the output parameter
599 of the C<read> method.
601 If set to 0, the contents of the output parameter of the C<read> method
602 will be overwritten by the uncompressed data.
606 =item -Strict =E<gt> 0|1
610 This option controls whether the extra checks defined below are used when
611 carrying out the decompression. When Strict is on, the extra tests are
612 carried out, when Strict is off they are not.
614 The default for this option is off.
641 $status = $z->read($buffer)
643 Reads a block of compressed data (the size the the compressed block is
644 determined by the C<Buffer> option in the constructor), uncompresses it and
645 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
646 set in the constructor, the uncompressed data will be appended to the
647 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
649 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
650 or a negative number on error.
656 $status = $z->read($buffer, $length)
657 $status = $z->read($buffer, $length, $offset)
659 $status = read($z, $buffer, $length)
660 $status = read($z, $buffer, $length, $offset)
662 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
664 The main difference between this form of the C<read> method and the
665 previous one, is that this one will attempt to return I<exactly> C<$length>
666 bytes. The only circumstances that this function will not is if end-of-file
667 or an IO error is encountered.
669 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
670 or a negative number on error.
677 $line = $z->getline()
682 This method fully supports the use of of the variable C<$/>
683 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
684 determine what constitutes an end of line. Both paragraph mode and file
685 slurp mode are supported.
694 Read a single character.
700 $char = $z->ungetc($string)
709 $hdr = $z->getHeaderInfo();
710 @hdrs = $z->getHeaderInfo();
712 This method returns either a hash reference (in scalar context) or a list
713 or hash references (in array context) that contains information about each
714 of the header fields in the compressed data stream(s).
726 Returns the uncompressed file offset.
737 Returns true if the end of the compressed input stream has been reached.
743 $z->seek($position, $whence);
744 seek($z, $position, $whence);
749 Provides a sub-set of the C<seek> functionality, with the restriction
750 that it is only legal to seek forward in the input file/buffer.
751 It is a fatal error to attempt to seek backward.
755 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
756 SEEK_CUR or SEEK_END.
758 Returns 1 on success, 0 on failure.
767 This is a noop provided for completeness.
773 Returns true if the object currently refers to a opened file/buffer.
777 my $prev = $z->autoflush()
778 my $prev = $z->autoflush(EXPR)
780 If the C<$z> object is associated with a file or a filehandle, this method
781 returns the current autoflush setting for the underlying filehandle. If
782 C<EXPR> is present, and is non-zero, it will enable flushing after every
783 write/print operation.
785 If C<$z> is associated with a buffer, this method has no effect and always
788 B<Note> that the special variable C<$|> B<cannot> be used to set or
789 retrieve the autoflush setting.
791 =head2 input_line_number
793 $z->input_line_number()
794 $z->input_line_number(EXPR)
798 Returns the current uncompressed line number. If C<EXPR> is present it has
799 the effect of setting the line number. Note that setting the line number
800 does not change the current position within the file/buffer being read.
802 The contents of C<$/> are used to to determine what constitutes a line
812 If the C<$z> object is associated with a file or a filehandle, this method
813 will return the underlying file descriptor.
815 If the C<$z> object is is associated with a buffer, this method will
825 Closes the output file/buffer.
829 For most versions of Perl this method will be automatically invoked if
830 the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
831 variable with the reference to the object going out of scope). The
832 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
833 these cases, the C<close> method will be called automatically, but
834 not until global destruction of all live objects when the program is
837 Therefore, if you want your scripts to be able to run on all versions
838 of Perl, you should call C<close> explicitly and not rely on automatic
841 Returns true on success, otherwise 0.
843 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
844 object was created, and the object is associated with a file, the
845 underlying file will also be closed.
852 No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
858 Imports C<anyuncompress> and C<$AnyUncompressError>.
861 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
872 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>
874 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
876 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
877 L<Archive::Tar|Archive::Tar>,
889 The I<IO::Uncompress::AnyUncompress> module was written by Paul Marquess,
894 =head1 MODIFICATION HISTORY
896 See the Changes file.
898 =head1 COPYRIGHT AND LICENSE
901 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
903 This program is free software; you can redistribute it and/or
904 modify it under the same terms as Perl itself.