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