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