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.
82
83 The $input_filename_or_reference parameter
84
85 The parameter, $input_filename_or_reference, is used to define the
86 source of the compressed data.
87
88 It can take one of the following forms:
89
90 A filename
91 If the <$input_filename_or_reference> parameter is a simple
92 scalar, it is assumed to be a filename. This file will be opened
93 for reading and the input data will be read from it.
94
95 A filehandle
96 If the $input_filename_or_reference parameter is a filehandle, the
97 input data will be read from it. The string '-' can be used as an
98 alias for standard input.
99
100 A scalar reference
101 If $input_filename_or_reference is a scalar reference, the input
102 data will be read from $$input_filename_or_reference.
103
104 An array reference
105 If $input_filename_or_reference is an array reference, each
106 element in the array must be a filename.
107
108 The input data will be read from each file in turn.
109
110 The complete array will be walked to ensure that it only contains
111 valid filenames before any data is uncompressed.
112
113 An Input FileGlob string
114 If $input_filename_or_reference is a string that is delimited by
115 the characters "<" and ">" "unxz" will assume that it is an input
116 fileglob string. The input is the list of files that match the
117 fileglob.
118
119 See File::GlobMapper for more details.
120
121 If the $input_filename_or_reference parameter is any other type,
122 "undef" will be returned.
123
124 The $output_filename_or_reference parameter
125
126 The parameter $output_filename_or_reference is used to control the
127 destination of the uncompressed data. This parameter can take one of
128 these forms.
129
130 A filename
131 If the $output_filename_or_reference parameter is a simple scalar,
132 it is assumed to be a filename. This file will be opened for
133 writing and the uncompressed data will be written to it.
134
135 A filehandle
136 If the $output_filename_or_reference parameter is a filehandle,
137 the uncompressed data will be written to it. The string '-' can
138 be used as an alias for standard output.
139
140 A scalar reference
141 If $output_filename_or_reference is a scalar reference, the
142 uncompressed data will be stored in
143 $$output_filename_or_reference.
144
145 An Array Reference
146 If $output_filename_or_reference is an array reference, the
147 uncompressed data will be pushed onto the array.
148
149 An Output FileGlob
150 If $output_filename_or_reference is a string that is delimited by
151 the characters "<" and ">" "unxz" will assume that it is an output
152 fileglob string. The output is the list of files that match the
153 fileglob.
154
155 When $output_filename_or_reference is an fileglob string,
156 $input_filename_or_reference must also be a fileglob string.
157 Anything else is an error.
158
159 See File::GlobMapper for more details.
160
161 If the $output_filename_or_reference parameter is any other type,
162 "undef" will be returned.
163
164 Notes
165 When $input_filename_or_reference maps to multiple compressed
166 files/buffers and $output_filename_or_reference is a single
167 file/buffer, after uncompression $output_filename_or_reference will
168 contain a concatenation of all the uncompressed data from each of the
169 input files/buffers.
170
171 Optional Parameters
172 Unless specified below, the optional parameters for "unxz", "OPTS", are
173 the same as those used with the OO interface defined in the
174 "Constructor Options" section below.
175
176 "AutoClose => 0|1"
177 This option applies to any input or output data streams to "unxz"
178 that are filehandles.
179
180 If "AutoClose" is specified, and the value is true, it will result
181 in all input and/or output filehandles being closed once "unxz"
182 has completed.
183
184 This parameter defaults to 0.
185
186 "BinModeOut => 0|1"
187 This option is now a no-op. All files will be written in binmode.
188
189 "Append => 0|1"
190 The behaviour of this option is dependent on the type of output
191 data stream.
192
193 · A Buffer
194
195 If "Append" is enabled, all uncompressed data will be append
196 to the end of the output buffer. Otherwise the output buffer
197 will be cleared before any uncompressed data is written to
198 it.
199
200 · A Filename
201
202 If "Append" is enabled, the file will be opened in append
203 mode. Otherwise the contents of the file, if any, will be
204 truncated before any uncompressed data is written to it.
205
206 · A Filehandle
207
208 If "Append" is enabled, the filehandle will be positioned to
209 the end of the file via a call to "seek" before any
210 uncompressed data is written to it. Otherwise the file
211 pointer will not be moved.
212
213 When "Append" is specified, and set to true, it will append all
214 uncompressed data to the output data stream.
215
216 So when the output is a filehandle it will carry out a seek to the
217 eof before writing any uncompressed data. If the output is a
218 filename, it will be opened for appending. If the output is a
219 buffer, all uncompressed data will be appended to the existing
220 buffer.
221
222 Conversely when "Append" is not specified, or it is present and is
223 set to false, it will operate as follows.
224
225 When the output is a filename, it will truncate the contents of
226 the file before writing any uncompressed data. If the output is a
227 filehandle its position will not be changed. If the output is a
228 buffer, it will be wiped before any uncompressed data is output.
229
230 Defaults to 0.
231
232 "MultiStream => 0|1"
233 If the input file/buffer contains multiple compressed data
234 streams, this option will uncompress the whole lot as a single
235 data stream.
236
237 Defaults to 0.
238
239 "TrailingData => $scalar"
240 Returns the data, if any, that is present immediately after the
241 compressed data stream once uncompression is complete.
242
243 This option can be used when there is useful information
244 immediately following the compressed data stream, and you don't
245 know the length of the compressed data stream.
246
247 If the input is a buffer, "trailingData" will return everything
248 from the end of the compressed data stream to the end of the
249 buffer.
250
251 If the input is a filehandle, "trailingData" will return the data
252 that is left in the filehandle input buffer once the end of the
253 compressed data stream has been reached. You can then use the
254 filehandle to read the rest of the input file.
255
256 Don't bother using "trailingData" if the input is a filename.
257
258 If you know the length of the compressed data stream before you
259 start uncompressing, you can avoid having to use "trailingData" by
260 setting the "InputLength" option.
261
262 Examples
263 To read the contents of the file "file1.txt.xz" and write the
264 uncompressed data to the file "file1.txt".
265
266 use strict ;
267 use warnings ;
268 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
269
270 my $input = "file1.txt.xz";
271 my $output = "file1.txt";
272 unxz $input => $output
273 or die "unxz failed: $UnXzError\n";
274
275 To read from an existing Perl filehandle, $input, and write the
276 uncompressed data to a buffer, $buffer.
277
278 use strict ;
279 use warnings ;
280 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
281 use IO::File ;
282
283 my $input = new IO::File "<file1.txt.xz"
284 or die "Cannot open 'file1.txt.xz': $!\n" ;
285 my $buffer ;
286 unxz $input => \$buffer
287 or die "unxz failed: $UnXzError\n";
288
289 To uncompress all files in the directory "/my/home" that match
290 "*.txt.xz" and store the compressed data in the same directory
291
292 use strict ;
293 use warnings ;
294 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
295
296 unxz '</my/home/*.txt.xz>' => '</my/home/#1.txt>'
297 or die "unxz failed: $UnXzError\n";
298
299 and if you want to compress each file one at a time, this will do the
300 trick
301
302 use strict ;
303 use warnings ;
304 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
305
306 for my $input ( glob "/my/home/*.txt.xz" )
307 {
308 my $output = $input;
309 $output =~ s/.xz// ;
310 unxz $input => $output
311 or die "Error compressing '$input': $UnXzError\n";
312 }
313
315 Constructor
316 The format of the constructor for IO::Uncompress::UnXz is shown below
317
318 my $z = new IO::Uncompress::UnXz $input [OPTS]
319 or die "IO::Uncompress::UnXz failed: $UnXzError\n";
320
321 Returns an "IO::Uncompress::UnXz" object on success and undef on
322 failure. The variable $UnXzError will contain an error message on
323 failure.
324
325 If you are running Perl 5.005 or better the object, $z, returned from
326 IO::Uncompress::UnXz can be used exactly like an IO::File filehandle.
327 This means that all normal input file operations can be carried out
328 with $z. For example, to read a line from a compressed file/buffer you
329 can use either of these forms
330
331 $line = $z->getline();
332 $line = <$z>;
333
334 The mandatory parameter $input is used to determine the source of the
335 compressed data. This parameter can take one of three forms.
336
337 A filename
338 If the $input parameter is a scalar, it is assumed to be a
339 filename. This file will be opened for reading and the compressed
340 data will be read from it.
341
342 A filehandle
343 If the $input parameter is a filehandle, the compressed data will
344 be read from it. The string '-' can be used as an alias for
345 standard input.
346
347 A scalar reference
348 If $input is a scalar reference, the compressed data will be read
349 from $$input.
350
351 Constructor Options
352 The option names defined below are case insensitive and can be
353 optionally prefixed by a '-'. So all of the following are valid
354
355 -AutoClose
356 -autoclose
357 AUTOCLOSE
358 autoclose
359
360 OPTS is a combination of the following options:
361
362 "AutoClose => 0|1"
363 This option is only valid when the $input parameter is a
364 filehandle. If specified, and the value is true, it will result in
365 the file being closed once either the "close" method is called or
366 the IO::Uncompress::UnXz object is destroyed.
367
368 This parameter defaults to 0.
369
370 "MultiStream => 0|1"
371 Allows multiple concatenated compressed streams to be treated as a
372 single compressed stream. Decompression will stop once either the
373 end of the file/buffer is reached, an error is encountered
374 (premature eof, corrupt compressed data) or the end of a stream is
375 not immediately followed by the start of another stream.
376
377 This parameter defaults to 0.
378
379 "Prime => $string"
380 This option will uncompress the contents of $string before
381 processing the input file/buffer.
382
383 This option can be useful when the compressed data is embedded in
384 another file/data structure and it is not possible to work out
385 where the compressed data begins without having to read the first
386 few bytes. If this is the case, the uncompression can be primed
387 with these bytes using this option.
388
389 "Transparent => 0|1"
390 If this option is set and the input file/buffer is not compressed
391 data, the module will allow reading of it anyway.
392
393 In addition, if the input file/buffer does contain compressed data
394 and there is non-compressed data immediately following it, setting
395 this option will make this module treat the whole file/buffer as a
396 single data stream.
397
398 This option defaults to 1.
399
400 "BlockSize => $num"
401 When reading the compressed input data, IO::Uncompress::UnXz will
402 read it in blocks of $num bytes.
403
404 This option defaults to 4096.
405
406 "InputLength => $size"
407 When present this option will limit the number of compressed bytes
408 read from the input file/buffer to $size. This option can be used
409 in the situation where there is useful data directly after the
410 compressed data stream and you know beforehand the exact length of
411 the compressed data stream.
412
413 This option is mostly used when reading from a filehandle, in
414 which case the file pointer will be left pointing to the first
415 byte directly after the compressed data stream.
416
417 This option defaults to off.
418
419 "Append => 0|1"
420 This option controls what the "read" method does with uncompressed
421 data.
422
423 If set to 1, all uncompressed data will be appended to the output
424 parameter of the "read" method.
425
426 If set to 0, the contents of the output parameter of the "read"
427 method will be overwritten by the uncompressed data.
428
429 Defaults to 0.
430
431 "Strict => 0|1"
432 This option controls whether the extra checks defined below are
433 used when carrying out the decompression. When Strict is on, the
434 extra tests are carried out, when Strict is off they are not.
435
436 The default for this option is off.
437
438 "MemLimit => $number"
439 Default is 128Meg.
440
441 "Flags => $flags"
442 Default is 0.
443
444 Examples
445 TODO
446
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::UnXz 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::UnXz 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
669 No symbolic constants are required by this IO::Uncompress::UnXz at
670 present.
671
672 :all Imports "unxz" and $UnXzError. Same as doing this
673
674 use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
675
678 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
679 IO::Compress::Deflate, IO::Uncompress::Inflate,
680 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
681 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
682 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Compress::Lzip,
683 IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
684 IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
685 IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
686 IO::Uncompress::AnyUncompress
687
688 IO::Compress::FAQ
689
690 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
691
693 This module was written by Paul Marquess, "pmqs@cpan.org".
694
696 See the Changes file.
697
699 Copyright (c) 2005-2019 Paul Marquess. All rights reserved.
700
701 This program is free software; you can redistribute it and/or modify it
702 under the same terms as Perl itself.
703
704
705
706perl v5.28.1 2019-01-05 IO::Uncompress::UnXz(3)