File Coverage

blib/lib/IO/Uncompress/AnyUncompress.pm
Criterion Covered Total %
statement 120 175 68.5
branch 20 56 35.7
condition 8 21 38.1
subroutine 31 31 100.0
pod 2 6 33.3
total 181 289 62.6


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