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