1IO::Uncompress::AnyUncomPperrelssP(r3opgmr)ammers ReferIeOn:c:eUnGcuoimdperess::AnyUncompress(3pm)
2
3
4

NAME

6       IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop
7       file/buffer
8

SYNOPSIS

10           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
11
12           my $status = anyuncompress $input => $output [,OPTS]
13               or die "anyuncompress failed: $AnyUncompressError\n";
14
15           my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
16               or die "anyuncompress failed: $AnyUncompressError\n";
17
18           $status = $z->read($buffer)
19           $status = $z->read($buffer, $length)
20           $status = $z->read($buffer, $length, $offset)
21           $line = $z->getline()
22           $char = $z->getc()
23           $char = $z->ungetc()
24           $char = $z->opened()
25
26           $data = $z->trailingData()
27           $status = $z->nextStream()
28           $data = $z->getHeaderInfo()
29           $z->tell()
30           $z->seek($position, $whence)
31           $z->binmode()
32           $z->fileno()
33           $z->eof()
34           $z->close()
35
36           $AnyUncompressError ;
37
38           # IO::File mode
39
40           <$z>
41           read($z, $buffer);
42           read($z, $buffer, $length);
43           read($z, $buffer, $length, $offset);
44           tell($z)
45           seek($z, $position, $whence)
46           binmode($z)
47           fileno($z)
48           eof($z)
49           close($z)
50

DESCRIPTION

52       This module provides a Perl interface that allows the reading of
53       files/buffers that have been compressed with a variety of compression
54       libraries.
55
56       The formats supported are:
57
58       RFC 1950
59       RFC 1951 (optionally)
60       gzip (RFC 1952)
61       zip
62       bzip2
63       lzop
64       lzf
65       lzma
66       xz
67
68       The module will auto-detect which, if any, of the supported compression
69       formats is being used.
70

Functional Interface

72       A top-level function, "anyuncompress", is provided to carry out "one-
73       shot" uncompression between buffers and/or files. For finer control
74       over the uncompression process, see the "OO Interface" section.
75
76           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
77
78           anyuncompress $input => $output [,OPTS]
79               or die "anyuncompress failed: $AnyUncompressError\n";
80
81       The functional interface needs Perl5.005 or better.
82
83   anyuncompress $input => $output [, OPTS]
84       "anyuncompress" expects at least two parameters, $input and $output.
85
86       The $input parameter
87
88       The parameter, $input, is used to define the source of the compressed
89       data.
90
91       It can take one of the following forms:
92
93       A filename
94            If the $input parameter is a simple scalar, it is assumed to be a
95            filename. This file will be opened for reading and the input data
96            will be read from it.
97
98       A filehandle
99            If the $input parameter is a filehandle, the input data will be
100            read from it.  The string '-' can be used as an alias for standard
101            input.
102
103       A scalar reference
104            If $input is a scalar reference, the input data will be read from
105            $$input.
106
107       An array reference
108            If $input is an array reference, each element in the array must be
109            a filename.
110
111            The input data will be read from each file in turn.
112
113            The complete array will be walked to ensure that it only contains
114            valid filenames before any data is uncompressed.
115
116       An Input FileGlob string
117            If $input is a string that is delimited by the characters "<" and
118            ">" "anyuncompress" will assume that it is an input fileglob
119            string. The input is the list of files that match the fileglob.
120
121            If the fileglob does not match any files ...
122
123            See File::GlobMapper for more details.
124
125       If the $input parameter is any other type, "undef" will be returned.
126
127       The $output parameter
128
129       The parameter $output is used to control the destination of the
130       uncompressed data. This parameter can take one of these forms.
131
132       A filename
133            If the $output parameter is a simple scalar, it is assumed to be a
134            filename.  This file will be opened for writing and the
135            uncompressed data will be written to it.
136
137       A filehandle
138            If the $output parameter is a filehandle, the uncompressed data
139            will be written to it.  The string '-' can be used as an alias for
140            standard output.
141
142       A scalar reference
143            If $output is a scalar reference, the uncompressed data will be
144            stored in $$output.
145
146       An Array Reference
147            If $output is an array reference, the uncompressed data will be
148            pushed onto the array.
149
150       An Output FileGlob
151            If $output is a string that is delimited by the characters "<" and
152            ">" "anyuncompress" will assume that it is an output fileglob
153            string. The output is the list of files that match the fileglob.
154
155            When $output is an fileglob string, $input must also be a fileglob
156            string. Anything else is an error.
157
158       If the $output parameter is any other type, "undef" will be returned.
159
160   Notes
161       When $input maps to multiple compressed files/buffers and $output is a
162       single file/buffer, after uncompression $output will contain a
163       concatenation of all the uncompressed data from each of the input
164       files/buffers.
165
166   Optional Parameters
167       Unless specified below, the optional parameters for "anyuncompress",
168       "OPTS", are the same as those used with the OO interface defined in the
169       "Constructor Options" section below.
170
171       "AutoClose => 0|1"
172            This option applies to any input or output data streams to
173            "anyuncompress" that are filehandles.
174
175            If "AutoClose" is specified, and the value is true, it will result
176            in all input and/or output filehandles being closed once
177            "anyuncompress" has completed.
178
179            This parameter defaults to 0.
180
181       "BinModeOut => 0|1"
182            When writing to a file or filehandle, set "binmode" before writing
183            to the file.
184
185            Defaults to 0.
186
187       "Append => 0|1"
188            The behaviour of this option is dependent on the type of output
189            data stream.
190
191            ·    A Buffer
192
193                 If "Append" is enabled, all uncompressed data will be append
194                 to the end of the output buffer. Otherwise the output buffer
195                 will be cleared before any uncompressed data is written to
196                 it.
197
198            ·    A Filename
199
200                 If "Append" is enabled, the file will be opened in append
201                 mode. Otherwise the contents of the file, if any, will be
202                 truncated before any uncompressed data is written to it.
203
204            ·    A Filehandle
205
206                 If "Append" is enabled, the filehandle will be positioned to
207                 the end of the file via a call to "seek" before any
208                 uncompressed data is written to it.  Otherwise the file
209                 pointer will not be moved.
210
211            When "Append" is specified, and set to true, it will append all
212            uncompressed data to the output data stream.
213
214            So when the output is a filehandle it will carry out a seek to the
215            eof before writing any uncompressed data. If the output is a
216            filename, it will be opened for appending. If the output is a
217            buffer, all uncompressed data will be appened to the existing
218            buffer.
219
220            Conversely when "Append" is not specified, or it is present and is
221            set to false, it will operate as follows.
222
223            When the output is a filename, it will truncate the contents of
224            the file before writing any uncompressed data. If the output is a
225            filehandle its position will not be changed. If the output is a
226            buffer, it will be wiped before any uncompressed data is output.
227
228            Defaults to 0.
229
230       "MultiStream => 0|1"
231            If the input file/buffer contains multiple compressed data
232            streams, this option will uncompress the whole lot as a single
233            data stream.
234
235            Defaults to 0.
236
237       "TrailingData => $scalar"
238            Returns the data, if any, that is present immediately after the
239            compressed data stream once uncompression is complete.
240
241            This option can be used when there is useful information
242            immediately following the compressed data stream, and you don't
243            know the length of the compressed data stream.
244
245            If the input is a buffer, "trailingData" will return everything
246            from the end of the compressed data stream to the end of the
247            buffer.
248
249            If the input is a filehandle, "trailingData" will return the data
250            that is left in the filehandle input buffer once the end of the
251            compressed data stream has been reached. You can then use the
252            filehandle to read the rest of the input file.
253
254            Don't bother using "trailingData" if the input is a filename.
255
256            If you know the length of the compressed data stream before you
257            start uncompressing, you can avoid having to use "trailingData" by
258            setting the "InputLength" option.
259
260   Examples
261       To read the contents of the file "file1.txt.Compressed" and write the
262       uncompressed data to the file "file1.txt".
263
264           use strict ;
265           use warnings ;
266           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
267
268           my $input = "file1.txt.Compressed";
269           my $output = "file1.txt";
270           anyuncompress $input => $output
271               or die "anyuncompress failed: $AnyUncompressError\n";
272
273       To read from an existing Perl filehandle, $input, and write the
274       uncompressed data to a buffer, $buffer.
275
276           use strict ;
277           use warnings ;
278           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
279           use IO::File ;
280
281           my $input = new IO::File "<file1.txt.Compressed"
282               or die "Cannot open 'file1.txt.Compressed': $!\n" ;
283           my $buffer ;
284           anyuncompress $input => \$buffer
285               or die "anyuncompress failed: $AnyUncompressError\n";
286
287       To uncompress all files in the directory "/my/home" that match
288       "*.txt.Compressed" and store the compressed data in the same directory
289
290           use strict ;
291           use warnings ;
292           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
293
294           anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
295               or die "anyuncompress failed: $AnyUncompressError\n";
296
297       and if you want to compress each file one at a time, this will do the
298       trick
299
300           use strict ;
301           use warnings ;
302           use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
303
304           for my $input ( glob "/my/home/*.txt.Compressed" )
305           {
306               my $output = $input;
307               $output =~ s/.Compressed// ;
308               anyuncompress $input => $output
309                   or die "Error compressing '$input': $AnyUncompressError\n";
310           }
311

OO Interface

313   Constructor
314       The format of the constructor for IO::Uncompress::AnyUncompress is
315       shown below
316
317           my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
318               or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
319
320       Returns an "IO::Uncompress::AnyUncompress" object on success and undef
321       on failure.  The variable $AnyUncompressError will contain an error
322       message on failure.
323
324       If you are running Perl 5.005 or better the object, $z, returned from
325       IO::Uncompress::AnyUncompress can be used exactly like an IO::File
326       filehandle.  This means that all normal input file operations can be
327       carried out with $z.  For example, to read a line from a compressed
328       file/buffer you can use either of these forms
329
330           $line = $z->getline();
331           $line = <$z>;
332
333       The mandatory parameter $input is used to determine the source of the
334       compressed data. This parameter can take one of three forms.
335
336       A filename
337            If the $input parameter is a scalar, it is assumed to be a
338            filename. This file will be opened for reading and the compressed
339            data will be read from it.
340
341       A filehandle
342            If the $input parameter is a filehandle, the compressed data will
343            be read from it.  The string '-' can be used as an alias for
344            standard input.
345
346       A scalar reference
347            If $input is a scalar reference, the compressed data will be read
348            from $$output.
349
350   Constructor Options
351       The option names defined below are case insensitive and can be
352       optionally prefixed by a '-'.  So all of the following are valid
353
354           -AutoClose
355           -autoclose
356           AUTOCLOSE
357           autoclose
358
359       OPTS is a combination of the following options:
360
361       "AutoClose => 0|1"
362            This option is only valid when the $input parameter is a
363            filehandle. If specified, and the value is true, it will result in
364            the file being closed once either the "close" method is called or
365            the IO::Uncompress::AnyUncompress object is destroyed.
366
367            This parameter defaults to 0.
368
369       "MultiStream => 0|1"
370            Allows multiple concatenated compressed streams to be treated as a
371            single compressed stream. Decompression will stop once either the
372            end of the file/buffer is reached, an error is encountered
373            (premature eof, corrupt compressed data) or the end of a stream is
374            not immediately followed by the start of another stream.
375
376            This parameter defaults to 0.
377
378       "Prime => $string"
379            This option will uncompress the contents of $string before
380            processing the input file/buffer.
381
382            This option can be useful when the compressed data is embedded in
383            another file/data structure and it is not possible to work out
384            where the compressed data begins without having to read the first
385            few bytes. If this is the case, the uncompression can be primed
386            with these bytes using this option.
387
388       "Transparent => 0|1"
389            If this option is set and the input file/buffer is not compressed
390            data, the module will allow reading of it anyway.
391
392            In addition, if the input file/buffer does contain compressed data
393            and there is non-compressed data immediately following it, setting
394            this option will make this module treat the whole file/bufffer as
395            a single data stream.
396
397            This option defaults to 1.
398
399       "BlockSize => $num"
400            When reading the compressed input data,
401            IO::Uncompress::AnyUncompress will read it in blocks of $num
402            bytes.
403
404            This option defaults to 4096.
405
406       "InputLength => $size"
407            When present this option will limit the number of compressed bytes
408            read from the input file/buffer to $size. This option can be used
409            in the situation where there is useful data directly after the
410            compressed data stream and you know beforehand the exact length of
411            the compressed data stream.
412
413            This option is mostly used when reading from a filehandle, in
414            which case the file pointer will be left pointing to the first
415            byte directly after the compressed data stream.
416
417            This option defaults to off.
418
419       "Append => 0|1"
420            This option controls what the "read" method does with uncompressed
421            data.
422
423            If set to 1, all uncompressed data will be appended to the output
424            parameter of the "read" method.
425
426            If set to 0, the contents of the output parameter of the "read"
427            method will be overwritten by the uncompressed data.
428
429            Defaults to 0.
430
431       "Strict => 0|1"
432            This option controls whether the extra checks defined below are
433            used when carrying out the decompression. When Strict is on, the
434            extra tests are carried out, when Strict is off they are not.
435
436            The default for this option is off.
437
438       "RawInflate => 0|1"
439            When auto-detecting the compressed format, try to test for raw-
440            deflate (RFC 1951) content using the "IO::Uncompress::RawInflate"
441            module.
442
443            The reason this is not default behaviour is because RFC 1951
444            content can only be detected by attempting to uncompress it. This
445            process is error prone and can result is false positives.
446
447            Defaults to 0.
448
449       "UnLzma => 0|1"
450            When auto-detecting the compressed format, try to test for
451            lzma_alone content using the "IO::Uncompress::UnLzma" module.
452
453            The reason this is not default behaviour is because lzma_alone
454            content can only be detected by attempting to uncompress it. This
455            process is error prone and can result is false positives.
456
457            Defaults to 0.
458
459   Examples
460       TODO
461

Methods

463   read
464       Usage is
465
466           $status = $z->read($buffer)
467
468       Reads a block of compressed data (the size the the compressed block is
469       determined by the "Buffer" option in the constructor), uncompresses it
470       and writes any uncompressed data into $buffer. If the "Append"
471       parameter is set in the constructor, the uncompressed data will be
472       appended to the $buffer parameter. Otherwise $buffer will be
473       overwritten.
474
475       Returns the number of uncompressed bytes written to $buffer, zero if
476       eof or a negative number on error.
477
478   read
479       Usage is
480
481           $status = $z->read($buffer, $length)
482           $status = $z->read($buffer, $length, $offset)
483
484           $status = read($z, $buffer, $length)
485           $status = read($z, $buffer, $length, $offset)
486
487       Attempt to read $length bytes of uncompressed data into $buffer.
488
489       The main difference between this form of the "read" method and the
490       previous one, is that this one will attempt to return exactly $length
491       bytes. The only circumstances that this function will not is if end-of-
492       file or an IO error is encountered.
493
494       Returns the number of uncompressed bytes written to $buffer, zero if
495       eof or a negative number on error.
496
497   getline
498       Usage is
499
500           $line = $z->getline()
501           $line = <$z>
502
503       Reads a single line.
504
505       This method fully supports the use of of the variable $/ (or
506       $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
507       what constitutes an end of line. Paragraph mode, record mode and file
508       slurp mode are all supported.
509
510   getc
511       Usage is
512
513           $char = $z->getc()
514
515       Read a single character.
516
517   ungetc
518       Usage is
519
520           $char = $z->ungetc($string)
521
522   getHeaderInfo
523       Usage is
524
525           $hdr  = $z->getHeaderInfo();
526           @hdrs = $z->getHeaderInfo();
527
528       This method returns either a hash reference (in scalar context) or a
529       list or hash references (in array context) that contains information
530       about each of the header fields in the compressed data stream(s).
531
532   tell
533       Usage is
534
535           $z->tell()
536           tell $z
537
538       Returns the uncompressed file offset.
539
540   eof
541       Usage is
542
543           $z->eof();
544           eof($z);
545
546       Returns true if the end of the compressed input stream has been
547       reached.
548
549   seek
550           $z->seek($position, $whence);
551           seek($z, $position, $whence);
552
553       Provides a sub-set of the "seek" functionality, with the restriction
554       that it is only legal to seek forward in the input file/buffer.  It is
555       a fatal error to attempt to seek backward.
556
557       The $whence parameter takes one the usual values, namely SEEK_SET,
558       SEEK_CUR or SEEK_END.
559
560       Returns 1 on success, 0 on failure.
561
562   binmode
563       Usage is
564
565           $z->binmode
566           binmode $z ;
567
568       This is a noop provided for completeness.
569
570   opened
571           $z->opened()
572
573       Returns true if the object currently refers to a opened file/buffer.
574
575   autoflush
576           my $prev = $z->autoflush()
577           my $prev = $z->autoflush(EXPR)
578
579       If the $z object is associated with a file or a filehandle, this method
580       returns the current autoflush setting for the underlying filehandle. If
581       "EXPR" is present, and is non-zero, it will enable flushing after every
582       write/print operation.
583
584       If $z is associated with a buffer, this method has no effect and always
585       returns "undef".
586
587       Note that the special variable $| cannot be used to set or retrieve the
588       autoflush setting.
589
590   input_line_number
591           $z->input_line_number()
592           $z->input_line_number(EXPR)
593
594       Returns the current uncompressed line number. If "EXPR" is present it
595       has the effect of setting the line number. Note that setting the line
596       number does not change the current position within the file/buffer
597       being read.
598
599       The contents of $/ are used to to determine what constitutes a line
600       terminator.
601
602   fileno
603           $z->fileno()
604           fileno($z)
605
606       If the $z object is associated with a file or a filehandle, "fileno"
607       will return the underlying file descriptor. Once the "close" method is
608       called "fileno" will return "undef".
609
610       If the $z object is is associated with a buffer, this method will
611       return "undef".
612
613   close
614           $z->close() ;
615           close $z ;
616
617       Closes the output file/buffer.
618
619       For most versions of Perl this method will be automatically invoked if
620       the IO::Uncompress::AnyUncompress object is destroyed (either
621       explicitly or by the variable with the reference to the object going
622       out of scope). The exceptions are Perl versions 5.005 through 5.00504
623       and 5.8.0. In these cases, the "close" method will be called
624       automatically, but not until global destruction of all live objects
625       when the program is terminating.
626
627       Therefore, if you want your scripts to be able to run on all versions
628       of Perl, you should call "close" explicitly and not rely on automatic
629       closing.
630
631       Returns true on success, otherwise 0.
632
633       If the "AutoClose" option has been enabled when the
634       IO::Uncompress::AnyUncompress object was created, and the object is
635       associated with a file, the underlying file will also be closed.
636
637   nextStream
638       Usage is
639
640           my $status = $z->nextStream();
641
642       Skips to the next compressed data stream in the input file/buffer. If a
643       new compressed data stream is found, the eof marker will be cleared and
644       $.  will be reset to 0.
645
646       Returns 1 if a new stream was found, 0 if none was found, and -1 if an
647       error was encountered.
648
649   trailingData
650       Usage is
651
652           my $data = $z->trailingData();
653
654       Returns the data, if any, that is present immediately after the
655       compressed data stream once uncompression is complete. It only makes
656       sense to call this method once the end of the compressed data stream
657       has been encountered.
658
659       This option can be used when there is useful information immediately
660       following the compressed data stream, and you don't know the length of
661       the compressed data stream.
662
663       If the input is a buffer, "trailingData" will return everything from
664       the end of the compressed data stream to the end of the buffer.
665
666       If the input is a filehandle, "trailingData" will return the data that
667       is left in the filehandle input buffer once the end of the compressed
668       data stream has been reached. You can then use the filehandle to read
669       the rest of the input file.
670
671       Don't bother using "trailingData" if the input is a filename.
672
673       If you know the length of the compressed data stream before you start
674       uncompressing, you can avoid having to use "trailingData" by setting
675       the "InputLength" option in the constructor.
676

Importing

678       No symbolic constants are required by this
679       IO::Uncompress::AnyUncompress at present.
680
681       :all Imports "anyuncompress" and $AnyUncompressError.  Same as doing
682            this
683
684                use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
685

EXAMPLES

SEE ALSO

688       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
689       IO::Compress::Deflate, IO::Uncompress::Inflate,
690       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
691       IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
692       IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
693       IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf,
694       IO::Uncompress::UnLzf, IO::Uncompress::AnyInflate
695
696       Compress::Zlib::FAQ
697
698       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
699

AUTHOR

701       This module was written by Paul Marquess, pmqs@cpan.org.
702

MODIFICATION HISTORY

704       See the Changes file.
705
707       Copyright (c) 2005-2010 Paul Marquess. All rights reserved.
708
709       This program is free software; you can redistribute it and/or modify it
710       under the same terms as Perl itself.
711
712
713
714perl v5.12.4                      2011-06-07IO::Uncompress::AnyUncompress(3pm)
Impressum