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