1IO::Uncompress::AnyInflaPteer(l3pPmr)ogrammers ReferencIeO:G:uUindceompress::AnyInflate(3pm)
2
3
4

NAME

6       IO::Uncompress::AnyInflate - Uncompress zlib-based (zip, gzip)
7       file/buffer
8

SYNOPSIS

10           use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
11
12           my $status = anyinflate $input => $output [,OPTS]
13               or die "anyinflate failed: $AnyInflateError\n";
14
15           my $z = new IO::Uncompress::AnyInflate $input [OPTS]
16               or die "anyinflate failed: $AnyInflateError\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           $status = $z->inflateSync()
27
28           $data = $z->trailingData()
29           $status = $z->nextStream()
30           $data = $z->getHeaderInfo()
31           $z->tell()
32           $z->seek($position, $whence)
33           $z->binmode()
34           $z->fileno()
35           $z->eof()
36           $z->close()
37
38           $AnyInflateError ;
39
40           # IO::File mode
41
42           <$z>
43           read($z, $buffer);
44           read($z, $buffer, $length);
45           read($z, $buffer, $length, $offset);
46           tell($z)
47           seek($z, $position, $whence)
48           binmode($z)
49           fileno($z)
50           eof($z)
51           close($z)
52

DESCRIPTION

54       This module provides a Perl interface that allows the reading of
55       files/buffers that have been compressed in a number of formats that use
56       the zlib compression library.
57
58       The formats supported are
59
60       RFC 1950
61       RFC 1951 (optionally)
62       gzip (RFC 1952)
63       zip
64
65       The module will auto-detect which, if any, of the supported compression
66       formats is being used.
67

Functional Interface

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

OO Interface

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

Methods

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

Importing

711       No symbolic constants are required by this IO::Uncompress::AnyInflate
712       at present.
713
714       :all Imports "anyinflate" and $AnyInflateError.  Same as doing this
715
716                use IO::Uncompress::AnyInflate qw(anyinflate $AnyInflateError) ;
717

EXAMPLES

719   Working with Net::FTP
720       See IO::Uncompress::AnyInflate::FAQ
721

SEE ALSO

723       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
724       IO::Compress::Deflate, IO::Uncompress::Inflate,
725       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
726       IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
727       IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
728       IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf,
729       IO::Uncompress::UnLzf, IO::Uncompress::AnyUncompress
730
731       Compress::Zlib::FAQ
732
733       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
734
735       For RFC 1950, 1951 and 1952 see http://www.faqs.org/rfcs/rfc1950.html,
736       http://www.faqs.org/rfcs/rfc1951.html and
737       http://www.faqs.org/rfcs/rfc1952.html
738
739       The zlib compression library was written by Jean-loup Gailly
740       gzip@prep.ai.mit.edu and Mark Adler madler@alumni.caltech.edu.
741
742       The primary site for the zlib compression library is
743       http://www.zlib.org.
744
745       The primary site for gzip is http://www.gzip.org.
746

AUTHOR

748       This module was written by Paul Marquess, pmqs@cpan.org.
749

MODIFICATION HISTORY

751       See the Changes file.
752
754       Copyright (c) 2005-2010 Paul Marquess. All rights reserved.
755
756       This program is free software; you can redistribute it and/or modify it
757       under the same terms as Perl itself.
758
759
760
761perl v5.12.4                      2011-06-07   IO::Uncompress::AnyInflate(3pm)
Impressum