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