1IO::Uncompress::UnLzip(U3s)er Contributed Perl DocumentatIiOo:n:Uncompress::UnLzip(3)
2
3
4

NAME

6       IO::Uncompress::UnLzip - Read lzip files/buffers
7

SYNOPSIS

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 = IO::Uncompress::UnLzip->new( $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

DESCRIPTION

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

Functional Interface

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   OneShot 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 = IO::File->new( "<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

OO Interface

307   Constructor
308       The format of the constructor for IO::Uncompress::UnLzip is shown below
309
310           my $z = IO::Uncompress::UnLzip->new( $input [OPTS] )
311               or die "IO::Uncompress::UnLzip failed: $UnLzipError\n";
312
313       The constructor takes one mandatory parameter, $input, defined below,
314       and zero or more "OPTS", defined in "Constructor Options".
315
316       Returns an "IO::Uncompress::UnLzip" object on success and undef on
317       failure.  The variable $UnLzipError will contain an error message on
318       failure.
319
320       If you are running Perl 5.005 or better the object, $z, returned from
321       IO::Uncompress::UnLzip can be used exactly like an IO::File filehandle.
322       This means that all normal input file operations can be carried out
323       with $z.  For example, to read a line from a compressed file/buffer you
324       can use either of these forms
325
326           $line = $z->getline();
327           $line = <$z>;
328
329       Below is a simple exaple of using the OO interface to read the
330       compressed file "myfile.xz" and write its contents to stdout.
331
332           my $filename = "myfile.xz";
333           my $z = IO::Uncompress::UnLzip->new($filename)
334               or die "IO::Uncompress::UnLzip failed: $UnLzipError\n";
335
336           while (<$z>) {
337               print $_;
338           }
339           $z->close();
340
341       See "EXAMPLES" for further examples
342
343       The mandatory parameter $input is used to determine the source of the
344       compressed data. This parameter can take one of three forms.
345
346       A filename
347            If the $input parameter is a scalar, it is assumed to be a
348            filename. This file will be opened for reading and the compressed
349            data will be read from it.
350
351       A filehandle
352            If the $input parameter is a filehandle, the compressed data will
353            be read from it.  The string '-' can be used as an alias for
354            standard input.
355
356       A scalar reference
357            If $input is a scalar reference, the compressed data will be read
358            from $$input.
359
360   Constructor Options
361       The option names defined below are case insensitive and can be
362       optionally prefixed by a '-'.  So all of the following are valid
363
364           -AutoClose
365           -autoclose
366           AUTOCLOSE
367           autoclose
368
369       OPTS is a combination of the following options:
370
371       "AutoClose => 0|1"
372            This option is only valid when the $input parameter is a
373            filehandle. If specified, and the value is true, it will result in
374            the file being closed once either the "close" method is called or
375            the IO::Uncompress::UnLzip object is destroyed.
376
377            This parameter defaults to 0.
378
379       "MultiStream => 0|1"
380            Allows multiple concatenated compressed streams to be treated as a
381            single compressed stream. Decompression will stop once either the
382            end of the file/buffer is reached, an error is encountered
383            (premature eof, corrupt compressed data) or the end of a stream is
384            not immediately followed by the start of another stream.
385
386            This parameter defaults to 0.
387
388       "Prime => $string"
389            This option will uncompress the contents of $string before
390            processing the input file/buffer.
391
392            This option can be useful when the compressed data is embedded in
393            another file/data structure and it is not possible to work out
394            where the compressed data begins without having to read the first
395            few bytes. If this is the case, the uncompression can be primed
396            with these bytes using this option.
397
398       "Transparent => 0|1"
399            If this option is set and the input file/buffer is not compressed
400            data, the module will allow reading of it anyway.
401
402            In addition, if the input file/buffer does contain compressed data
403            and there is non-compressed data immediately following it, setting
404            this option will make this module treat the whole file/buffer as a
405            single data stream.
406
407            This option defaults to 1.
408
409       "BlockSize => $num"
410            When reading the compressed input data, IO::Uncompress::UnLzip
411            will read it in blocks of $num bytes.
412
413            This option defaults to 4096.
414
415       "InputLength => $size"
416            When present this option will limit the number of compressed bytes
417            read from the input file/buffer to $size. This option can be used
418            in the situation where there is useful data directly after the
419            compressed data stream and you know beforehand the exact length of
420            the compressed data stream.
421
422            This option is mostly used when reading from a filehandle, in
423            which case the file pointer will be left pointing to the first
424            byte directly after the compressed data stream.
425
426            This option defaults to off.
427
428       "Append => 0|1"
429            This option controls what the "read" method does with uncompressed
430            data.
431
432            If set to 1, all uncompressed data will be appended to the output
433            parameter of the "read" method.
434
435            If set to 0, the contents of the output parameter of the "read"
436            method will be overwritten by the uncompressed data.
437
438            Defaults to 0.
439
440       "Strict => 0|1"
441            This option controls whether the extra checks defined below are
442            used when carrying out the decompression. When Strict is on, the
443            extra tests are carried out, when Strict is off they are not.
444
445            The default for this option is off.
446

Methods

448   read
449       Usage is
450
451           $status = $z->read($buffer)
452
453       Reads a block of compressed data (the size of the compressed block is
454       determined by the "Buffer" option in the constructor), uncompresses it
455       and writes any uncompressed data into $buffer. If the "Append"
456       parameter is set in the constructor, the uncompressed data will be
457       appended to the $buffer parameter. Otherwise $buffer will be
458       overwritten.
459
460       Returns the number of uncompressed bytes written to $buffer, zero if
461       eof or a negative number on error.
462
463   read
464       Usage is
465
466           $status = $z->read($buffer, $length)
467           $status = $z->read($buffer, $length, $offset)
468
469           $status = read($z, $buffer, $length)
470           $status = read($z, $buffer, $length, $offset)
471
472       Attempt to read $length bytes of uncompressed data into $buffer.
473
474       The main difference between this form of the "read" method and the
475       previous one, is that this one will attempt to return exactly $length
476       bytes. The only circumstances that this function will not is if end-of-
477       file or an IO error is encountered.
478
479       Returns the number of uncompressed bytes written to $buffer, zero if
480       eof or a negative number on error.
481
482   getline
483       Usage is
484
485           $line = $z->getline()
486           $line = <$z>
487
488       Reads a single line.
489
490       This method fully supports the use of the variable $/ (or
491       $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
492       what constitutes an end of line. Paragraph mode, record mode and file
493       slurp mode are all supported.
494
495   getc
496       Usage is
497
498           $char = $z->getc()
499
500       Read a single character.
501
502   ungetc
503       Usage is
504
505           $char = $z->ungetc($string)
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 uncompressed 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 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::UnLzip 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::UnLzip 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

Importing

669       No symbolic constants are required by IO::Uncompress::UnLzip at
670       present.
671
672       :all Imports "unlzip" and $UnLzipError.  Same as doing this
673
674                use IO::Uncompress::UnLzip qw(unlzip $UnLzipError) ;
675

EXAMPLES

SUPPORT

678       General feedback/questions/bug reports should be sent to
679       <https://github.com/pmqs/IO-Compress-Lzma/issues> (preferred) or
680       <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Lzma>.
681

SEE ALSO

683       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
684       IO::Compress::Deflate, IO::Uncompress::Inflate,
685       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
686       IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
687       IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
688       IO::Compress::Lzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
689       IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
690       IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
691       IO::Uncompress::AnyUncompress
692
693       IO::Compress::FAQ
694
695       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
696

AUTHOR

698       This module was written by Paul Marquess, "pmqs@cpan.org".
699

MODIFICATION HISTORY

701       See the Changes file.
702
704       Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
705
706       This program is free software; you can redistribute it and/or modify it
707       under the same terms as Perl itself.
708
709
710
711perl v5.38.0                      2023-07-26         IO::Uncompress::UnLzip(3)
Impressum