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