1IO::Uncompress::UnLzip(U3s)er Contributed Perl DocumentatIiOo:n:Uncompress::UnLzip(3)
2
3
4
6 IO::Uncompress::UnLzip - Read lzip files/buffers
7
9 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
10
11 my $status = unlzip $input => $output [,OPTS]
12 or die "unlzip failed: $UnLzipError\n";
13
14 my $z = new IO::Uncompress::UnLzip $input [OPTS]
15 or die "unlzip failed: $UnLzipError\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 $data = $z->trailingData()
26 $status = $z->nextStream()
27 $data = $z->getHeaderInfo()
28 $z->tell()
29 $z->seek($position, $whence)
30 $z->binmode()
31 $z->fileno()
32 $z->eof()
33 $z->close()
34
35 $UnLzipError ;
36
37 # IO::File mode
38
39 <$z>
40 read($z, $buffer);
41 read($z, $buffer, $length);
42 read($z, $buffer, $length, $offset);
43 tell($z)
44 seek($z, $position, $whence)
45 binmode($z)
46 fileno($z)
47 eof($z)
48 close($z)
49
51 This module provides a Perl interface that allows the reading of lzma
52 files/buffers.
53
54 For writing lzip files/buffers, see the companion module
55 IO::Compress::Lzip.
56
58 A top-level function, "unlzip", is provided to carry out "one-shot"
59 uncompression between buffers and/or files. For finer control over the
60 uncompression process, see the "OO Interface" section.
61
62 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
63
64 unlzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
65 or die "unlzip failed: $UnLzipError\n";
66
67 The functional interface needs Perl5.005 or better.
68
69 unlzip $input_filename_or_reference => $output_filename_or_reference [,
70 OPTS]
71 "unlzip" expects at least two parameters, $input_filename_or_reference
72 and $output_filename_or_reference and zero or more optional parameters
73 (see "Optional Parameters")
74
75 The $input_filename_or_reference parameter
76
77 The parameter, $input_filename_or_reference, is used to define the
78 source of the compressed data.
79
80 It can take one of the following forms:
81
82 A filename
83 If the $input_filename_or_reference parameter is a simple scalar,
84 it is assumed to be a filename. This file will be opened for
85 reading and the input data will be read from it.
86
87 A filehandle
88 If the $input_filename_or_reference parameter is a filehandle, the
89 input data will be read from it. The string '-' can be used as an
90 alias for standard input.
91
92 A scalar reference
93 If $input_filename_or_reference is a scalar reference, the input
94 data will be read from $$input_filename_or_reference.
95
96 An array reference
97 If $input_filename_or_reference is an array reference, each
98 element in the array must be 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_filename_or_reference is a string that is delimited by
107 the characters "<" and ">" "unlzip" will assume that it is an
108 input fileglob string. The input is the list of files that match
109 the fileglob.
110
111 See File::GlobMapper for more details.
112
113 If the $input_filename_or_reference parameter is any other type,
114 "undef" will be returned.
115
116 The $output_filename_or_reference parameter
117
118 The parameter $output_filename_or_reference is used to control the
119 destination of the uncompressed data. This parameter can take one of
120 these forms.
121
122 A filename
123 If the $output_filename_or_reference parameter is a simple scalar,
124 it is assumed to be a filename. This file will be opened for
125 writing and the uncompressed data will be written to it.
126
127 A filehandle
128 If the $output_filename_or_reference parameter is a filehandle,
129 the uncompressed data will be written to it. The string '-' can
130 be used as an alias for standard output.
131
132 A scalar reference
133 If $output_filename_or_reference is a scalar reference, the
134 uncompressed data will be stored in
135 $$output_filename_or_reference.
136
137 An Array Reference
138 If $output_filename_or_reference is an array reference, the
139 uncompressed data will be pushed onto the array.
140
141 An Output FileGlob
142 If $output_filename_or_reference is a string that is delimited by
143 the characters "<" and ">" "unlzip" will assume that it is an
144 output fileglob string. The output is the list of files that match
145 the fileglob.
146
147 When $output_filename_or_reference is an fileglob string,
148 $input_filename_or_reference must also be a fileglob string.
149 Anything else is an error.
150
151 See File::GlobMapper for more details.
152
153 If the $output_filename_or_reference parameter is any other type,
154 "undef" will be returned.
155
156 Notes
157 When $input_filename_or_reference maps to multiple compressed
158 files/buffers and $output_filename_or_reference is a single
159 file/buffer, after uncompression $output_filename_or_reference will
160 contain a concatenation of all the uncompressed data from each of the
161 input files/buffers.
162
163 Optional Parameters
164 The optional parameters for the one-shot function "unlzip" are (for the
165 most part) identical to those used with the OO interface defined in the
166 "Constructor Options" section. The exceptions are listed below
167
168 "AutoClose => 0|1"
169 This option applies to any input or output data streams to
170 "unlzip" 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 "unlzip"
174 has completed.
175
176 This parameter defaults to 0.
177
178 "BinModeOut => 0|1"
179 This option is now a no-op. All files will be written in binmode.
180
181 "Append => 0|1"
182 The behaviour of this option is dependent on the type of output
183 data stream.
184
185 · A Buffer
186
187 If "Append" is enabled, all uncompressed data will be append
188 to the end of the output buffer. Otherwise the output buffer
189 will be cleared before any uncompressed data is written to
190 it.
191
192 · A Filename
193
194 If "Append" is enabled, the file will be opened in append
195 mode. Otherwise the contents of the file, if any, will be
196 truncated before any uncompressed data is written to it.
197
198 · A Filehandle
199
200 If "Append" is enabled, the filehandle will be positioned to
201 the end of the file via a call to "seek" before any
202 uncompressed data is written to it. Otherwise the file
203 pointer will not be moved.
204
205 When "Append" is specified, and set to true, it will append all
206 uncompressed data to the output data stream.
207
208 So when the output is a filehandle it will carry out a seek to the
209 eof before writing any uncompressed data. If the output is a
210 filename, it will be opened for appending. If the output is a
211 buffer, all uncompressed data will be appended to the existing
212 buffer.
213
214 Conversely when "Append" is not specified, or it is present and is
215 set to false, it will operate as follows.
216
217 When the output is a filename, it will truncate the contents of
218 the file before writing any uncompressed data. If the output is a
219 filehandle its position will not be changed. If the output is a
220 buffer, it will be wiped before any uncompressed data is output.
221
222 Defaults to 0.
223
224 "MultiStream => 0|1"
225 If the input file/buffer contains multiple compressed data
226 streams, this option will uncompress the whole lot as a single
227 data stream.
228
229 Defaults to 0.
230
231 "TrailingData => $scalar"
232 Returns the data, if any, that is present immediately after the
233 compressed data stream once uncompression is complete.
234
235 This option can be used when there is useful information
236 immediately following the compressed data stream, and you don't
237 know the length of the compressed data stream.
238
239 If the input is a buffer, "trailingData" will return everything
240 from the end of the compressed data stream to the end of the
241 buffer.
242
243 If the input is a filehandle, "trailingData" will return the data
244 that is left in the filehandle input buffer once the end of the
245 compressed data stream has been reached. You can then use the
246 filehandle to read the rest of the input file.
247
248 Don't bother using "trailingData" if the input is a filename.
249
250 If you know the length of the compressed data stream before you
251 start uncompressing, you can avoid having to use "trailingData" by
252 setting the "InputLength" option.
253
254 Examples
255 To read the contents of the file "file1.txt.xz" and write the
256 uncompressed data to the file "file1.txt".
257
258 use strict ;
259 use warnings ;
260 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
261
262 my $input = "file1.txt.xz";
263 my $output = "file1.txt";
264 unlzip $input => $output
265 or die "unlzip failed: $UnLzipError\n";
266
267 To read from an existing Perl filehandle, $input, and write the
268 uncompressed data to a buffer, $buffer.
269
270 use strict ;
271 use warnings ;
272 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
273 use IO::File ;
274
275 my $input = new IO::File "<file1.txt.xz"
276 or die "Cannot open 'file1.txt.xz': $!\n" ;
277 my $buffer ;
278 unlzip $input => \$buffer
279 or die "unlzip failed: $UnLzipError\n";
280
281 To uncompress all files in the directory "/my/home" that match
282 "*.txt.xz" and store the compressed data in the same directory
283
284 use strict ;
285 use warnings ;
286 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
287
288 unlzip '</my/home/*.txt.xz>' => '</my/home/#1.txt>'
289 or die "unlzip failed: $UnLzipError\n";
290
291 and if you want to compress each file one at a time, this will do the
292 trick
293
294 use strict ;
295 use warnings ;
296 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
297
298 for my $input ( glob "/my/home/*.txt.xz" )
299 {
300 my $output = $input;
301 $output =~ s/.xz// ;
302 unlzip $input => $output
303 or die "Error compressing '$input': $UnLzipError\n";
304 }
305
307 Constructor
308 The format of the constructor for IO::Uncompress::UnLzip is shown below
309
310 my $z = new IO::Uncompress::UnLzip $input [OPTS]
311 or die "IO::Uncompress::UnLzip failed: $UnLzipError\n";
312
313 Returns an "IO::Uncompress::UnLzip" object on success and undef on
314 failure. The variable $UnLzipError will contain an error message on
315 failure.
316
317 If you are running Perl 5.005 or better the object, $z, returned from
318 IO::Uncompress::UnLzip can be used exactly like an IO::File filehandle.
319 This means that all normal input file operations can be carried out
320 with $z. For example, to read a line from a compressed file/buffer you
321 can use either of these forms
322
323 $line = $z->getline();
324 $line = <$z>;
325
326 The mandatory parameter $input is used to determine the source of the
327 compressed data. This parameter can take one of three forms.
328
329 A filename
330 If the $input parameter is a scalar, it is assumed to be a
331 filename. This file will be opened for reading and the compressed
332 data will be read from it.
333
334 A filehandle
335 If the $input parameter is a filehandle, the compressed data will
336 be read from it. The string '-' can be used as an alias for
337 standard input.
338
339 A scalar reference
340 If $input is a scalar reference, the compressed data will be read
341 from $$input.
342
343 Constructor Options
344 The option names defined below are case insensitive and can be
345 optionally prefixed by a '-'. So all of the following are valid
346
347 -AutoClose
348 -autoclose
349 AUTOCLOSE
350 autoclose
351
352 OPTS is a combination of the following options:
353
354 "AutoClose => 0|1"
355 This option is only valid when the $input parameter is a
356 filehandle. If specified, and the value is true, it will result in
357 the file being closed once either the "close" method is called or
358 the IO::Uncompress::UnLzip object is destroyed.
359
360 This parameter defaults to 0.
361
362 "MultiStream => 0|1"
363 Allows multiple concatenated compressed streams to be treated as a
364 single compressed stream. Decompression will stop once either the
365 end of the file/buffer is reached, an error is encountered
366 (premature eof, corrupt compressed data) or the end of a stream is
367 not immediately followed by the start of another stream.
368
369 This parameter defaults to 0.
370
371 "Prime => $string"
372 This option will uncompress the contents of $string before
373 processing the input file/buffer.
374
375 This option can be useful when the compressed data is embedded in
376 another file/data structure and it is not possible to work out
377 where the compressed data begins without having to read the first
378 few bytes. If this is the case, the uncompression can be primed
379 with these bytes using this option.
380
381 "Transparent => 0|1"
382 If this option is set and the input file/buffer is not compressed
383 data, the module will allow reading of it anyway.
384
385 In addition, if the input file/buffer does contain compressed data
386 and there is non-compressed data immediately following it, setting
387 this option will make this module treat the whole file/buffer as a
388 single data stream.
389
390 This option defaults to 1.
391
392 "BlockSize => $num"
393 When reading the compressed input data, IO::Uncompress::UnLzip
394 will read it in blocks of $num bytes.
395
396 This option defaults to 4096.
397
398 "InputLength => $size"
399 When present this option will limit the number of compressed bytes
400 read from the input file/buffer to $size. This option can be used
401 in the situation where there is useful data directly after the
402 compressed data stream and you know beforehand the exact length of
403 the compressed data stream.
404
405 This option is mostly used when reading from a filehandle, in
406 which case the file pointer will be left pointing to the first
407 byte directly after the compressed data stream.
408
409 This option defaults to off.
410
411 "Append => 0|1"
412 This option controls what the "read" method does with uncompressed
413 data.
414
415 If set to 1, all uncompressed data will be appended to the output
416 parameter of the "read" method.
417
418 If set to 0, the contents of the output parameter of the "read"
419 method will be overwritten by the uncompressed data.
420
421 Defaults to 0.
422
423 "Strict => 0|1"
424 This option controls whether the extra checks defined below are
425 used when carrying out the decompression. When Strict is on, the
426 extra tests are carried out, when Strict is off they are not.
427
428 The default for this option is off.
429
430 Examples
431 TODO
432
434 read
435 Usage is
436
437 $status = $z->read($buffer)
438
439 Reads a block of compressed data (the size of the compressed block is
440 determined by the "Buffer" option in the constructor), uncompresses it
441 and writes any uncompressed data into $buffer. If the "Append"
442 parameter is set in the constructor, the uncompressed data will be
443 appended to the $buffer parameter. Otherwise $buffer will be
444 overwritten.
445
446 Returns the number of uncompressed bytes written to $buffer, zero if
447 eof or a negative number on error.
448
449 read
450 Usage is
451
452 $status = $z->read($buffer, $length)
453 $status = $z->read($buffer, $length, $offset)
454
455 $status = read($z, $buffer, $length)
456 $status = read($z, $buffer, $length, $offset)
457
458 Attempt to read $length bytes of uncompressed data into $buffer.
459
460 The main difference between this form of the "read" method and the
461 previous one, is that this one will attempt to return exactly $length
462 bytes. The only circumstances that this function will not is if end-of-
463 file or an IO error is encountered.
464
465 Returns the number of uncompressed bytes written to $buffer, zero if
466 eof or a negative number on error.
467
468 getline
469 Usage is
470
471 $line = $z->getline()
472 $line = <$z>
473
474 Reads a single line.
475
476 This method fully supports the use of the variable $/ (or
477 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
478 what constitutes an end of line. Paragraph mode, record mode and file
479 slurp mode are all supported.
480
481 getc
482 Usage is
483
484 $char = $z->getc()
485
486 Read a single character.
487
488 ungetc
489 Usage is
490
491 $char = $z->ungetc($string)
492
493 getHeaderInfo
494 Usage is
495
496 $hdr = $z->getHeaderInfo();
497 @hdrs = $z->getHeaderInfo();
498
499 This method returns either a hash reference (in scalar context) or a
500 list or hash references (in array context) that contains information
501 about each of the header fields in the compressed data stream(s).
502
503 tell
504 Usage is
505
506 $z->tell()
507 tell $z
508
509 Returns the uncompressed file offset.
510
511 eof
512 Usage is
513
514 $z->eof();
515 eof($z);
516
517 Returns true if the end of the compressed input stream has been
518 reached.
519
520 seek
521 $z->seek($position, $whence);
522 seek($z, $position, $whence);
523
524 Provides a sub-set of the "seek" functionality, with the restriction
525 that it is only legal to seek forward in the input file/buffer. It is
526 a fatal error to attempt to seek backward.
527
528 Note that the implementation of "seek" in this module does not provide
529 true random access to a compressed file/buffer. It works by
530 uncompressing data from the current offset in the file/buffer until it
531 reaches the uncompressed offset specified in the parameters to "seek".
532 For very small files this may be acceptable behaviour. For large files
533 it may cause an unacceptable delay.
534
535 The $whence parameter takes one the usual values, namely SEEK_SET,
536 SEEK_CUR or SEEK_END.
537
538 Returns 1 on success, 0 on failure.
539
540 binmode
541 Usage is
542
543 $z->binmode
544 binmode $z ;
545
546 This is a noop provided for completeness.
547
548 opened
549 $z->opened()
550
551 Returns true if the object currently refers to a opened file/buffer.
552
553 autoflush
554 my $prev = $z->autoflush()
555 my $prev = $z->autoflush(EXPR)
556
557 If the $z object is associated with a file or a filehandle, this method
558 returns the current autoflush setting for the underlying filehandle. If
559 "EXPR" is present, and is non-zero, it will enable flushing after every
560 write/print operation.
561
562 If $z is associated with a buffer, this method has no effect and always
563 returns "undef".
564
565 Note that the special variable $| cannot be used to set or retrieve the
566 autoflush setting.
567
568 input_line_number
569 $z->input_line_number()
570 $z->input_line_number(EXPR)
571
572 Returns the current uncompressed line number. If "EXPR" is present it
573 has the effect of setting the line number. Note that setting the line
574 number does not change the current position within the file/buffer
575 being read.
576
577 The contents of $/ are used to determine what constitutes a line
578 terminator.
579
580 fileno
581 $z->fileno()
582 fileno($z)
583
584 If the $z object is associated with a file or a filehandle, "fileno"
585 will return the underlying file descriptor. Once the "close" method is
586 called "fileno" will return "undef".
587
588 If the $z object is associated with a buffer, this method will return
589 "undef".
590
591 close
592 $z->close() ;
593 close $z ;
594
595 Closes the output file/buffer.
596
597 For most versions of Perl this method will be automatically invoked if
598 the IO::Uncompress::UnLzip object is destroyed (either explicitly or by
599 the variable with the reference to the object going out of scope). The
600 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
601 cases, the "close" method will be called automatically, but not until
602 global destruction of all live objects when the program is terminating.
603
604 Therefore, if you want your scripts to be able to run on all versions
605 of Perl, you should call "close" explicitly and not rely on automatic
606 closing.
607
608 Returns true on success, otherwise 0.
609
610 If the "AutoClose" option has been enabled when the
611 IO::Uncompress::UnLzip object was created, and the object is associated
612 with a file, the underlying file will also be closed.
613
614 nextStream
615 Usage is
616
617 my $status = $z->nextStream();
618
619 Skips to the next compressed data stream in the input file/buffer. If a
620 new compressed data stream is found, the eof marker will be cleared and
621 $. will be reset to 0.
622
623 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
624 error was encountered.
625
626 trailingData
627 Usage is
628
629 my $data = $z->trailingData();
630
631 Returns the data, if any, that is present immediately after the
632 compressed data stream once uncompression is complete. It only makes
633 sense to call this method once the end of the compressed data stream
634 has been encountered.
635
636 This option can be used when there is useful information immediately
637 following the compressed data stream, and you don't know the length of
638 the compressed data stream.
639
640 If the input is a buffer, "trailingData" will return everything from
641 the end of the compressed data stream to the end of the buffer.
642
643 If the input is a filehandle, "trailingData" will return the data that
644 is left in the filehandle input buffer once the end of the compressed
645 data stream has been reached. You can then use the filehandle to read
646 the rest of the input file.
647
648 Don't bother using "trailingData" if the input is a filename.
649
650 If you know the length of the compressed data stream before you start
651 uncompressing, you can avoid having to use "trailingData" by setting
652 the "InputLength" option in the constructor.
653
655 No symbolic constants are required by this IO::Uncompress::UnLzip at
656 present.
657
658 :all Imports "unlzip" and $UnLzipError. Same as doing this
659
660 use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
661
664 General feedback/questions/bug reports should be sent to
665 <https://github.com/pmqs/IO-Compress-Lzma/issues> (preferred) or
666 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Lzma>.
667
669 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
670 IO::Compress::Deflate, IO::Uncompress::Inflate,
671 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
672 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
673 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
674 IO::Compress::Lzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
675 IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
676 IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
677 IO::Uncompress::AnyUncompress
678
679 IO::Compress::FAQ
680
681 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
682
684 This module was written by Paul Marquess, "pmqs@cpan.org".
685
687 See the Changes file.
688
690 Copyright (c) 2005-2019 Paul Marquess. All rights reserved.
691
692 This program is free software; you can redistribute it and/or modify it
693 under the same terms as Perl itself.
694
695
696
697perl v5.30.1 2020-01-30 IO::Uncompress::UnLzip(3)