xref: /openbsd-src/gnu/usr.bin/perl/cpan/IO-Compress/lib/IO/Uncompress/AnyUncompress.pm (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1package IO::Uncompress::AnyUncompress ;
2
3use strict;
4use warnings;
5use bytes;
6
7use IO::Compress::Base::Common 2.064 ();
8
9use IO::Uncompress::Base 2.064 ;
10
11
12require Exporter ;
13
14our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
15
16$VERSION = '2.064_01';
17$AnyUncompressError = '';
18
19@ISA = qw( Exporter IO::Uncompress::Base );
20@EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
21%EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
22push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
23Exporter::export_ok_tags('all');
24
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.
27
28BEGIN
29{
30   local @INC = @INC;
31   pop @INC if $INC[-1] eq '.';
32   eval ' use IO::Uncompress::Adapter::Inflate 2.064 ;';
33   eval ' use IO::Uncompress::Adapter::Bunzip2 2.064 ;';
34   eval ' use IO::Uncompress::Adapter::LZO 2.064 ;';
35   eval ' use IO::Uncompress::Adapter::Lzf 2.064 ;';
36   eval ' use IO::Uncompress::Adapter::UnLzma 2.064 ;';
37   eval ' use IO::Uncompress::Adapter::UnXz 2.064 ;';
38
39   eval ' use IO::Uncompress::Bunzip2 2.064 ;';
40   eval ' use IO::Uncompress::UnLzop 2.064 ;';
41   eval ' use IO::Uncompress::Gunzip 2.064 ;';
42   eval ' use IO::Uncompress::Inflate 2.064 ;';
43   eval ' use IO::Uncompress::RawInflate 2.064 ;';
44   eval ' use IO::Uncompress::Unzip 2.064 ;';
45   eval ' use IO::Uncompress::UnLzf 2.064 ;';
46   eval ' use IO::Uncompress::UnLzma 2.064 ;';
47   eval ' use IO::Uncompress::UnXz 2.064 ;';
48}
49
50sub new
51{
52    my $class = shift ;
53    my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$AnyUncompressError);
54    $obj->_create(undef, 0, @_);
55}
56
57sub anyuncompress
58{
59    my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$AnyUncompressError);
60    return $obj->_inf(@_) ;
61}
62
63sub getExtraParams
64{
65    return ( 'rawinflate' => [IO::Compress::Base::Common::Parse_boolean,  0] ,
66             'unlzma'     => [IO::Compress::Base::Common::Parse_boolean,  0] ) ;
67}
68
69sub ckParams
70{
71    my $self = shift ;
72    my $got = shift ;
73
74    # any always needs both crc32 and adler32
75    $got->setValue('crc32' => 1);
76    $got->setValue('adler32' => 1);
77
78    return 1;
79}
80
81sub mkUncomp
82{
83    my $self = shift ;
84    my $got = shift ;
85
86    my $magic ;
87
88    # try zlib first
89    if (defined $IO::Uncompress::RawInflate::VERSION )
90    {
91        my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
92
93        return $self->saveErrorString(undef, $errstr, $errno)
94            if ! defined $obj;
95
96        *$self->{Uncomp} = $obj;
97
98        my @possible = qw( Inflate Gunzip Unzip );
99        unshift @possible, 'RawInflate'
100            if $got->getValue('rawinflate');
101
102        $magic = $self->ckMagic( @possible );
103
104        if ($magic) {
105            *$self->{Info} = $self->readHeader($magic)
106                or return undef ;
107
108            return 1;
109        }
110     }
111
112    if (defined $IO::Uncompress::UnLzma::VERSION && $got->getValue('unlzma'))
113    {
114        my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::UnLzma::mkUncompObject();
115
116        return $self->saveErrorString(undef, $errstr, $errno)
117            if ! defined $obj;
118
119        *$self->{Uncomp} = $obj;
120
121        my @possible = qw( UnLzma );
122        #unshift @possible, 'RawInflate'
123        #    if $got->getValue('rawinflate');
124
125        if ( *$self->{Info} = $self->ckMagic( @possible ))
126        {
127            return 1;
128        }
129     }
130
131     if (defined $IO::Uncompress::UnXz::VERSION and
132         $magic = $self->ckMagic('UnXz')) {
133        *$self->{Info} = $self->readHeader($magic)
134            or return undef ;
135
136        my ($obj, $errstr, $errno) =
137            IO::Uncompress::Adapter::UnXz::mkUncompObject();
138
139        return $self->saveErrorString(undef, $errstr, $errno)
140            if ! defined $obj;
141
142        *$self->{Uncomp} = $obj;
143
144         return 1;
145     }
146
147     if (defined $IO::Uncompress::Bunzip2::VERSION and
148         $magic = $self->ckMagic('Bunzip2')) {
149        *$self->{Info} = $self->readHeader($magic)
150            or return undef ;
151
152        my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
153
154        return $self->saveErrorString(undef, $errstr, $errno)
155            if ! defined $obj;
156
157        *$self->{Uncomp} = $obj;
158
159         return 1;
160     }
161
162     if (defined $IO::Uncompress::UnLzop::VERSION and
163            $magic = $self->ckMagic('UnLzop')) {
164
165        *$self->{Info} = $self->readHeader($magic)
166            or return undef ;
167
168        my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
169
170        return $self->saveErrorString(undef, $errstr, $errno)
171            if ! defined $obj;
172
173        *$self->{Uncomp} = $obj;
174
175         return 1;
176     }
177
178     if (defined $IO::Uncompress::UnLzf::VERSION and
179            $magic = $self->ckMagic('UnLzf')) {
180
181        *$self->{Info} = $self->readHeader($magic)
182            or return undef ;
183
184        my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Lzf::mkUncompObject();
185
186        return $self->saveErrorString(undef, $errstr, $errno)
187            if ! defined $obj;
188
189        *$self->{Uncomp} = $obj;
190
191         return 1;
192     }
193
194     return 0 ;
195}
196
197
198
199sub ckMagic
200{
201    my $self = shift;
202    my @names = @_ ;
203
204    my $keep = ref $self ;
205    for my $class ( map { "IO::Uncompress::$_" } @names)
206    {
207        bless $self => $class;
208        my $magic = $self->ckMagic();
209
210        if ($magic)
211        {
212            #bless $self => $class;
213            return $magic ;
214        }
215
216        $self->pushBack(*$self->{HeaderPending})  ;
217        *$self->{HeaderPending} = ''  ;
218    }
219
220    bless $self => $keep;
221    return undef;
222}
223
2241 ;
225
226__END__
227
228
229=head1 NAME
230
231IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop file/buffer
232
233=head1 SYNOPSIS
234
235    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
236
237    my $status = anyuncompress $input => $output [,OPTS]
238        or die "anyuncompress failed: $AnyUncompressError\n";
239
240    my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
241        or die "anyuncompress failed: $AnyUncompressError\n";
242
243    $status = $z->read($buffer)
244    $status = $z->read($buffer, $length)
245    $status = $z->read($buffer, $length, $offset)
246    $line = $z->getline()
247    $char = $z->getc()
248    $char = $z->ungetc()
249    $char = $z->opened()
250
251    $data = $z->trailingData()
252    $status = $z->nextStream()
253    $data = $z->getHeaderInfo()
254    $z->tell()
255    $z->seek($position, $whence)
256    $z->binmode()
257    $z->fileno()
258    $z->eof()
259    $z->close()
260
261    $AnyUncompressError ;
262
263    # IO::File mode
264
265    <$z>
266    read($z, $buffer);
267    read($z, $buffer, $length);
268    read($z, $buffer, $length, $offset);
269    tell($z)
270    seek($z, $position, $whence)
271    binmode($z)
272    fileno($z)
273    eof($z)
274    close($z)
275
276=head1 DESCRIPTION
277
278This module provides a Perl interface that allows the reading of
279files/buffers that have been compressed with a variety of compression
280libraries.
281
282The formats supported are:
283
284=over 5
285
286=item RFC 1950
287
288=item RFC 1951 (optionally)
289
290=item gzip (RFC 1952)
291
292=item zip
293
294=item bzip2
295
296=item lzop
297
298=item lzf
299
300=item lzma
301
302=item xz
303
304=back
305
306The module will auto-detect which, if any, of the supported
307compression formats is being used.
308
309=head1 Functional Interface
310
311A top-level function, C<anyuncompress>, is provided to carry out
312"one-shot" uncompression between buffers and/or files. For finer
313control over the uncompression process, see the L</"OO Interface">
314section.
315
316    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
317
318    anyuncompress $input_filename_or_reference => $output_filename_or_reference [,OPTS]
319        or die "anyuncompress failed: $AnyUncompressError\n";
320
321The functional interface needs Perl5.005 or better.
322
323=head2 anyuncompress $input_filename_or_reference => $output_filename_or_reference [, OPTS]
324
325C<anyuncompress> expects at least two parameters,
326C<$input_filename_or_reference> and C<$output_filename_or_reference>.
327
328=head3 The C<$input_filename_or_reference> parameter
329
330The parameter, C<$input_filename_or_reference>, is used to define the
331source of the compressed data.
332
333It can take one of the following forms:
334
335=over 5
336
337=item A filename
338
339If the <$input_filename_or_reference> parameter is a simple scalar, it is
340assumed to be a filename. This file will be opened for reading and the
341input data will be read from it.
342
343=item A filehandle
344
345If the C<$input_filename_or_reference> parameter is a filehandle, the input
346data will be read from it.  The string '-' can be used as an alias for
347standard input.
348
349=item A scalar reference
350
351If C<$input_filename_or_reference> is a scalar reference, the input data
352will be read from C<$$input_filename_or_reference>.
353
354=item An array reference
355
356If C<$input_filename_or_reference> is an array reference, each element in
357the array must be a filename.
358
359The input data will be read from each file in turn.
360
361The complete array will be walked to ensure that it only
362contains valid filenames before any data is uncompressed.
363
364=item An Input FileGlob string
365
366If C<$input_filename_or_reference> is a string that is delimited by the
367characters "<" and ">" C<anyuncompress> will assume that it is an
368I<input fileglob string>. The input is the list of files that match the
369fileglob.
370
371See L<File::GlobMapper|File::GlobMapper> for more details.
372
373=back
374
375If the C<$input_filename_or_reference> parameter is any other type,
376C<undef> will be returned.
377
378=head3 The C<$output_filename_or_reference> parameter
379
380The parameter C<$output_filename_or_reference> is used to control the
381destination of the uncompressed data. This parameter can take one of
382these forms.
383
384=over 5
385
386=item A filename
387
388If the C<$output_filename_or_reference> parameter is a simple scalar, it is
389assumed to be a filename.  This file will be opened for writing and the
390uncompressed data will be written to it.
391
392=item A filehandle
393
394If the C<$output_filename_or_reference> parameter is a filehandle, the
395uncompressed data will be written to it.  The string '-' can be used as
396an alias for standard output.
397
398=item A scalar reference
399
400If C<$output_filename_or_reference> is a scalar reference, the
401uncompressed data will be stored in C<$$output_filename_or_reference>.
402
403=item An Array Reference
404
405If C<$output_filename_or_reference> is an array reference,
406the uncompressed data will be pushed onto the array.
407
408=item An Output FileGlob
409
410If C<$output_filename_or_reference> is a string that is delimited by the
411characters "<" and ">" C<anyuncompress> will assume that it is an
412I<output fileglob string>. The output is the list of files that match the
413fileglob.
414
415When C<$output_filename_or_reference> is an fileglob string,
416C<$input_filename_or_reference> must also be a fileglob string. Anything
417else is an error.
418
419See L<File::GlobMapper|File::GlobMapper> for more details.
420
421=back
422
423If the C<$output_filename_or_reference> parameter is any other type,
424C<undef> will be returned.
425
426=head2 Notes
427
428When C<$input_filename_or_reference> maps to multiple compressed
429files/buffers and C<$output_filename_or_reference> is
430a single file/buffer, after uncompression C<$output_filename_or_reference> will contain a
431concatenation of all the uncompressed data from each of the input
432files/buffers.
433
434=head2 Optional Parameters
435
436Unless specified below, the optional parameters for C<anyuncompress>,
437C<OPTS>, are the same as those used with the OO interface defined in the
438L</"Constructor Options"> section below.
439
440=over 5
441
442=item C<< AutoClose => 0|1 >>
443
444This option applies to any input or output data streams to
445C<anyuncompress> that are filehandles.
446
447If C<AutoClose> is specified, and the value is true, it will result in all
448input and/or output filehandles being closed once C<anyuncompress> has
449completed.
450
451This parameter defaults to 0.
452
453=item C<< BinModeOut => 0|1 >>
454
455When writing to a file or filehandle, set C<binmode> before writing to the
456file.
457
458Defaults to 0.
459
460=item C<< Append => 0|1 >>
461
462The behaviour of this option is dependent on the type of output data
463stream.
464
465=over 5
466
467=item * A Buffer
468
469If C<Append> is enabled, all uncompressed data will be append to the end of
470the output buffer. Otherwise the output buffer will be cleared before any
471uncompressed data is written to it.
472
473=item * A Filename
474
475If C<Append> is enabled, the file will be opened in append mode. Otherwise
476the contents of the file, if any, will be truncated before any uncompressed
477data is written to it.
478
479=item * A Filehandle
480
481If C<Append> is enabled, the filehandle will be positioned to the end of
482the file via a call to C<seek> before any uncompressed data is
483written to it.  Otherwise the file pointer will not be moved.
484
485=back
486
487When C<Append> is specified, and set to true, it will I<append> all uncompressed
488data to the output data stream.
489
490So when the output is a filehandle it will carry out a seek to the eof
491before writing any uncompressed data. If the output is a filename, it will be opened for
492appending. If the output is a buffer, all uncompressed data will be
493appended to the existing buffer.
494
495Conversely when C<Append> is not specified, or it is present and is set to
496false, it will operate as follows.
497
498When the output is a filename, it will truncate the contents of the file
499before writing any uncompressed data. If the output is a filehandle
500its position will not be changed. If the output is a buffer, it will be
501wiped before any uncompressed data is output.
502
503Defaults to 0.
504
505=item C<< MultiStream => 0|1 >>
506
507If the input file/buffer contains multiple compressed data streams, this
508option will uncompress the whole lot as a single data stream.
509
510Defaults to 0.
511
512=item C<< TrailingData => $scalar >>
513
514Returns the data, if any, that is present immediately after the compressed
515data stream once uncompression is complete.
516
517This option can be used when there is useful information immediately
518following the compressed data stream, and you don't know the length of the
519compressed data stream.
520
521If the input is a buffer, C<trailingData> will return everything from the
522end of the compressed data stream to the end of the buffer.
523
524If the input is a filehandle, C<trailingData> will return the data that is
525left in the filehandle input buffer once the end of the compressed data
526stream has been reached. You can then use the filehandle to read the rest
527of the input file.
528
529Don't bother using C<trailingData> if the input is a filename.
530
531If you know the length of the compressed data stream before you start
532uncompressing, you can avoid having to use C<trailingData> by setting the
533C<InputLength> option.
534
535=back
536
537=head2 Examples
538
539To read the contents of the file C<file1.txt.Compressed> and write the
540uncompressed data to the file C<file1.txt>.
541
542    use strict ;
543    use warnings ;
544    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
545
546    my $input = "file1.txt.Compressed";
547    my $output = "file1.txt";
548    anyuncompress $input => $output
549        or die "anyuncompress failed: $AnyUncompressError\n";
550
551To read from an existing Perl filehandle, C<$input>, and write the
552uncompressed data to a buffer, C<$buffer>.
553
554    use strict ;
555    use warnings ;
556    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
557    use IO::File ;
558
559    my $input = new IO::File "<file1.txt.Compressed"
560        or die "Cannot open 'file1.txt.Compressed': $!\n" ;
561    my $buffer ;
562    anyuncompress $input => \$buffer
563        or die "anyuncompress failed: $AnyUncompressError\n";
564
565To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
566
567    use strict ;
568    use warnings ;
569    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
570
571    anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
572        or die "anyuncompress failed: $AnyUncompressError\n";
573
574and if you want to compress each file one at a time, this will do the trick
575
576    use strict ;
577    use warnings ;
578    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
579
580    for my $input ( glob "/my/home/*.txt.Compressed" )
581    {
582        my $output = $input;
583        $output =~ s/.Compressed// ;
584        anyuncompress $input => $output
585            or die "Error compressing '$input': $AnyUncompressError\n";
586    }
587
588=head1 OO Interface
589
590=head2 Constructor
591
592The format of the constructor for IO::Uncompress::AnyUncompress is shown below
593
594    my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
595        or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
596
597Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
598The variable C<$AnyUncompressError> will contain an error message on failure.
599
600If you are running Perl 5.005 or better the object, C<$z>, returned from
601IO::Uncompress::AnyUncompress can be used exactly like an L<IO::File|IO::File> filehandle.
602This means that all normal input file operations can be carried out with
603C<$z>.  For example, to read a line from a compressed file/buffer you can
604use either of these forms
605
606    $line = $z->getline();
607    $line = <$z>;
608
609The mandatory parameter C<$input> is used to determine the source of the
610compressed data. This parameter can take one of three forms.
611
612=over 5
613
614=item A filename
615
616If the C<$input> parameter is a scalar, it is assumed to be a filename. This
617file will be opened for reading and the compressed data will be read from it.
618
619=item A filehandle
620
621If the C<$input> parameter is a filehandle, the compressed data will be
622read from it.
623The string '-' can be used as an alias for standard input.
624
625=item A scalar reference
626
627If C<$input> is a scalar reference, the compressed data will be read from
628C<$$input>.
629
630=back
631
632=head2 Constructor Options
633
634The option names defined below are case insensitive and can be optionally
635prefixed by a '-'.  So all of the following are valid
636
637    -AutoClose
638    -autoclose
639    AUTOCLOSE
640    autoclose
641
642OPTS is a combination of the following options:
643
644=over 5
645
646=item C<< AutoClose => 0|1 >>
647
648This option is only valid when the C<$input> parameter is a filehandle. If
649specified, and the value is true, it will result in the file being closed once
650either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
651destroyed.
652
653This parameter defaults to 0.
654
655=item C<< MultiStream => 0|1 >>
656
657Allows multiple concatenated compressed streams to be treated as a single
658compressed stream. Decompression will stop once either the end of the
659file/buffer is reached, an error is encountered (premature eof, corrupt
660compressed data) or the end of a stream is not immediately followed by the
661start of another stream.
662
663This parameter defaults to 0.
664
665=item C<< Prime => $string >>
666
667This option will uncompress the contents of C<$string> before processing the
668input file/buffer.
669
670This option can be useful when the compressed data is embedded in another
671file/data structure and it is not possible to work out where the compressed
672data begins without having to read the first few bytes. If this is the
673case, the uncompression can be I<primed> with these bytes using this
674option.
675
676=item C<< Transparent => 0|1 >>
677
678If this option is set and the input file/buffer is not compressed data,
679the module will allow reading of it anyway.
680
681In addition, if the input file/buffer does contain compressed data and
682there is non-compressed data immediately following it, setting this option
683will make this module treat the whole file/buffer as a single data stream.
684
685This option defaults to 1.
686
687=item C<< BlockSize => $num >>
688
689When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
690blocks of C<$num> bytes.
691
692This option defaults to 4096.
693
694=item C<< InputLength => $size >>
695
696When present this option will limit the number of compressed bytes read
697from the input file/buffer to C<$size>. This option can be used in the
698situation where there is useful data directly after the compressed data
699stream and you know beforehand the exact length of the compressed data
700stream.
701
702This option is mostly used when reading from a filehandle, in which case
703the file pointer will be left pointing to the first byte directly after the
704compressed data stream.
705
706This option defaults to off.
707
708=item C<< Append => 0|1 >>
709
710This option controls what the C<read> method does with uncompressed data.
711
712If set to 1, all uncompressed data will be appended to the output parameter
713of the C<read> method.
714
715If set to 0, the contents of the output parameter of the C<read> method
716will be overwritten by the uncompressed data.
717
718Defaults to 0.
719
720=item C<< Strict => 0|1 >>
721
722This option controls whether the extra checks defined below are used when
723carrying out the decompression. When Strict is on, the extra tests are
724carried out, when Strict is off they are not.
725
726The default for this option is off.
727
728=item C<< RawInflate => 0|1 >>
729
730When auto-detecting the compressed format, try to test for raw-deflate (RFC
7311951) content using the C<IO::Uncompress::RawInflate> module.
732
733The reason this is not default behaviour is because RFC 1951 content can
734only be detected by attempting to uncompress it. This process is error
735prone and can result is false positives.
736
737Defaults to 0.
738
739=item C<< UnLzma => 0|1 >>
740
741When auto-detecting the compressed format, try to test for lzma_alone
742content using the C<IO::Uncompress::UnLzma> module.
743
744The reason this is not default behaviour is because lzma_alone content can
745only be detected by attempting to uncompress it. This process is error
746prone and can result is false positives.
747
748Defaults to 0.
749
750=back
751
752=head2 Examples
753
754TODO
755
756=head1 Methods
757
758=head2 read
759
760Usage is
761
762    $status = $z->read($buffer)
763
764Reads a block of compressed data (the size of the compressed block is
765determined by the C<Buffer> option in the constructor), uncompresses it and
766writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
767set in the constructor, the uncompressed data will be appended to the
768C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
769
770Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
771or a negative number on error.
772
773=head2 read
774
775Usage is
776
777    $status = $z->read($buffer, $length)
778    $status = $z->read($buffer, $length, $offset)
779
780    $status = read($z, $buffer, $length)
781    $status = read($z, $buffer, $length, $offset)
782
783Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
784
785The main difference between this form of the C<read> method and the
786previous one, is that this one will attempt to return I<exactly> C<$length>
787bytes. The only circumstances that this function will not is if end-of-file
788or an IO error is encountered.
789
790Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
791or a negative number on error.
792
793=head2 getline
794
795Usage is
796
797    $line = $z->getline()
798    $line = <$z>
799
800Reads a single line.
801
802This method fully supports the use of the variable C<$/> (or
803C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
804determine what constitutes an end of line. Paragraph mode, record mode and
805file slurp mode are all supported.
806
807=head2 getc
808
809Usage is
810
811    $char = $z->getc()
812
813Read a single character.
814
815=head2 ungetc
816
817Usage is
818
819    $char = $z->ungetc($string)
820
821=head2 getHeaderInfo
822
823Usage is
824
825    $hdr  = $z->getHeaderInfo();
826    @hdrs = $z->getHeaderInfo();
827
828This method returns either a hash reference (in scalar context) or a list
829or hash references (in array context) that contains information about each
830of the header fields in the compressed data stream(s).
831
832=head2 tell
833
834Usage is
835
836    $z->tell()
837    tell $z
838
839Returns the uncompressed file offset.
840
841=head2 eof
842
843Usage is
844
845    $z->eof();
846    eof($z);
847
848Returns true if the end of the compressed input stream has been reached.
849
850=head2 seek
851
852    $z->seek($position, $whence);
853    seek($z, $position, $whence);
854
855Provides a sub-set of the C<seek> functionality, with the restriction
856that it is only legal to seek forward in the input file/buffer.
857It is a fatal error to attempt to seek backward.
858
859Note that the implementation of C<seek> in this module does not provide
860true random access to a compressed file/buffer. It  works by uncompressing
861data from the current offset in the file/buffer until it reaches the
862uncompressed offset specified in the parameters to C<seek>. For very small
863files this may be acceptable behaviour. For large files it may cause an
864unacceptable delay.
865
866The C<$whence> parameter takes one the usual values, namely SEEK_SET,
867SEEK_CUR or SEEK_END.
868
869Returns 1 on success, 0 on failure.
870
871=head2 binmode
872
873Usage is
874
875    $z->binmode
876    binmode $z ;
877
878This is a noop provided for completeness.
879
880=head2 opened
881
882    $z->opened()
883
884Returns true if the object currently refers to a opened file/buffer.
885
886=head2 autoflush
887
888    my $prev = $z->autoflush()
889    my $prev = $z->autoflush(EXPR)
890
891If the C<$z> object is associated with a file or a filehandle, this method
892returns the current autoflush setting for the underlying filehandle. If
893C<EXPR> is present, and is non-zero, it will enable flushing after every
894write/print operation.
895
896If C<$z> is associated with a buffer, this method has no effect and always
897returns C<undef>.
898
899B<Note> that the special variable C<$|> B<cannot> be used to set or
900retrieve the autoflush setting.
901
902=head2 input_line_number
903
904    $z->input_line_number()
905    $z->input_line_number(EXPR)
906
907Returns the current uncompressed line number. If C<EXPR> is present it has
908the effect of setting the line number. Note that setting the line number
909does not change the current position within the file/buffer being read.
910
911The contents of C<$/> are used to determine what constitutes a line
912terminator.
913
914=head2 fileno
915
916    $z->fileno()
917    fileno($z)
918
919If the C<$z> object is associated with a file or a filehandle, C<fileno>
920will return the underlying file descriptor. Once the C<close> method is
921called C<fileno> will return C<undef>.
922
923If the C<$z> object is associated with a buffer, this method will return
924C<undef>.
925
926=head2 close
927
928    $z->close() ;
929    close $z ;
930
931Closes the output file/buffer.
932
933For most versions of Perl this method will be automatically invoked if
934the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
935variable with the reference to the object going out of scope). The
936exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
937these cases, the C<close> method will be called automatically, but
938not until global destruction of all live objects when the program is
939terminating.
940
941Therefore, if you want your scripts to be able to run on all versions
942of Perl, you should call C<close> explicitly and not rely on automatic
943closing.
944
945Returns true on success, otherwise 0.
946
947If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
948object was created, and the object is associated with a file, the
949underlying file will also be closed.
950
951=head2 nextStream
952
953Usage is
954
955    my $status = $z->nextStream();
956
957Skips to the next compressed data stream in the input file/buffer. If a new
958compressed data stream is found, the eof marker will be cleared and C<$.>
959will be reset to 0.
960
961Returns 1 if a new stream was found, 0 if none was found, and -1 if an
962error was encountered.
963
964=head2 trailingData
965
966Usage is
967
968    my $data = $z->trailingData();
969
970Returns the data, if any, that is present immediately after the compressed
971data stream once uncompression is complete. It only makes sense to call
972this method once the end of the compressed data stream has been
973encountered.
974
975This option can be used when there is useful information immediately
976following the compressed data stream, and you don't know the length of the
977compressed data stream.
978
979If the input is a buffer, C<trailingData> will return everything from the
980end of the compressed data stream to the end of the buffer.
981
982If the input is a filehandle, C<trailingData> will return the data that is
983left in the filehandle input buffer once the end of the compressed data
984stream has been reached. You can then use the filehandle to read the rest
985of the input file.
986
987Don't bother using C<trailingData> if the input is a filename.
988
989If you know the length of the compressed data stream before you start
990uncompressing, you can avoid having to use C<trailingData> by setting the
991C<InputLength> option in the constructor.
992
993=head1 Importing
994
995No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
996
997=over 5
998
999=item :all
1000
1001Imports C<anyuncompress> and C<$AnyUncompressError>.
1002Same as doing this
1003
1004    use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
1005
1006=back
1007
1008=head1 EXAMPLES
1009
1010=head1 SEE ALSO
1011
1012L<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::AnyInflate>
1013
1014L<IO::Compress::FAQ|IO::Compress::FAQ>
1015
1016L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1017L<Archive::Tar|Archive::Tar>,
1018L<IO::Zlib|IO::Zlib>
1019
1020=head1 AUTHOR
1021
1022This module was written by Paul Marquess, F<pmqs@cpan.org>.
1023
1024=head1 MODIFICATION HISTORY
1025
1026See the Changes file.
1027
1028=head1 COPYRIGHT AND LICENSE
1029
1030Copyright (c) 2005-2014 Paul Marquess. All rights reserved.
1031
1032This program is free software; you can redistribute it and/or
1033modify it under the same terms as Perl itself.
1034
1035