1IO::Uncompress::Unzip(3U)ser Contributed Perl DocumentatiIoOn::Uncompress::Unzip(3)
2
3
4
6 IO::Uncompress::Unzip - Read zip files/buffers
7
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
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
57 IO::Compress::Zip.
58
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_filename_or_reference => $output_filename_or_reference [,OPTS]
67 or die "unzip failed: $UnzipError\n";
68
69 The functional interface needs Perl5.005 or better.
70
71 unzip $input_filename_or_reference => $output_filename_or_reference [,
72 OPTS]
73 "unzip" expects at least two parameters, $input_filename_or_reference
74 and $output_filename_or_reference.
75
76 The $input_filename_or_reference parameter
77
78 The parameter, $input_filename_or_reference, is used to define the
79 source of the compressed data.
80
81 It can take one of the following forms:
82
83 A filename
84 If the <$input_filename_or_reference> parameter is a simple
85 scalar, it is assumed to be a filename. This file will be opened
86 for reading and the input data will be read from it.
87
88 A filehandle
89 If the $input_filename_or_reference parameter is a filehandle, the
90 input data will be read from it. The string '-' can be used as an
91 alias for standard input.
92
93 A scalar reference
94 If $input_filename_or_reference is a scalar reference, the input
95 data will be read from $$input_filename_or_reference.
96
97 An array reference
98 If $input_filename_or_reference is an array reference, each
99 element in the array must be a filename.
100
101 The input data will be read from each file in turn.
102
103 The complete array will be walked to ensure that it only contains
104 valid filenames before any data is uncompressed.
105
106 An Input FileGlob string
107 If $input_filename_or_reference is a string that is delimited by
108 the characters "<" and ">" "unzip" will assume that it is an input
109 fileglob string. The input is the list of files that match the
110 fileglob.
111
112 See File::GlobMapper for more details.
113
114 If the $input_filename_or_reference parameter is any other type,
115 "undef" will be returned.
116
117 The $output_filename_or_reference parameter
118
119 The parameter $output_filename_or_reference is used to control the
120 destination of the uncompressed data. This parameter can take one of
121 these forms.
122
123 A filename
124 If the $output_filename_or_reference parameter is a simple scalar,
125 it is assumed to be a filename. This file will be opened for
126 writing and the uncompressed data will be written to it.
127
128 A filehandle
129 If the $output_filename_or_reference parameter is a filehandle,
130 the uncompressed data will be written to it. The string '-' can
131 be used as an alias for standard output.
132
133 A scalar reference
134 If $output_filename_or_reference is a scalar reference, the
135 uncompressed data will be stored in
136 $$output_filename_or_reference.
137
138 An Array Reference
139 If $output_filename_or_reference is an array reference, the
140 uncompressed data will be pushed onto the array.
141
142 An Output FileGlob
143 If $output_filename_or_reference is a string that is delimited by
144 the characters "<" and ">" "unzip" will assume that it is an
145 output fileglob string. The output is the list of files that match
146 the fileglob.
147
148 When $output_filename_or_reference is an fileglob string,
149 $input_filename_or_reference must also be a fileglob string.
150 Anything else is an error.
151
152 See File::GlobMapper for more details.
153
154 If the $output_filename_or_reference parameter is any other type,
155 "undef" will be returned.
156
157 Notes
158 When $input_filename_or_reference maps to multiple compressed
159 files/buffers and $output_filename_or_reference is a single
160 file/buffer, after uncompression $output_filename_or_reference will
161 contain a concatenation of all the uncompressed data from each of the
162 input files/buffers.
163
164 Optional Parameters
165 Unless specified below, the optional parameters for "unzip", "OPTS",
166 are the same as those used with the OO interface defined in the
167 "Constructor Options" section below.
168
169 "AutoClose => 0|1"
170 This option applies to any input or output data streams to "unzip"
171 that are filehandles.
172
173 If "AutoClose" is specified, and the value is true, it will result
174 in all input and/or output filehandles being closed once "unzip"
175 has completed.
176
177 This parameter defaults to 0.
178
179 "BinModeOut => 0|1"
180 When writing to a file or filehandle, set "binmode" before writing
181 to the file.
182
183 Defaults to 0.
184
185 "Append => 0|1"
186 The behaviour of this option is dependent on the type of output
187 data stream.
188
189 · A Buffer
190
191 If "Append" is enabled, all uncompressed data will be append
192 to the end of the output buffer. Otherwise the output buffer
193 will be cleared before any uncompressed data is written to
194 it.
195
196 · A Filename
197
198 If "Append" is enabled, the file will be opened in append
199 mode. Otherwise the contents of the file, if any, will be
200 truncated before any uncompressed data is written to it.
201
202 · A Filehandle
203
204 If "Append" is enabled, the filehandle will be positioned to
205 the end of the file via a call to "seek" before any
206 uncompressed data is written to it. Otherwise the file
207 pointer will not be moved.
208
209 When "Append" is specified, and set to true, it will append all
210 uncompressed data to the output data stream.
211
212 So when the output is a filehandle it will carry out a seek to the
213 eof before writing any uncompressed data. If the output is a
214 filename, it will be opened for appending. If the output is a
215 buffer, all uncompressed data will be appended to the existing
216 buffer.
217
218 Conversely when "Append" is not specified, or it is present and is
219 set to false, it will operate as follows.
220
221 When the output is a filename, it will truncate the contents of
222 the file before writing any uncompressed data. If the output is a
223 filehandle its position will not be changed. If the output is a
224 buffer, it will be wiped before any uncompressed data is output.
225
226 Defaults to 0.
227
228 "MultiStream => 0|1"
229 If the input file/buffer contains multiple compressed data
230 streams, this option will uncompress the whole lot as a single
231 data stream.
232
233 Defaults to 0.
234
235 "TrailingData => $scalar"
236 Returns the data, if any, that is present immediately after the
237 compressed data stream once uncompression is complete.
238
239 This option can be used when there is useful information
240 immediately following the compressed data stream, and you don't
241 know the length of the compressed data stream.
242
243 If the input is a buffer, "trailingData" will return everything
244 from the end of the compressed data stream to the end of the
245 buffer.
246
247 If the input is a filehandle, "trailingData" will return the data
248 that is left in the filehandle input buffer once the end of the
249 compressed data stream has been reached. You can then use the
250 filehandle to read the rest of the input file.
251
252 Don't bother using "trailingData" if the input is a filename.
253
254 If you know the length of the compressed data stream before you
255 start uncompressing, you can avoid having to use "trailingData" by
256 setting the "InputLength" option.
257
258 Examples
259 Say you have a zip file, "file1.zip", that only contains a single
260 member, you can read it and write the uncompressed data to the file
261 "file1.txt" like this.
262
263 use strict ;
264 use warnings ;
265 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
266
267 my $input = "file1.zip";
268 my $output = "file1.txt";
269 unzip $input => $output
270 or die "unzip failed: $UnzipError\n";
271
272 If you have a zip file that contains multiple members and want to read
273 a specific member from the file, say "data1", use the "Name" option
274
275 use strict ;
276 use warnings ;
277 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
278
279 my $input = "file1.zip";
280 my $output = "file1.txt";
281 unzip $input => $output, Name => "data1"
282 or die "unzip failed: $UnzipError\n";
283
284 Alternatively, if you want to read the "data1" member into memory, use
285 a scalar reference for the "output" partameter.
286
287 use strict ;
288 use warnings ;
289 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
290
291 my $input = "file1.zip";
292 my $output ;
293 unzip $input => \$output, Name => "data1"
294 or die "unzip failed: $UnzipError\n";
295 # $output now contains the uncompressed data
296
297 To read from an existing Perl filehandle, $input, and write the
298 uncompressed data to a buffer, $buffer.
299
300 use strict ;
301 use warnings ;
302 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
303 use IO::File ;
304
305 my $input = new IO::File "<file1.zip"
306 or die "Cannot open 'file1.zip': $!\n" ;
307 my $buffer ;
308 unzip $input => \$buffer
309 or die "unzip failed: $UnzipError\n";
310
312 Constructor
313 The format of the constructor for IO::Uncompress::Unzip is shown below
314
315 my $z = new IO::Uncompress::Unzip $input [OPTS]
316 or die "IO::Uncompress::Unzip failed: $UnzipError\n";
317
318 Returns an "IO::Uncompress::Unzip" object on success and undef on
319 failure. The variable $UnzipError will contain an error message on
320 failure.
321
322 If you are running Perl 5.005 or better the object, $z, returned from
323 IO::Uncompress::Unzip can be used exactly like an IO::File filehandle.
324 This means that all normal input file operations can be carried out
325 with $z. For example, to read a line from a compressed file/buffer you
326 can use either of these forms
327
328 $line = $z->getline();
329 $line = <$z>;
330
331 The mandatory parameter $input is used to determine the source of the
332 compressed data. This parameter can take one of three forms.
333
334 A filename
335 If the $input parameter is a scalar, it is assumed to be a
336 filename. This file will be opened for reading and the compressed
337 data will be read from it.
338
339 A filehandle
340 If the $input parameter is a filehandle, the compressed data will
341 be read from it. The string '-' can be used as an alias for
342 standard input.
343
344 A scalar reference
345 If $input is a scalar reference, the compressed data will be read
346 from $$input.
347
348 Constructor Options
349 The option names defined below are case insensitive and can be
350 optionally prefixed by a '-'. So all of the following are valid
351
352 -AutoClose
353 -autoclose
354 AUTOCLOSE
355 autoclose
356
357 OPTS is a combination of the following options:
358
359 "Name => "membername""
360 Open "membername" from the zip file for reading.
361
362 "AutoClose => 0|1"
363 This option is only valid when the $input parameter is a
364 filehandle. If specified, and the value is true, it will result in
365 the file being closed once either the "close" method is called or
366 the IO::Uncompress::Unzip object is destroyed.
367
368 This parameter defaults to 0.
369
370 "MultiStream => 0|1"
371 Treats the complete zip file/buffer as a single compressed data
372 stream. When reading in multi-stream mode each member of the zip
373 file/buffer will be uncompressed in turn until the end of the
374 file/buffer is encountered.
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/buffer as a
395 single data stream.
396
397 This option defaults to 1.
398
399 "BlockSize => $num"
400 When reading the compressed input data, IO::Uncompress::Unzip will
401 read it in blocks of $num bytes.
402
403 This option defaults to 4096.
404
405 "InputLength => $size"
406 When present this option will limit the number of compressed bytes
407 read from the input file/buffer to $size. This option can be used
408 in the situation where there is useful data directly after the
409 compressed data stream and you know beforehand the exact length of
410 the compressed data stream.
411
412 This option is mostly used when reading from a filehandle, in
413 which case the file pointer will be left pointing to the first
414 byte directly after the compressed data stream.
415
416 This option defaults to off.
417
418 "Append => 0|1"
419 This option controls what the "read" method does with uncompressed
420 data.
421
422 If set to 1, all uncompressed data will be appended to the output
423 parameter of the "read" method.
424
425 If set to 0, the contents of the output parameter of the "read"
426 method will be overwritten by the uncompressed data.
427
428 Defaults to 0.
429
430 "Strict => 0|1"
431 This option controls whether the extra checks defined below are
432 used when carrying out the decompression. When Strict is on, the
433 extra tests are carried out, when Strict is off they are not.
434
435 The default for this option is off.
436
437 Examples
438 TODO
439
441 read
442 Usage is
443
444 $status = $z->read($buffer)
445
446 Reads a block of compressed data (the size the the compressed block is
447 determined by the "Buffer" option in the constructor), uncompresses it
448 and writes any uncompressed data into $buffer. If the "Append"
449 parameter is set in the constructor, the uncompressed data will be
450 appended to the $buffer parameter. Otherwise $buffer will be
451 overwritten.
452
453 Returns the number of uncompressed bytes written to $buffer, zero if
454 eof or a negative number on error.
455
456 read
457 Usage is
458
459 $status = $z->read($buffer, $length)
460 $status = $z->read($buffer, $length, $offset)
461
462 $status = read($z, $buffer, $length)
463 $status = read($z, $buffer, $length, $offset)
464
465 Attempt to read $length bytes of uncompressed data into $buffer.
466
467 The main difference between this form of the "read" method and the
468 previous one, is that this one will attempt to return exactly $length
469 bytes. The only circumstances that this function will not is if end-of-
470 file or an IO error is encountered.
471
472 Returns the number of uncompressed bytes written to $buffer, zero if
473 eof or a negative number on error.
474
475 getline
476 Usage is
477
478 $line = $z->getline()
479 $line = <$z>
480
481 Reads a single line.
482
483 This method fully supports the use of of the variable $/ (or
484 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
485 what constitutes an end of line. Paragraph mode, record mode and file
486 slurp mode are all supported.
487
488 getc
489 Usage is
490
491 $char = $z->getc()
492
493 Read a single character.
494
495 ungetc
496 Usage is
497
498 $char = $z->ungetc($string)
499
500 inflateSync
501 Usage is
502
503 $status = $z->inflateSync()
504
505 TODO
506
507 getHeaderInfo
508 Usage is
509
510 $hdr = $z->getHeaderInfo();
511 @hdrs = $z->getHeaderInfo();
512
513 This method returns either a hash reference (in scalar context) or a
514 list or hash references (in array context) that contains information
515 about each of the header fields in the compressed data stream(s).
516
517 tell
518 Usage is
519
520 $z->tell()
521 tell $z
522
523 Returns the uncompressed file offset.
524
525 eof
526 Usage is
527
528 $z->eof();
529 eof($z);
530
531 Returns true if the end of the compressed input stream has been
532 reached.
533
534 seek
535 $z->seek($position, $whence);
536 seek($z, $position, $whence);
537
538 Provides a sub-set of the "seek" functionality, with the restriction
539 that it is only legal to seek forward in the input file/buffer. It is
540 a fatal error to attempt to seek backward.
541
542 Note that the implementation of "seek" in this module does not provide
543 true random access to a compressed file/buffer. It works by
544 uncompressing data from the current offset in the file/buffer until it
545 reaches the ucompressed offset specified in the parameters to "seek".
546 For very small files this may be acceptable behaviour. For large files
547 it may cause an unacceptable delay.
548
549 The $whence parameter takes one the usual values, namely SEEK_SET,
550 SEEK_CUR or SEEK_END.
551
552 Returns 1 on success, 0 on failure.
553
554 binmode
555 Usage is
556
557 $z->binmode
558 binmode $z ;
559
560 This is a noop provided for completeness.
561
562 opened
563 $z->opened()
564
565 Returns true if the object currently refers to a opened file/buffer.
566
567 autoflush
568 my $prev = $z->autoflush()
569 my $prev = $z->autoflush(EXPR)
570
571 If the $z object is associated with a file or a filehandle, this method
572 returns the current autoflush setting for the underlying filehandle. If
573 "EXPR" is present, and is non-zero, it will enable flushing after every
574 write/print operation.
575
576 If $z is associated with a buffer, this method has no effect and always
577 returns "undef".
578
579 Note that the special variable $| cannot be used to set or retrieve the
580 autoflush setting.
581
582 input_line_number
583 $z->input_line_number()
584 $z->input_line_number(EXPR)
585
586 Returns the current uncompressed line number. If "EXPR" is present it
587 has the effect of setting the line number. Note that setting the line
588 number does not change the current position within the file/buffer
589 being read.
590
591 The contents of $/ are used to to determine what constitutes a line
592 terminator.
593
594 fileno
595 $z->fileno()
596 fileno($z)
597
598 If the $z object is associated with a file or a filehandle, "fileno"
599 will return the underlying file descriptor. Once the "close" method is
600 called "fileno" will return "undef".
601
602 If the $z object is associated with a buffer, this method will return
603 "undef".
604
605 close
606 $z->close() ;
607 close $z ;
608
609 Closes the output file/buffer.
610
611 For most versions of Perl this method will be automatically invoked if
612 the IO::Uncompress::Unzip object is destroyed (either explicitly or by
613 the variable with the reference to the object going out of scope). The
614 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
615 cases, the "close" method will be called automatically, but not until
616 global destruction of all live objects when the program is terminating.
617
618 Therefore, if you want your scripts to be able to run on all versions
619 of Perl, you should call "close" explicitly and not rely on automatic
620 closing.
621
622 Returns true on success, otherwise 0.
623
624 If the "AutoClose" option has been enabled when the
625 IO::Uncompress::Unzip object was created, and the object is associated
626 with a file, the underlying file will also be closed.
627
628 nextStream
629 Usage is
630
631 my $status = $z->nextStream();
632
633 Skips to the next compressed data stream in the input file/buffer. If a
634 new compressed data stream is found, the eof marker will be cleared and
635 $. will be reset to 0.
636
637 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
638 error was encountered.
639
640 trailingData
641 Usage is
642
643 my $data = $z->trailingData();
644
645 Returns the data, if any, that is present immediately after the
646 compressed data stream once uncompression is complete. It only makes
647 sense to call this method once the end of the compressed data stream
648 has been encountered.
649
650 This option can be used when there is useful information immediately
651 following the compressed data stream, and you don't know the length of
652 the compressed data stream.
653
654 If the input is a buffer, "trailingData" will return everything from
655 the end of the compressed data stream to the end of the buffer.
656
657 If the input is a filehandle, "trailingData" will return the data that
658 is left in the filehandle input buffer once the end of the compressed
659 data stream has been reached. You can then use the filehandle to read
660 the rest of the input file.
661
662 Don't bother using "trailingData" if the input is a filename.
663
664 If you know the length of the compressed data stream before you start
665 uncompressing, you can avoid having to use "trailingData" by setting
666 the "InputLength" option in the constructor.
667
669 No symbolic constants are required by this IO::Uncompress::Unzip at
670 present.
671
672 :all Imports "unzip" and $UnzipError. Same as doing this
673
674 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
675
677 Working with Net::FTP
678 See IO::Compress::FAQ
679
680 Walking through a zip file
681 The code below can be used to traverse a zip file, one compressed data
682 stream at a time.
683
684 use IO::Uncompress::Unzip qw($UnzipError);
685
686 my $zipfile = "somefile.zip";
687 my $u = new IO::Uncompress::Unzip $zipfile
688 or die "Cannot open $zipfile: $UnzipError";
689
690 my $status;
691 for ($status = 1; $status > 0; $status = $u->nextStream())
692 {
693
694 my $name = $u->getHeaderInfo()->{Name};
695 warn "Processing member $name\n" ;
696
697 my $buff;
698 while (($status = $u->read($buff)) > 0) {
699 # Do something here
700 }
701
702 last if $status < 0;
703 }
704
705 die "Error processing $zipfile: $!\n"
706 if $status < 0 ;
707
708 Each individual compressed data stream is read until the logical end-
709 of-file is reached. Then "nextStream" is called. This will skip to the
710 start of the next compressed data stream and clear the end-of-file
711 flag.
712
713 It is also worth noting that "nextStream" can be called at any time --
714 you don't have to wait until you have exhausted a compressed data
715 stream before skipping to the next one.
716
717 Unzipping a complete zip file to disk
718 Daniel S. Sterling has written a script that uses
719 "IO::Uncompress::UnZip" to read a zip file and unzip its contents to
720 disk.
721
722 The script is available from <https://gist.github.com/eqhmcow/5389877>
723
725 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
726 IO::Compress::Deflate, IO::Uncompress::Inflate,
727 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
728 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
729 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
730 IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf,
731 IO::Uncompress::UnLzf, IO::Uncompress::AnyInflate,
732 IO::Uncompress::AnyUncompress
733
734 IO::Compress::FAQ
735
736 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
737
738 For RFC 1950, 1951 and 1952 see http://www.faqs.org/rfcs/rfc1950.html,
739 http://www.faqs.org/rfcs/rfc1951.html and
740 http://www.faqs.org/rfcs/rfc1952.html
741
742 The zlib compression library was written by Jean-loup Gailly
743 gzip@prep.ai.mit.edu and Mark Adler madler@alumni.caltech.edu.
744
745 The primary site for the zlib compression library is
746 http://www.zlib.org.
747
748 The primary site for gzip is http://www.gzip.org.
749
751 This module was written by Paul Marquess, pmqs@cpan.org.
752
754 See the Changes file.
755
757 Copyright (c) 2005-2013 Paul Marquess. All rights reserved.
758
759 This program is free software; you can redistribute it and/or modify it
760 under the same terms as Perl itself.
761
762
763
764perl v5.16.3 2013-05-19 IO::Uncompress::Unzip(3)