1IO::Uncompress::Unzip(3U)ser Contributed Perl DocumentatiIoOn::Uncompress::Unzip(3)
2
3
4

NAME

6       IO::Uncompress::Unzip - Read zip files/buffers
7

SYNOPSIS

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

DESCRIPTION

53       This module provides a Perl interface that allows the reading of zlib
54       files/buffers.
55
56       For writing zip files/buffers, see the companion module IO::Com‐
57       press::Zip.
58

Functional Interface

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

OO Interface

265       Constructor
266
267       The format of the constructor for IO::Uncompress::Unzip is shown below
268
269           my $z = new IO::Uncompress::Unzip $input [OPTS]
270               or die "IO::Uncompress::Unzip failed: $UnzipError\n";
271
272       Returns an "IO::Uncompress::Unzip" object on success and undef on fail‐
273       ure.  The variable $UnzipError will contain an error message on fail‐
274       ure.
275
276       If you are running Perl 5.005 or better the object, $z, returned from
277       IO::Uncompress::Unzip can be used exactly like an IO::File filehandle.
278       This means that all normal input file operations can be carried out
279       with $z.  For example, to read a line from a compressed file/buffer you
280       can use either of these forms
281
282           $line = $z->getline();
283           $line = <$z>;
284
285       The mandatory parameter $input is used to determine the source of the
286       compressed data. This parameter can take one of three forms.
287
288       A filename
289            If the $input parameter is a scalar, it is assumed to be a file‐
290            name. This file will be opened for reading and the compressed data
291            will be read from it.
292
293       A filehandle
294            If the $input parameter is a filehandle, the compressed data will
295            be read from it.  The string '-' can be used as an alias for stan‐
296            dard input.
297
298       A scalar reference
299            If $input is a scalar reference, the compressed data will be read
300            from $$output.
301
302       Constructor Options
303
304       The option names defined below are case insensitive and can be option‐
305       ally prefixed by a '-'.  So all of the following are valid
306
307           -AutoClose
308           -autoclose
309           AUTOCLOSE
310           autoclose
311
312       OPTS is a combination of the following options:
313
314       "AutoClose => 0⎪1"
315            This option is only valid when the $input parameter is a filehan‐
316            dle. If specified, and the value is true, it will result in the
317            file being closed once either the "close" method is called or the
318            IO::Uncompress::Unzip object is destroyed.
319
320            This parameter defaults to 0.
321
322       "MultiStream => 0⎪1"
323            Treats the complete zip file/buffer as a single compressed data
324            stream. When reading in multi-stream mode each member of the zip
325            file/buffer will be uncompressed in turn until the end of the
326            file/buffer is encountered.
327
328            This parameter defaults to 0.
329
330       "Prime => $string"
331            This option will uncompress the contents of $string before pro‐
332            cessing the input file/buffer.
333
334            This option can be useful when the compressed data is embedded in
335            another file/data structure and it is not possible to work out
336            where the compressed data begins without having to read the first
337            few bytes. If this is the case, the uncompression can be primed
338            with these bytes using this option.
339
340       "Transparent => 0⎪1"
341            If this option is set and the input file/buffer is not compressed
342            data, the module will allow reading of it anyway.
343
344            In addition, if the input file/buffer does contain compressed data
345            and there is non-compressed data immediately following it, setting
346            this option will make this module treat the whole file/bufffer as
347            a single data stream.
348
349            This option defaults to 1.
350
351       "BlockSize => $num"
352            When reading the compressed input data, IO::Uncompress::Unzip will
353            read it in blocks of $num bytes.
354
355            This option defaults to 4096.
356
357       "InputLength => $size"
358            When present this option will limit the number of compressed bytes
359            read from the input file/buffer to $size. This option can be used
360            in the situation where there is useful data directly after the
361            compressed data stream and you know beforehand the exact length of
362            the compressed data stream.
363
364            This option is mostly used when reading from a filehandle, in
365            which case the file pointer will be left pointing to the first
366            byte directly after the compressed data stream.
367
368            This option defaults to off.
369
370       "Append => 0⎪1"
371            This option controls what the "read" method does with uncompressed
372            data.
373
374            If set to 1, all uncompressed data will be appended to the output
375            parameter of the "read" method.
376
377            If set to 0, the contents of the output parameter of the "read"
378            method will be overwritten by the uncompressed data.
379
380            Defaults to 0.
381
382       "Strict => 0⎪1"
383            This option controls whether the extra checks defined below are
384            used when carrying out the decompression. When Strict is on, the
385            extra tests are carried out, when Strict is off they are not.
386
387            The default for this option is off.
388
389       Examples
390
391       TODO
392

Methods

394       read
395
396       Usage is
397
398           $status = $z->read($buffer)
399
400       Reads a block of compressed data (the size the the compressed block is
401       determined by the "Buffer" option in the constructor), uncompresses it
402       and writes any uncompressed data into $buffer. If the "Append" parame‐
403       ter is set in the constructor, the uncompressed data will be appended
404       to the $buffer parameter. Otherwise $buffer will be overwritten.
405
406       Returns the number of uncompressed bytes written to $buffer, zero if
407       eof or a negative number on error.
408
409       read
410
411       Usage is
412
413           $status = $z->read($buffer, $length)
414           $status = $z->read($buffer, $length, $offset)
415
416           $status = read($z, $buffer, $length)
417           $status = read($z, $buffer, $length, $offset)
418
419       Attempt to read $length bytes of uncompressed data into $buffer.
420
421       The main difference between this form of the "read" method and the pre‐
422       vious one, is that this one will attempt to return exactly $length
423       bytes. The only circumstances that this function will not is if end-of-
424       file or an IO error is encountered.
425
426       Returns the number of uncompressed bytes written to $buffer, zero if
427       eof or a negative number on error.
428
429       getline
430
431       Usage is
432
433           $line = $z->getline()
434           $line = <$z>
435
436       Reads a single line.
437
438       This method fully supports the use of of the variable $/ (or
439       $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
440       what constitutes an end of line. Paragraph mode, record mode and file
441       slurp mode are all supported.
442
443       getc
444
445       Usage is
446
447           $char = $z->getc()
448
449       Read a single character.
450
451       ungetc
452
453       Usage is
454
455           $char = $z->ungetc($string)
456
457       inflateSync
458
459       Usage is
460
461           $status = $z->inflateSync()
462
463       TODO
464
465       getHeaderInfo
466
467       Usage is
468
469           $hdr  = $z->getHeaderInfo();
470           @hdrs = $z->getHeaderInfo();
471
472       This method returns either a hash reference (in scalar context) or a
473       list or hash references (in array context) that contains information
474       about each of the header fields in the compressed data stream(s).
475
476       tell
477
478       Usage is
479
480           $z->tell()
481           tell $z
482
483       Returns the uncompressed file offset.
484
485       eof
486
487       Usage is
488
489           $z->eof();
490           eof($z);
491
492       Returns true if the end of the compressed input stream has been
493       reached.
494
495       seek
496
497           $z->seek($position, $whence);
498           seek($z, $position, $whence);
499
500       Provides a sub-set of the "seek" functionality, with the restriction
501       that it is only legal to seek forward in the input file/buffer.  It is
502       a fatal error to attempt to seek backward.
503
504       The $whence parameter takes one the usual values, namely SEEK_SET,
505       SEEK_CUR or SEEK_END.
506
507       Returns 1 on success, 0 on failure.
508
509       binmode
510
511       Usage is
512
513           $z->binmode
514           binmode $z ;
515
516       This is a noop provided for completeness.
517
518       opened
519
520           $z->opened()
521
522       Returns true if the object currently refers to a opened file/buffer.
523
524       autoflush
525
526           my $prev = $z->autoflush()
527           my $prev = $z->autoflush(EXPR)
528
529       If the $z object is associated with a file or a filehandle, this method
530       returns the current autoflush setting for the underlying filehandle. If
531       "EXPR" is present, and is non-zero, it will enable flushing after every
532       write/print operation.
533
534       If $z is associated with a buffer, this method has no effect and always
535       returns "undef".
536
537       Note that the special variable $⎪ cannot be used to set or retrieve the
538       autoflush setting.
539
540       input_line_number
541
542           $z->input_line_number()
543           $z->input_line_number(EXPR)
544
545       Returns the current uncompressed line number. If "EXPR" is present it
546       has the effect of setting the line number. Note that setting the line
547       number does not change the current position within the file/buffer
548       being read.
549
550       The contents of $/ are used to to determine what constitutes a line
551       terminator.
552
553       fileno
554
555           $z->fileno()
556           fileno($z)
557
558       If the $z object is associated with a file or a filehandle, this method
559       will return the underlying file descriptor.
560
561       If the $z object is is associated with a buffer, this method will
562       return undef.
563
564       close
565
566           $z->close() ;
567           close $z ;
568
569       Closes the output file/buffer.
570
571       For most versions of Perl this method will be automatically invoked if
572       the IO::Uncompress::Unzip object is destroyed (either explicitly or by
573       the variable with the reference to the object going out of scope). The
574       exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
575       cases, the "close" method will be called automatically, but not until
576       global destruction of all live objects when the program is terminating.
577
578       Therefore, if you want your scripts to be able to run on all versions
579       of Perl, you should call "close" explicitly and not rely on automatic
580       closing.
581
582       Returns true on success, otherwise 0.
583
584       If the "AutoClose" option has been enabled when the IO::Uncom‐
585       press::Unzip object was created, and the object is associated with a
586       file, the underlying file will also be closed.
587
588       nextStream
589
590       Usage is
591
592           my $status = $z->nextStream();
593
594       Skips to the next compressed data stream in the input file/buffer. If a
595       new compressed data stream is found, the eof marker will be cleared and
596       $.  will be reset to 0.
597
598       Returns 1 if a new stream was found, 0 if none was found, and -1 if an
599       error was encountered.
600
601       trailingData
602
603       Usage is
604
605           my $data = $z->trailingData();
606
607       Returns the data, if any, that is present immediately after the com‐
608       pressed data stream once uncompression is complete. It only makes sense
609       to call this method once the end of the compressed data stream has been
610       encountered.
611
612       This option can be used when there is useful information immediately
613       following the compressed data stream, and you don't know the length of
614       the compressed data stream.
615
616       If the input is a buffer, "trailingData" will return everything from
617       the end of the compressed data stream to the end of the buffer.
618
619       If the input is a filehandle, "trailingData" will return the data that
620       is left in the filehandle input buffer once the end of the compressed
621       data stream has been reached. You can then use the filehandle to read
622       the rest of the input file.
623
624       Don't bother using "trailingData" if the input is a filename.
625
626       If you know the length of the compressed data stream before you start
627       uncompressing, you can avoid having to use "trailingData" by setting
628       the "InputLength" option in the constructor.
629

Importing

631       No symbolic constants are required by this IO::Uncompress::Unzip at
632       present.
633
634       :all Imports "unzip" and $UnzipError.  Same as doing this
635
636                use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
637

EXAMPLES

SEE ALSO

640       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Com‐
641       press::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate,
642       IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bun‐
643       zip2, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf,
644       IO::Uncompress::UnLzf, IO::Uncompress::AnyInflate, IO::Uncom‐
645       press::AnyUncompress
646
647       Compress::Zlib::FAQ
648
649       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
650
651       For RFC 1950, 1951 and 1952 see http://www.faqs.org/rfcs/rfc1950.html,
652       http://www.faqs.org/rfcs/rfc1951.html and
653       http://www.faqs.org/rfcs/rfc1952.html
654
655       The zlib compression library was written by Jean-loup Gailly
656       gzip@prep.ai.mit.edu and Mark Adler madler@alumni.caltech.edu.
657
658       The primary site for the zlib compression library is
659       http://www.zlib.org.
660
661       The primary site for gzip is http://www.gzip.org.
662

AUTHOR

664       This module was written by Paul Marquess, pmqs@cpan.org.
665

MODIFICATION HISTORY

667       See the Changes file.
668
670       Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
671
672       This program is free software; you can redistribute it and/or modify it
673       under the same terms as Perl itself.
674
675
676
677perl v5.8.8                       2007-06-18          IO::Uncompress::Unzip(3)
Impressum