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 OneShot 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 The constructor takes one mandatory parameter, $input, defined below,
313 and zero or more "OPTS", defined in "Constructor Options".
314
315 Returns an "IO::Uncompress::UnXz" object on success and undef on
316 failure. The variable $UnXzError will contain an error message on
317 failure.
318
319 If you are running Perl 5.005 or better the object, $z, returned from
320 IO::Uncompress::UnXz can be used exactly like an IO::File filehandle.
321 This means that all normal input file operations can be carried out
322 with $z. For example, to read a line from a compressed file/buffer you
323 can use either of these forms
324
325 $line = $z->getline();
326 $line = <$z>;
327
328 Below is a simple exaple of using the OO interface to read the
329 compressed file "myfile.xz" and write its contents to stdout.
330
331 my $filename = "myfile.xz";
332 my $z = IO::Uncompress::UnXz->new($filename)
333 or die "IO::Uncompress::UnXz failed: $UnXzError\n";
334
335 while (<$z>) {
336 print $_;
337 }
338 $z->close();
339
340 See "EXAMPLES" for further examples
341
342 The mandatory parameter $input is used to determine the source of the
343 compressed data. This parameter can take one of three forms.
344
345 A filename
346 If the $input parameter is a scalar, it is assumed to be a
347 filename. This file will be opened for reading and the compressed
348 data will be read from it.
349
350 A filehandle
351 If the $input parameter is a filehandle, the compressed data will
352 be read from it. The string '-' can be used as an alias for
353 standard input.
354
355 A scalar reference
356 If $input is a scalar reference, the compressed data will be read
357 from $$input.
358
359 Constructor Options
360 The option names defined below are case insensitive and can be
361 optionally prefixed by a '-'. So all of the following are valid
362
363 -AutoClose
364 -autoclose
365 AUTOCLOSE
366 autoclose
367
368 OPTS is a combination of the following options:
369
370 "AutoClose => 0|1"
371 This option is only valid when the $input parameter is a
372 filehandle. If specified, and the value is true, it will result in
373 the file being closed once either the "close" method is called or
374 the IO::Uncompress::UnXz object is destroyed.
375
376 This parameter defaults to 0.
377
378 "MultiStream => 0|1"
379 Allows multiple concatenated compressed streams to be treated as a
380 single compressed stream. Decompression will stop once either the
381 end of the file/buffer is reached, an error is encountered
382 (premature eof, corrupt compressed data) or the end of a stream is
383 not immediately followed by the start of another stream.
384
385 This parameter defaults to 0.
386
387 "Prime => $string"
388 This option will uncompress the contents of $string before
389 processing the input file/buffer.
390
391 This option can be useful when the compressed data is embedded in
392 another file/data structure and it is not possible to work out
393 where the compressed data begins without having to read the first
394 few bytes. If this is the case, the uncompression can be primed
395 with these bytes using this option.
396
397 "Transparent => 0|1"
398 If this option is set and the input file/buffer is not compressed
399 data, the module will allow reading of it anyway.
400
401 In addition, if the input file/buffer does contain compressed data
402 and there is non-compressed data immediately following it, setting
403 this option will make this module treat the whole file/buffer as a
404 single data stream.
405
406 This option defaults to 1.
407
408 "BlockSize => $num"
409 When reading the compressed input data, IO::Uncompress::UnXz will
410 read it in blocks of $num bytes.
411
412 This option defaults to 4096.
413
414 "InputLength => $size"
415 When present this option will limit the number of compressed bytes
416 read from the input file/buffer to $size. This option can be used
417 in the situation where there is useful data directly after the
418 compressed data stream and you know beforehand the exact length of
419 the compressed data stream.
420
421 This option is mostly used when reading from a filehandle, in
422 which case the file pointer will be left pointing to the first
423 byte directly after the compressed data stream.
424
425 This option defaults to off.
426
427 "Append => 0|1"
428 This option controls what the "read" method does with uncompressed
429 data.
430
431 If set to 1, all uncompressed data will be appended to the output
432 parameter of the "read" method.
433
434 If set to 0, the contents of the output parameter of the "read"
435 method will be overwritten by the uncompressed data.
436
437 Defaults to 0.
438
439 "Strict => 0|1"
440 This option controls whether the extra checks defined below are
441 used when carrying out the decompression. When Strict is on, the
442 extra tests are carried out, when Strict is off they are not.
443
444 The default for this option is off.
445
446 "MemLimit => $number"
447 Default is 128Meg.
448
449 "Flags => $flags"
450 Default is 0.
451
453 read
454 Usage is
455
456 $status = $z->read($buffer)
457
458 Reads a block of compressed data (the size of the compressed block is
459 determined by the "Buffer" option in the constructor), uncompresses it
460 and writes any uncompressed data into $buffer. If the "Append"
461 parameter is set in the constructor, the uncompressed data will be
462 appended to the $buffer parameter. Otherwise $buffer will be
463 overwritten.
464
465 Returns the number of uncompressed bytes written to $buffer, zero if
466 eof or a negative number on error.
467
468 read
469 Usage is
470
471 $status = $z->read($buffer, $length)
472 $status = $z->read($buffer, $length, $offset)
473
474 $status = read($z, $buffer, $length)
475 $status = read($z, $buffer, $length, $offset)
476
477 Attempt to read $length bytes of uncompressed data into $buffer.
478
479 The main difference between this form of the "read" method and the
480 previous one, is that this one will attempt to return exactly $length
481 bytes. The only circumstances that this function will not is if end-of-
482 file or an IO error is encountered.
483
484 Returns the number of uncompressed bytes written to $buffer, zero if
485 eof or a negative number on error.
486
487 getline
488 Usage is
489
490 $line = $z->getline()
491 $line = <$z>
492
493 Reads a single line.
494
495 This method fully supports the use of the variable $/ (or
496 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
497 what constitutes an end of line. Paragraph mode, record mode and file
498 slurp mode are all supported.
499
500 getc
501 Usage is
502
503 $char = $z->getc()
504
505 Read a single character.
506
507 ungetc
508 Usage is
509
510 $char = $z->ungetc($string)
511
512 getHeaderInfo
513 Usage is
514
515 $hdr = $z->getHeaderInfo();
516 @hdrs = $z->getHeaderInfo();
517
518 This method returns either a hash reference (in scalar context) or a
519 list or hash references (in array context) that contains information
520 about each of the header fields in the compressed data stream(s).
521
522 tell
523 Usage is
524
525 $z->tell()
526 tell $z
527
528 Returns the uncompressed file offset.
529
530 eof
531 Usage is
532
533 $z->eof();
534 eof($z);
535
536 Returns true if the end of the compressed input stream has been
537 reached.
538
539 seek
540 $z->seek($position, $whence);
541 seek($z, $position, $whence);
542
543 Provides a sub-set of the "seek" functionality, with the restriction
544 that it is only legal to seek forward in the input file/buffer. It is
545 a fatal error to attempt to seek backward.
546
547 Note that the implementation of "seek" in this module does not provide
548 true random access to a compressed file/buffer. It works by
549 uncompressing data from the current offset in the file/buffer until it
550 reaches the uncompressed offset specified in the parameters to "seek".
551 For very small files this may be acceptable behaviour. For large files
552 it may cause an unacceptable delay.
553
554 The $whence parameter takes one the usual values, namely SEEK_SET,
555 SEEK_CUR or SEEK_END.
556
557 Returns 1 on success, 0 on failure.
558
559 binmode
560 Usage is
561
562 $z->binmode
563 binmode $z ;
564
565 This is a noop provided for completeness.
566
567 opened
568 $z->opened()
569
570 Returns true if the object currently refers to a opened file/buffer.
571
572 autoflush
573 my $prev = $z->autoflush()
574 my $prev = $z->autoflush(EXPR)
575
576 If the $z object is associated with a file or a filehandle, this method
577 returns the current autoflush setting for the underlying filehandle. If
578 "EXPR" is present, and is non-zero, it will enable flushing after every
579 write/print operation.
580
581 If $z is associated with a buffer, this method has no effect and always
582 returns "undef".
583
584 Note that the special variable $| cannot be used to set or retrieve the
585 autoflush setting.
586
587 input_line_number
588 $z->input_line_number()
589 $z->input_line_number(EXPR)
590
591 Returns the current uncompressed line number. If "EXPR" is present it
592 has the effect of setting the line number. Note that setting the line
593 number does not change the current position within the file/buffer
594 being read.
595
596 The contents of $/ are used to determine what constitutes a line
597 terminator.
598
599 fileno
600 $z->fileno()
601 fileno($z)
602
603 If the $z object is associated with a file or a filehandle, "fileno"
604 will return the underlying file descriptor. Once the "close" method is
605 called "fileno" will return "undef".
606
607 If the $z object is associated with a buffer, this method will return
608 "undef".
609
610 close
611 $z->close() ;
612 close $z ;
613
614 Closes the output file/buffer.
615
616 For most versions of Perl this method will be automatically invoked if
617 the IO::Uncompress::UnXz object is destroyed (either explicitly or by
618 the variable with the reference to the object going out of scope). The
619 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
620 cases, the "close" method will be called automatically, but not until
621 global destruction of all live objects when the program is terminating.
622
623 Therefore, if you want your scripts to be able to run on all versions
624 of Perl, you should call "close" explicitly and not rely on automatic
625 closing.
626
627 Returns true on success, otherwise 0.
628
629 If the "AutoClose" option has been enabled when the
630 IO::Uncompress::UnXz object was created, and the object is associated
631 with a file, the underlying file will also be closed.
632
633 nextStream
634 Usage is
635
636 my $status = $z->nextStream();
637
638 Skips to the next compressed data stream in the input file/buffer. If a
639 new compressed data stream is found, the eof marker will be cleared and
640 $. will be reset to 0.
641
642 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
643 error was encountered.
644
645 trailingData
646 Usage is
647
648 my $data = $z->trailingData();
649
650 Returns the data, if any, that is present immediately after the
651 compressed data stream once uncompression is complete. It only makes
652 sense to call this method once the end of the compressed data stream
653 has been encountered.
654
655 This option can be used when there is useful information immediately
656 following the compressed data stream, and you don't know the length of
657 the compressed data stream.
658
659 If the input is a buffer, "trailingData" will return everything from
660 the end of the compressed data stream to the end of the buffer.
661
662 If the input is a filehandle, "trailingData" will return the data that
663 is left in the filehandle input buffer once the end of the compressed
664 data stream has been reached. You can then use the filehandle to read
665 the rest of the input file.
666
667 Don't bother using "trailingData" if the input is a filename.
668
669 If you know the length of the compressed data stream before you start
670 uncompressing, you can avoid having to use "trailingData" by setting
671 the "InputLength" option in the constructor.
672
674 No symbolic constants are required by IO::Uncompress::UnXz at present.
675
676 :all Imports "unxz" and $UnXzError. Same as doing this
677
678 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
679
682 General feedback/questions/bug reports should be sent to
683 <https://github.com/pmqs/IO-Compress-Lzma/issues> (preferred) or
684 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Lzma>.
685
687 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
688 IO::Compress::Deflate, IO::Uncompress::Inflate,
689 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
690 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
691 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Compress::Lzip,
692 IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
693 IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
694 IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
695 IO::Uncompress::AnyUncompress
696
697 IO::Compress::FAQ
698
699 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
700
702 This module was written by Paul Marquess, "pmqs@cpan.org".
703
705 See the Changes file.
706
708 Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
709
710 This program is free software; you can redistribute it and/or modify it
711 under the same terms as Perl itself.
712
713
714
715perl v5.38.0 2023-07-26 IO::Uncompress::UnXz(3)