Debian Codegraph @

upstream/mageia-cauldron/man3pm/IO::Uncompress::Unzip.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "IO::Uncompress::Unzip 3pm"
61 .TH IO::Uncompress::Unzip 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 IO::Uncompress::Unzip \- Read zip files/buffers
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
72 \&
73 \& my $status = unzip $input => $output [,OPTS]
74 \& or die "unzip failed: $UnzipError\en";
75 \&
76 \& my $z = IO::Uncompress::Unzip\->new( $input [OPTS] )
77 \& or die "unzip failed: $UnzipError\en";
78 \&
79 \& $status = $z\->read($buffer)
80 \& $status = $z\->read($buffer, $length)
81 \& $status = $z\->read($buffer, $length, $offset)
82 \& $line = $z\->getline()
83 \& $char = $z\->getc()
84 \& $char = $z\->ungetc()
85 \& $char = $z\->opened()
86 \&
87 \& $status = $z\->inflateSync()
88 \&
89 \& $data = $z\->trailingData()
90 \& $status = $z\->nextStream()
91 \& $data = $z\->getHeaderInfo()
92 \& $z\->tell()
93 \& $z\->seek($position, $whence)
94 \& $z\->binmode()
95 \& $z\->fileno()
96 \& $z\->eof()
97 \& $z\->close()
98 \&
99 \& $UnzipError ;
100 \&
101 \& # IO::File mode
102 \&
103 \& <$z>
104 \& read($z, $buffer);
105 \& read($z, $buffer, $length);
106 \& read($z, $buffer, $length, $offset);
107 \& tell($z)
108 \& seek($z, $position, $whence)
109 \& binmode($z)
110 \& fileno($z)
111 \& eof($z)
112 \& close($z)
113 .Ve
114 .SH DESCRIPTION
115 .IX Header "DESCRIPTION"
116 This module provides a Perl interface that allows the reading of
117 zlib files/buffers.
118 .PP
119 For writing zip files/buffers, see the companion module IO::Compress::Zip.
120 .PP
121 The primary purpose of this module is to provide \fIstreaming\fR read access to
122 zip files and buffers.
123 .PP
124 At present the following compression methods are supported by IO::Uncompress::Unzip
125 .IP "Store (0)" 5
126 .IX Item "Store (0)"
127 .PD 0
128 .IP "Deflate (8)" 5
129 .IX Item "Deflate (8)"
130 .IP "Bzip2 (12)" 5
131 .IX Item "Bzip2 (12)"
132 .PD
133 To read Bzip2 content, the module \f(CW\*(C`IO::Uncompress::Bunzip2\*(C'\fR must
134 be installed.
135 .IP "Lzma (14)" 5
136 .IX Item "Lzma (14)"
137 To read LZMA content, the module \f(CW\*(C`IO::Uncompress::UnLzma\*(C'\fR must
138 be installed.
139 .IP "Xz (95)" 5
140 .IX Item "Xz (95)"
141 To read Xz content, the module \f(CW\*(C`IO::Uncompress::UnXz\*(C'\fR must
142 be installed.
143 .IP "Zstandard (93)" 5
144 .IX Item "Zstandard (93)"
145 To read Zstandard content, the module \f(CW\*(C`IO::Uncompress::UnZstd\*(C'\fR must
146 be installed.
147 .SH "Functional Interface"
148 .IX Header "Functional Interface"
149 A top\-level function, \f(CW\*(C`unzip\*(C'\fR, is provided to carry out
150 "one\-shot" uncompression between buffers and/or files. For finer
151 control over the uncompression process, see the "OO Interface"
152 section.
153 .PP
154 .Vb 1
155 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
156 \&
157 \& unzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
158 \& or die "unzip failed: $UnzipError\en";
159 .Ve
160 .PP
161 The functional interface needs Perl5.005 or better.
162 .ie n .SS "unzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
163 .el .SS "unzip \f(CW$input_filename_or_reference\fP => \f(CW$output_filename_or_reference\fP [, OPTS]"
164 .IX Subsection "unzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
165 \&\f(CW\*(C`unzip\*(C'\fR expects at least two parameters,
166 \&\f(CW$input_filename_or_reference\fR and \f(CW$output_filename_or_reference\fR
167 and zero or more optional parameters (see "Optional Parameters")
168 .PP
169 \fIThe \fR\f(CI$input_filename_or_reference\fR\fI parameter\fR
170 .IX Subsection "The $input_filename_or_reference parameter"
171 .PP
172 The parameter, \f(CW$input_filename_or_reference\fR, is used to define the
173 source of the compressed data.
174 .PP
175 It can take one of the following forms:
176 .IP "A filename" 5
177 .IX Item "A filename"
178 If the \f(CW$input_filename_or_reference\fR parameter is a simple scalar, it is
179 assumed to be a filename. This file will be opened for reading and the
180 input data will be read from it.
181 .IP "A filehandle" 5
182 .IX Item "A filehandle"
183 If the \f(CW$input_filename_or_reference\fR parameter is a filehandle, the input
184 data will be read from it. The string \*(Aq\-\*(Aq can be used as an alias for
185 standard input.
186 .IP "A scalar reference" 5
187 .IX Item "A scalar reference"
188 If \f(CW$input_filename_or_reference\fR is a scalar reference, the input data
189 will be read from \f(CW$$input_filename_or_reference\fR.
190 .IP "An array reference" 5
191 .IX Item "An array reference"
192 If \f(CW$input_filename_or_reference\fR is an array reference, each element in
193 the array must be a filename.
194 .Sp
195 The input data will be read from each file in turn.
196 .Sp
197 The complete array will be walked to ensure that it only
198 contains valid filenames before any data is uncompressed.
199 .IP "An Input FileGlob string" 5
200 .IX Item "An Input FileGlob string"
201 If \f(CW$input_filename_or_reference\fR is a string that is delimited by the
202 characters "<" and ">" \f(CW\*(C`unzip\*(C'\fR will assume that it is an
203 \&\fIinput fileglob string\fR. The input is the list of files that match the
204 fileglob.
205 .Sp
206 See File::GlobMapper for more details.
207 .PP
208 If the \f(CW$input_filename_or_reference\fR parameter is any other type,
209 \&\f(CW\*(C`undef\*(C'\fR will be returned.
210 .PP
211 \fIThe \fR\f(CI$output_filename_or_reference\fR\fI parameter\fR
212 .IX Subsection "The $output_filename_or_reference parameter"
213 .PP
214 The parameter \f(CW$output_filename_or_reference\fR is used to control the
215 destination of the uncompressed data. This parameter can take one of
216 these forms.
217 .IP "A filename" 5
218 .IX Item "A filename"
219 If the \f(CW$output_filename_or_reference\fR parameter is a simple scalar, it is
220 assumed to be a filename. This file will be opened for writing and the
221 uncompressed data will be written to it.
222 .IP "A filehandle" 5
223 .IX Item "A filehandle"
224 If the \f(CW$output_filename_or_reference\fR parameter is a filehandle, the
225 uncompressed data will be written to it. The string \*(Aq\-\*(Aq can be used as
226 an alias for standard output.
227 .IP "A scalar reference" 5
228 .IX Item "A scalar reference"
229 If \f(CW$output_filename_or_reference\fR is a scalar reference, the
230 uncompressed data will be stored in \f(CW$$output_filename_or_reference\fR.
231 .IP "An Array Reference" 5
232 .IX Item "An Array Reference"
233 If \f(CW$output_filename_or_reference\fR is an array reference,
234 the uncompressed data will be pushed onto the array.
235 .IP "An Output FileGlob" 5
236 .IX Item "An Output FileGlob"
237 If \f(CW$output_filename_or_reference\fR is a string that is delimited by the
238 characters "<" and ">" \f(CW\*(C`unzip\*(C'\fR will assume that it is an
239 \&\fIoutput fileglob string\fR. The output is the list of files that match the
240 fileglob.
241 .Sp
242 When \f(CW$output_filename_or_reference\fR is an fileglob string,
243 \&\f(CW$input_filename_or_reference\fR must also be a fileglob string. Anything
244 else is an error.
245 .Sp
246 See File::GlobMapper for more details.
247 .PP
248 If the \f(CW$output_filename_or_reference\fR parameter is any other type,
249 \&\f(CW\*(C`undef\*(C'\fR will be returned.
250 .SS Notes
251 .IX Subsection "Notes"
252 When \f(CW$input_filename_or_reference\fR maps to multiple compressed
253 files/buffers and \f(CW$output_filename_or_reference\fR is
254 a single file/buffer, after uncompression \f(CW$output_filename_or_reference\fR will contain a
255 concatenation of all the uncompressed data from each of the input
256 files/buffers.
257 .SS "Optional Parameters"
258 .IX Subsection "Optional Parameters"
259 The optional parameters for the one\-shot function \f(CW\*(C`unzip\*(C'\fR
260 are (for the most part) identical to those used with the OO interface defined in the
261 "Constructor Options" section. The exceptions are listed below
262 .ie n .IP """AutoClose => 0|1""" 5
263 .el .IP "\f(CWAutoClose => 0|1\fR" 5
264 .IX Item "AutoClose => 0|1"
265 This option applies to any input or output data streams to
266 \&\f(CW\*(C`unzip\*(C'\fR that are filehandles.
267 .Sp
268 If \f(CW\*(C`AutoClose\*(C'\fR is specified, and the value is true, it will result in all
269 input and/or output filehandles being closed once \f(CW\*(C`unzip\*(C'\fR has
270 completed.
271 .Sp
272 This parameter defaults to 0.
273 .ie n .IP """BinModeOut => 0|1""" 5
274 .el .IP "\f(CWBinModeOut => 0|1\fR" 5
275 .IX Item "BinModeOut => 0|1"
276 This option is now a no\-op. All files will be written in binmode.
277 .ie n .IP """Append => 0|1""" 5
278 .el .IP "\f(CWAppend => 0|1\fR" 5
279 .IX Item "Append => 0|1"
280 The behaviour of this option is dependent on the type of output data
281 stream.
282 .RS 5
283 .IP \(bu 5
284 A Buffer
285 .Sp
286 If \f(CW\*(C`Append\*(C'\fR is enabled, all uncompressed data will be append to the end of
287 the output buffer. Otherwise the output buffer will be cleared before any
288 uncompressed data is written to it.
289 .IP \(bu 5
290 A Filename
291 .Sp
292 If \f(CW\*(C`Append\*(C'\fR is enabled, the file will be opened in append mode. Otherwise
293 the contents of the file, if any, will be truncated before any uncompressed
294 data is written to it.
295 .IP \(bu 5
296 A Filehandle
297 .Sp
298 If \f(CW\*(C`Append\*(C'\fR is enabled, the filehandle will be positioned to the end of
299 the file via a call to \f(CW\*(C`seek\*(C'\fR before any uncompressed data is
300 written to it. Otherwise the file pointer will not be moved.
301 .RE
302 .RS 5
303 .Sp
304 When \f(CW\*(C`Append\*(C'\fR is specified, and set to true, it will \fIappend\fR all uncompressed
305 data to the output data stream.
306 .Sp
307 So when the output is a filehandle it will carry out a seek to the eof
308 before writing any uncompressed data. If the output is a filename, it will be opened for
309 appending. If the output is a buffer, all uncompressed data will be
310 appended to the existing buffer.
311 .Sp
312 Conversely when \f(CW\*(C`Append\*(C'\fR is not specified, or it is present and is set to
313 false, it will operate as follows.
314 .Sp
315 When the output is a filename, it will truncate the contents of the file
316 before writing any uncompressed data. If the output is a filehandle
317 its position will not be changed. If the output is a buffer, it will be
318 wiped before any uncompressed data is output.
319 .Sp
320 Defaults to 0.
321 .RE
322 .ie n .IP """MultiStream => 0|1""" 5
323 .el .IP "\f(CWMultiStream => 0|1\fR" 5
324 .IX Item "MultiStream => 0|1"
325 If the input file/buffer contains multiple compressed data streams, this
326 option will uncompress the whole lot as a single data stream.
327 .Sp
328 Defaults to 0.
329 .ie n .IP """TrailingData => $scalar""" 5
330 .el .IP "\f(CWTrailingData => $scalar\fR" 5
331 .IX Item "TrailingData => $scalar"
332 Returns the data, if any, that is present immediately after the compressed
333 data stream once uncompression is complete.
334 .Sp
335 This option can be used when there is useful information immediately
336 following the compressed data stream, and you don\*(Aqt know the length of the
337 compressed data stream.
338 .Sp
339 If the input is a buffer, \f(CW\*(C`trailingData\*(C'\fR will return everything from the
340 end of the compressed data stream to the end of the buffer.
341 .Sp
342 If the input is a filehandle, \f(CW\*(C`trailingData\*(C'\fR will return the data that is
343 left in the filehandle input buffer once the end of the compressed data
344 stream has been reached. You can then use the filehandle to read the rest
345 of the input file.
346 .Sp
347 Don\*(Aqt bother using \f(CW\*(C`trailingData\*(C'\fR if the input is a filename.
348 .Sp
349 If you know the length of the compressed data stream before you start
350 uncompressing, you can avoid having to use \f(CW\*(C`trailingData\*(C'\fR by setting the
351 \&\f(CW\*(C`InputLength\*(C'\fR option.
352 .SS "OneShot Examples"
353 .IX Subsection "OneShot Examples"
354 Say you have a zip file, \f(CW\*(C`file1.zip\*(C'\fR, that only contains a
355 single member, you can read it and write the uncompressed data to the
356 file \f(CW\*(C`file1.txt\*(C'\fR like this.
357 .PP
358 .Vb 3
359 \& use strict ;
360 \& use warnings ;
361 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
362 \&
363 \& my $input = "file1.zip";
364 \& my $output = "file1.txt";
365 \& unzip $input => $output
366 \& or die "unzip failed: $UnzipError\en";
367 .Ve
368 .PP
369 If you have a zip file that contains multiple members and want to read a
370 specific member from the file, say \f(CW"data1"\fR, use the \f(CW\*(C`Name\*(C'\fR option
371 .PP
372 .Vb 3
373 \& use strict ;
374 \& use warnings ;
375 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
376 \&
377 \& my $input = "file1.zip";
378 \& my $output = "file1.txt";
379 \& unzip $input => $output, Name => "data1"
380 \& or die "unzip failed: $UnzipError\en";
381 .Ve
382 .PP
383 Alternatively, if you want to read the \f(CW"data1"\fR member into memory, use
384 a scalar reference for the \f(CW\*(C`output\*(C'\fR parameter.
385 .PP
386 .Vb 3
387 \& use strict ;
388 \& use warnings ;
389 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
390 \&
391 \& my $input = "file1.zip";
392 \& my $output ;
393 \& unzip $input => \e$output, Name => "data1"
394 \& or die "unzip failed: $UnzipError\en";
395 \& # $output now contains the uncompressed data
396 .Ve
397 .PP
398 To read from an existing Perl filehandle, \f(CW$input\fR, and write the
399 uncompressed data to a buffer, \f(CW$buffer\fR.
400 .PP
401 .Vb 4
402 \& use strict ;
403 \& use warnings ;
404 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
405 \& use IO::File ;
406 \&
407 \& my $input = IO::File\->new( "<file1.zip" )
408 \& or die "Cannot open \*(Aqfile1.zip\*(Aq: $!\en" ;
409 \& my $buffer ;
410 \& unzip $input => \e$buffer
411 \& or die "unzip failed: $UnzipError\en";
412 .Ve
413 .SH "OO Interface"
414 .IX Header "OO Interface"
415 .SS Constructor
416 .IX Subsection "Constructor"
417 The format of the constructor for IO::Uncompress::Unzip is shown below
418 .PP
419 .Vb 2
420 \& my $z = IO::Uncompress::Unzip\->new( $input [OPTS] )
421 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
422 .Ve
423 .PP
424 The constructor takes one mandatory parameter, \f(CW$input\fR, defined below, and
425 zero or more \f(CW\*(C`OPTS\*(C'\fR, defined in "Constructor Options".
426 .PP
427 Returns an \f(CW\*(C`IO::Uncompress::Unzip\*(C'\fR object on success and undef on failure.
428 The variable \f(CW$UnzipError\fR will contain an error message on failure.
429 .PP
430 If you are running Perl 5.005 or better the object, \f(CW$z\fR, returned from
431 IO::Uncompress::Unzip can be used exactly like an IO::File filehandle.
432 This means that all normal input file operations can be carried out with
433 \&\f(CW$z\fR. For example, to read a line from a compressed file/buffer you can
434 use either of these forms
435 .PP
436 .Vb 2
437 \& $line = $z\->getline();
438 \& $line = <$z>;
439 .Ve
440 .PP
441 Below is a simple exaple of using the OO interface to read the compressed file
442 \&\f(CW\*(C`myfile.zip\*(C'\fR and write its contents to stdout.
443 .PP
444 .Vb 3
445 \& my $filename = "myfile.zip";
446 \& my $z = IO::Uncompress::Unzip\->new($filename)
447 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
448 \&
449 \& while (<$z>) {
450 \& print $_;
451 \& }
452 \& $z\->close();
453 .Ve
454 .PP
455 See "EXAMPLES" for further examples
456 .PP
457 The mandatory parameter \f(CW$input\fR is used to determine the source of the
458 compressed data. This parameter can take one of three forms.
459 .IP "A filename" 5
460 .IX Item "A filename"
461 If the \f(CW$input\fR parameter is a scalar, it is assumed to be a filename. This
462 file will be opened for reading and the compressed data will be read from it.
463 .IP "A filehandle" 5
464 .IX Item "A filehandle"
465 If the \f(CW$input\fR parameter is a filehandle, the compressed data will be
466 read from it.
467 The string \*(Aq\-\*(Aq can be used as an alias for standard input.
468 .IP "A scalar reference" 5
469 .IX Item "A scalar reference"
470 If \f(CW$input\fR is a scalar reference, the compressed data will be read from
471 \&\f(CW$$input\fR.
472 .SS "Constructor Options"
473 .IX Subsection "Constructor Options"
474 The option names defined below are case insensitive and can be optionally
475 prefixed by a \*(Aq\-\*(Aq. So all of the following are valid
476 .PP
477 .Vb 4
478 \& \-AutoClose
479 \& \-autoclose
480 \& AUTOCLOSE
481 \& autoclose
482 .Ve
483 .PP
484 OPTS is a combination of the following options:
485 .ie n .IP """Name => ""membername""""" 5
486 .el .IP "\f(CWName => ""membername""\fR" 5
487 .IX Item "Name => ""membername"""
488 Open "membername" from the zip file for reading.
489 .ie n .IP """Efs => 0| 1""" 5
490 .el .IP "\f(CWEfs => 0| 1\fR" 5
491 .IX Item "Efs => 0| 1"
492 When this option is set to true AND the zip archive being read has
493 the "Language Encoding Flag" (EFS) set, the member name is assumed to be encoded in UTF\-8.
494 .Sp
495 If the member name in the zip archive is not valid UTF\-8 when this optionn is true,
496 the script will die with an error message.
497 .Sp
498 Note that this option only works with Perl 5.8.4 or better.
499 .Sp
500 This option defaults to \fBfalse\fR.
501 .ie n .IP """AutoClose => 0|1""" 5
502 .el .IP "\f(CWAutoClose => 0|1\fR" 5
503 .IX Item "AutoClose => 0|1"
504 This option is only valid when the \f(CW$input\fR parameter is a filehandle. If
505 specified, and the value is true, it will result in the file being closed once
506 either the \f(CW\*(C`close\*(C'\fR method is called or the IO::Uncompress::Unzip object is
507 destroyed.
508 .Sp
509 This parameter defaults to 0.
510 .ie n .IP """MultiStream => 0|1""" 5
511 .el .IP "\f(CWMultiStream => 0|1\fR" 5
512 .IX Item "MultiStream => 0|1"
513 Treats the complete zip file/buffer as a single compressed data
514 stream. When reading in multi\-stream mode each member of the zip
515 file/buffer will be uncompressed in turn until the end of the file/buffer
516 is encountered.
517 .Sp
518 This parameter defaults to 0.
519 .ie n .IP """Prime => $string""" 5
520 .el .IP "\f(CWPrime => $string\fR" 5
521 .IX Item "Prime => $string"
522 This option will uncompress the contents of \f(CW$string\fR before processing the
523 input file/buffer.
524 .Sp
525 This option can be useful when the compressed data is embedded in another
526 file/data structure and it is not possible to work out where the compressed
527 data begins without having to read the first few bytes. If this is the
528 case, the uncompression can be \fIprimed\fR with these bytes using this
529 option.
530 .ie n .IP """Transparent => 0|1""" 5
531 .el .IP "\f(CWTransparent => 0|1\fR" 5
532 .IX Item "Transparent => 0|1"
533 If this option is set and the input file/buffer is not compressed data,
534 the module will allow reading of it anyway.
535 .Sp
536 In addition, if the input file/buffer does contain compressed data and
537 there is non\-compressed data immediately following it, setting this option
538 will make this module treat the whole file/buffer as a single data stream.
539 .Sp
540 This option defaults to 1.
541 .ie n .IP """BlockSize => $num""" 5
542 .el .IP "\f(CWBlockSize => $num\fR" 5
543 .IX Item "BlockSize => $num"
544 When reading the compressed input data, IO::Uncompress::Unzip will read it in
545 blocks of \f(CW$num\fR bytes.
546 .Sp
547 This option defaults to 4096.
548 .ie n .IP """InputLength => $size""" 5
549 .el .IP "\f(CWInputLength => $size\fR" 5
550 .IX Item "InputLength => $size"
551 When present this option will limit the number of compressed bytes read
552 from the input file/buffer to \f(CW$size\fR. This option can be used in the
553 situation where there is useful data directly after the compressed data
554 stream and you know beforehand the exact length of the compressed data
555 stream.
556 .Sp
557 This option is mostly used when reading from a filehandle, in which case
558 the file pointer will be left pointing to the first byte directly after the
559 compressed data stream.
560 .Sp
561 This option defaults to off.
562 .ie n .IP """Append => 0|1""" 5
563 .el .IP "\f(CWAppend => 0|1\fR" 5
564 .IX Item "Append => 0|1"
565 This option controls what the \f(CW\*(C`read\*(C'\fR method does with uncompressed data.
566 .Sp
567 If set to 1, all uncompressed data will be appended to the output parameter
568 of the \f(CW\*(C`read\*(C'\fR method.
569 .Sp
570 If set to 0, the contents of the output parameter of the \f(CW\*(C`read\*(C'\fR method
571 will be overwritten by the uncompressed data.
572 .Sp
573 Defaults to 0.
574 .ie n .IP """Strict => 0|1""" 5
575 .el .IP "\f(CWStrict => 0|1\fR" 5
576 .IX Item "Strict => 0|1"
577 This option controls whether the extra checks defined below are used when
578 carrying out the decompression. When Strict is on, the extra tests are
579 carried out, when Strict is off they are not.
580 .Sp
581 The default for this option is off.
582 .SH Methods
583 .IX Header "Methods"
584 .SS read
585 .IX Subsection "read"
586 Usage is
587 .PP
588 .Vb 1
589 \& $status = $z\->read($buffer)
590 .Ve
591 .PP
592 Reads a block of compressed data (the size of the compressed block is
593 determined by the \f(CW\*(C`Buffer\*(C'\fR option in the constructor), uncompresses it and
594 writes any uncompressed data into \f(CW$buffer\fR. If the \f(CW\*(C`Append\*(C'\fR parameter is
595 set in the constructor, the uncompressed data will be appended to the
596 \&\f(CW$buffer\fR parameter. Otherwise \f(CW$buffer\fR will be overwritten.
597 .PP
598 Returns the number of uncompressed bytes written to \f(CW$buffer\fR, zero if eof
599 or a negative number on error.
600 .SS read
601 .IX Subsection "read"
602 Usage is
603 .PP
604 .Vb 2
605 \& $status = $z\->read($buffer, $length)
606 \& $status = $z\->read($buffer, $length, $offset)
607 \&
608 \& $status = read($z, $buffer, $length)
609 \& $status = read($z, $buffer, $length, $offset)
610 .Ve
611 .PP
612 Attempt to read \f(CW$length\fR bytes of uncompressed data into \f(CW$buffer\fR.
613 .PP
614 The main difference between this form of the \f(CW\*(C`read\*(C'\fR method and the
615 previous one, is that this one will attempt to return \fIexactly\fR \f(CW$length\fR
616 bytes. The only circumstances that this function will not is if end\-of\-file
617 or an IO error is encountered.
618 .PP
619 Returns the number of uncompressed bytes written to \f(CW$buffer\fR, zero if eof
620 or a negative number on error.
621 .SS getline
622 .IX Subsection "getline"
623 Usage is
624 .PP
625 .Vb 2
626 \& $line = $z\->getline()
627 \& $line = <$z>
628 .Ve
629 .PP
630 Reads a single line.
631 .PP
632 This method fully supports the use of the variable \f(CW$/\fR (or
633 \&\f(CW$INPUT_RECORD_SEPARATOR\fR or \f(CW$RS\fR when \f(CW\*(C`English\*(C'\fR is in use) to
634 determine what constitutes an end of line. Paragraph mode, record mode and
635 file slurp mode are all supported.
636 .SS getc
637 .IX Subsection "getc"
638 Usage is
639 .PP
640 .Vb 1
641 \& $char = $z\->getc()
642 .Ve
643 .PP
644 Read a single character.
645 .SS ungetc
646 .IX Subsection "ungetc"
647 Usage is
648 .PP
649 .Vb 1
650 \& $char = $z\->ungetc($string)
651 .Ve
652 .SS inflateSync
653 .IX Subsection "inflateSync"
654 Usage is
655 .PP
656 .Vb 1
657 \& $status = $z\->inflateSync()
658 .Ve
659 .PP
660 TODO
661 .SS getHeaderInfo
662 .IX Subsection "getHeaderInfo"
663 Usage is
664 .PP
665 .Vb 2
666 \& $hdr = $z\->getHeaderInfo();
667 \& @hdrs = $z\->getHeaderInfo();
668 .Ve
669 .PP
670 This method returns either a hash reference (in scalar context) or a list
671 or hash references (in array context) that contains information about each
672 of the header fields in the compressed data stream(s).
673 .SS tell
674 .IX Subsection "tell"
675 Usage is
676 .PP
677 .Vb 2
678 \& $z\->tell()
679 \& tell $z
680 .Ve
681 .PP
682 Returns the uncompressed file offset.
683 .SS eof
684 .IX Subsection "eof"
685 Usage is
686 .PP
687 .Vb 2
688 \& $z\->eof();
689 \& eof($z);
690 .Ve
691 .PP
692 Returns true if the end of the compressed input stream has been reached.
693 .SS seek
694 .IX Subsection "seek"
695 .Vb 2
696 \& $z\->seek($position, $whence);
697 \& seek($z, $position, $whence);
698 .Ve
699 .PP
700 Provides a sub\-set of the \f(CW\*(C`seek\*(C'\fR functionality, with the restriction
701 that it is only legal to seek forward in the input file/buffer.
702 It is a fatal error to attempt to seek backward.
703 .PP
704 Note that the implementation of \f(CW\*(C`seek\*(C'\fR in this module does not provide
705 true random access to a compressed file/buffer. It works by uncompressing
706 data from the current offset in the file/buffer until it reaches the
707 uncompressed offset specified in the parameters to \f(CW\*(C`seek\*(C'\fR. For very small
708 files this may be acceptable behaviour. For large files it may cause an
709 unacceptable delay.
710 .PP
711 The \f(CW$whence\fR parameter takes one the usual values, namely SEEK_SET,
712 SEEK_CUR or SEEK_END.
713 .PP
714 Returns 1 on success, 0 on failure.
715 .SS binmode
716 .IX Subsection "binmode"
717 Usage is
718 .PP
719 .Vb 2
720 \& $z\->binmode
721 \& binmode $z ;
722 .Ve
723 .PP
724 This is a noop provided for completeness.
725 .SS opened
726 .IX Subsection "opened"
727 .Vb 1
728 \& $z\->opened()
729 .Ve
730 .PP
731 Returns true if the object currently refers to a opened file/buffer.
732 .SS autoflush
733 .IX Subsection "autoflush"
734 .Vb 2
735 \& my $prev = $z\->autoflush()
736 \& my $prev = $z\->autoflush(EXPR)
737 .Ve
738 .PP
739 If the \f(CW$z\fR object is associated with a file or a filehandle, this method
740 returns the current autoflush setting for the underlying filehandle. If
741 \&\f(CW\*(C`EXPR\*(C'\fR is present, and is non\-zero, it will enable flushing after every
742 write/print operation.
743 .PP
744 If \f(CW$z\fR is associated with a buffer, this method has no effect and always
745 returns \f(CW\*(C`undef\*(C'\fR.
746 .PP
747 \&\fBNote\fR that the special variable \f(CW$|\fR \fBcannot\fR be used to set or
748 retrieve the autoflush setting.
749 .SS input_line_number
750 .IX Subsection "input_line_number"
751 .Vb 2
752 \& $z\->input_line_number()
753 \& $z\->input_line_number(EXPR)
754 .Ve
755 .PP
756 Returns the current uncompressed line number. If \f(CW\*(C`EXPR\*(C'\fR is present it has
757 the effect of setting the line number. Note that setting the line number
758 does not change the current position within the file/buffer being read.
759 .PP
760 The contents of \f(CW$/\fR are used to determine what constitutes a line
761 terminator.
762 .SS fileno
763 .IX Subsection "fileno"
764 .Vb 2
765 \& $z\->fileno()
766 \& fileno($z)
767 .Ve
768 .PP
769 If the \f(CW$z\fR object is associated with a file or a filehandle, \f(CW\*(C`fileno\*(C'\fR
770 will return the underlying file descriptor. Once the \f(CW\*(C`close\*(C'\fR method is
771 called \f(CW\*(C`fileno\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR.
772 .PP
773 If the \f(CW$z\fR object is associated with a buffer, this method will return
774 \&\f(CW\*(C`undef\*(C'\fR.
775 .SS close
776 .IX Subsection "close"
777 .Vb 2
778 \& $z\->close() ;
779 \& close $z ;
780 .Ve
781 .PP
782 Closes the output file/buffer.
783 .PP
784 For most versions of Perl this method will be automatically invoked if
785 the IO::Uncompress::Unzip object is destroyed (either explicitly or by the
786 variable with the reference to the object going out of scope). The
787 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
788 these cases, the \f(CW\*(C`close\*(C'\fR method will be called automatically, but
789 not until global destruction of all live objects when the program is
790 terminating.
791 .PP
792 Therefore, if you want your scripts to be able to run on all versions
793 of Perl, you should call \f(CW\*(C`close\*(C'\fR explicitly and not rely on automatic
794 closing.
795 .PP
796 Returns true on success, otherwise 0.
797 .PP
798 If the \f(CW\*(C`AutoClose\*(C'\fR option has been enabled when the IO::Uncompress::Unzip
799 object was created, and the object is associated with a file, the
800 underlying file will also be closed.
801 .SS nextStream
802 .IX Subsection "nextStream"
803 Usage is
804 .PP
805 .Vb 1
806 \& my $status = $z\->nextStream();
807 .Ve
808 .PP
809 Skips to the next compressed data stream in the input file/buffer. If a new
810 compressed data stream is found, the eof marker will be cleared and \f(CW$.\fR
811 will be reset to 0.
812 .PP
813 If trailing data is present immediately after the zip archive and the
814 \&\f(CW\*(C`Transparent\*(C'\fR option is enabled, this method will consider that trailing
815 data to be another member of the zip archive.
816 .PP
817 Returns 1 if a new stream was found, 0 if none was found, and \-1 if an
818 error was encountered.
819 .SS trailingData
820 .IX Subsection "trailingData"
821 Usage is
822 .PP
823 .Vb 1
824 \& my $data = $z\->trailingData();
825 .Ve
826 .PP
827 Returns the data, if any, that is present immediately after the compressed
828 data stream once uncompression is complete. It only makes sense to call
829 this method once the end of the compressed data stream has been
830 encountered.
831 .PP
832 This option can be used when there is useful information immediately
833 following the compressed data stream, and you don\*(Aqt know the length of the
834 compressed data stream.
835 .PP
836 If the input is a buffer, \f(CW\*(C`trailingData\*(C'\fR will return everything from the
837 end of the compressed data stream to the end of the buffer.
838 .PP
839 If the input is a filehandle, \f(CW\*(C`trailingData\*(C'\fR will return the data that is
840 left in the filehandle input buffer once the end of the compressed data
841 stream has been reached. You can then use the filehandle to read the rest
842 of the input file.
843 .PP
844 Don\*(Aqt bother using \f(CW\*(C`trailingData\*(C'\fR if the input is a filename.
845 .PP
846 If you know the length of the compressed data stream before you start
847 uncompressing, you can avoid having to use \f(CW\*(C`trailingData\*(C'\fR by setting the
848 \&\f(CW\*(C`InputLength\*(C'\fR option in the constructor.
849 .SH Importing
850 .IX Header "Importing"
851 No symbolic constants are required by IO::Uncompress::Unzip at present.
852 .IP :all 5
853 .IX Item ":all"
854 Imports \f(CW\*(C`unzip\*(C'\fR and \f(CW$UnzipError\fR.
855 Same as doing this
856 .Sp
857 .Vb 1
858 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
859 .Ve
860 .SH EXAMPLES
861 .IX Header "EXAMPLES"
862 .SS "Simple Read"
863 .IX Subsection "Simple Read"
864 Say you have a zip file, \f(CW\*(C`file1.zip\*(C'\fR, that only contains a
865 single member, you can read it and write the uncompressed data to the
866 file \f(CW\*(C`file1.txt\*(C'\fR like this.
867 .PP
868 .Vb 3
869 \& use strict ;
870 \& use warnings ;
871 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
872 \&
873 \& my $filename = "file1.zip";
874 \& my $z = IO::Uncompress::Unzip\->new($filename)
875 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
876 \& open my $out, ">", "file1.txt";
877 \&
878 \& while (<$z>) {
879 \& print $out $_;
880 \& }
881 \& $z\->close();
882 .Ve
883 .PP
884 If you have a zip file that contains multiple members and want to read a
885 specific member from the file, say \f(CW"data1"\fR, use the \f(CW\*(C`Name\*(C'\fR option when
886 constructing the
887 .PP
888 .Vb 3
889 \& use strict ;
890 \& use warnings ;
891 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
892 \&
893 \& my $filename = "file1.zip";
894 \& my $z = IO::Uncompress::Unzip\->new($filename, Name => "data1")
895 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
896 .Ve
897 .SS "Walking through a zip file"
898 .IX Subsection "Walking through a zip file"
899 The code below can be used to traverse a zip file, one compressed data
900 stream at a time.
901 .PP
902 .Vb 1
903 \& use IO::Uncompress::Unzip qw($UnzipError);
904 \&
905 \& my $zipfile = "somefile.zip";
906 \& my $u = IO::Uncompress::Unzip\->new( $zipfile )
907 \& or die "Cannot open $zipfile: $UnzipError";
908 \&
909 \& my $status;
910 \& for ($status = 1; $status > 0; $status = $u\->nextStream())
911 \& {
912 \&
913 \& my $name = $u\->getHeaderInfo()\->{Name};
914 \& warn "Processing member $name\en" ;
915 \&
916 \& my $buff;
917 \& while (($status = $u\->read($buff)) > 0) {
918 \& # Do something here
919 \& }
920 \&
921 \& last if $status < 0;
922 \& }
923 \&
924 \& die "Error processing $zipfile: $!\en"
925 \& if $status < 0 ;
926 .Ve
927 .PP
928 Each individual compressed data stream is read until the logical
929 end\-of\-file is reached. Then \f(CW\*(C`nextStream\*(C'\fR is called. This will skip to the
930 start of the next compressed data stream and clear the end\-of\-file flag.
931 .PP
932 It is also worth noting that \f(CW\*(C`nextStream\*(C'\fR can be called at any time \-\- you
933 don\*(Aqt have to wait until you have exhausted a compressed data stream before
934 skipping to the next one.
935 .SS "Unzipping a complete zip file to disk"
936 .IX Subsection "Unzipping a complete zip file to disk"
937 Daniel S. Sterling has written a script that uses \f(CW\*(C`IO::Uncompress::UnZip\*(C'\fR
938 to read a zip file and unzip its contents to disk.
939 .PP
940 The script is available from <https://gist.github.com/eqhmcow/5389877>
941 .SS "Working with Net::FTP"
942 .IX Subsection "Working with Net::FTP"
943 See IO::Compress::FAQ
944 .SH SUPPORT
945 .IX Header "SUPPORT"
946 General feedback/questions/bug reports should be sent to
947 <https://github.com/pmqs/IO\-Compress/issues> (preferred) or
948 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO\-Compress>.
949 .SH "SEE ALSO"
950 .IX Header "SEE ALSO"
951 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Compress::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd, IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
952 .PP
953 IO::Compress::FAQ
954 .PP
955 File::GlobMapper, Archive::Zip,
956 Archive::Tar,
957 IO::Zlib
958 .PP
959 For RFC 1950, 1951 and 1952 see
960 <https://datatracker.ietf.org/doc/html/rfc1950>,
961 <https://datatracker.ietf.org/doc/html/rfc1951> and
962 <https://datatracker.ietf.org/doc/html/rfc1952>
963 .PP
964 The \fIzlib\fR compression library was written by Jean\-loup Gailly
965 \&\f(CW\*(C`[email protected]\*(C'\fR and Mark Adler \f(CW\*(C`[email protected]\*(C'\fR.
966 .PP
967 The primary site for the \fIzlib\fR compression library is
968 <http://www.zlib.org>.
969 .PP
970 The primary site for the \fIzlib\-ng\fR compression library is
971 <https://github.com/zlib\-ng/zlib\-ng>.
972 .PP
973 The primary site for gzip is <http://www.gzip.org>.
974 .SH AUTHOR
975 .IX Header "AUTHOR"
976 This module was written by Paul Marquess, \f(CW\*(C`[email protected]\*(C'\fR.
977 .SH "MODIFICATION HISTORY"
978 .IX Header "MODIFICATION HISTORY"
979 See the Changes file.
980 .SH "COPYRIGHT AND LICENSE"
981 .IX Header "COPYRIGHT AND LICENSE"
982 Copyright (c) 2005\-2024 Paul Marquess. All rights reserved.
983 .PP
984 This program is free software; you can redistribute it and/or
985 modify it under the same terms as Perl itself.