IO::Unc...s::UnXz(3)
β¨ NAME β¨
IO::Uncompress::UnXz β bestie, this one reads xz files/buffers. That's it, that's the whole vibe π
(this page is from the IO::Unc...s::UnXz(3) corner of the User Contributed Perl Documentation, iykyk)
πΈ SYNOPSIS πΈ
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
my $status = unxz $input => $output [,OPTS]
or die "unxz failed: $UnXzError\n";
my $z = IO::Uncompress::UnXz->new( $input [OPTS] )
or die "unxz failed: $UnXzError\n";
$status = $z->read($buffer)
$status = $z->read($buffer, $length)
$status = $z->read($buffer, $length, $offset)
$line = $z->getline()
$char = $z->getc()
$char = $z->ungetc()
$char = $z->opened()
$data = $z->trailingData()
$status = $z->nextStream()
$data = $z->getHeaderInfo()
$z->tell()
$z->seek($position, $whence)
$z->binmode()
$z->fileno()
$z->eof()
$z->close()
$UnXzError ;
# IO::File mode
<$z>
read($z, $buffer);
read($z, $buffer, $length);
read($z, $buffer, $length, $offset);
tell($z)
seek($z, $position, $whence)
binmode($z)
fileno($z)
eof($z)
close($z)
π DESCRIPTION π
So this module hands you a Perl interface that lets you read lzma files/buffers. That's the whole job, and it does it gorgeously. π
Now β for writing xz files/buffers, you do NOT come here, you go hang out with its companion module IO::Compress::Xz. Different bestie, different job. π
π¦ Functional Interface π¦
There's a top-level function, unxz, and it's giving "one-shot" uncompression between buffers and/or files. Like, you hand it a thing, it hands you the uncompressed thing, no small talk. If you want finer control over the whole uncompression process, go read the "OO Interface" section instead, bestie.
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
unxz $input_filename_or_reference => $output_filename_or_reference [,OPTS]
or die "unxz failed: $UnXzError\n";
Heads up: the functional interface needs Perl5.005 or better. No dusty Perls allowed at this party. π«Ά
π unxz $input_filename_or_reference => $output_filename_or_reference [, OPTS] π
unxz expects at least two parameters, $input_filename_or_reference and $output_filename_or_reference, plus zero or more optional parameters (peep "Optional Parameters" for those).
The $input_filename_or_reference parameter
This parameter is the whole source of the compressed data. It can show up dressed as any of these:
- A filename β if
$input_filename_or_referenceis a simple scalar, it gets treated as a filename. That file gets opened for reading and the input data gets read out of it. Classic. π - A filehandle β if
$input_filename_or_referenceis a filehandle, the input data gets read from it. And the string'-'is usable as an alias for standard input. Cute, right? - A scalar reference β if
$input_filename_or_referenceis a scalar reference, the input data gets read from$$input_filename_or_reference. - An array reference β if
$input_filename_or_referenceis an array reference, each element in the array must be a filename. The input data gets read from each file in turn. And the complete array gets walked first to make sure it only contains valid filenames before any data is uncompressed. It's checking the guest list before the party starts, very responsible. β¨ - An Input FileGlob string β if
$input_filename_or_referenceis a string delimited by the characters"<"and">",unxzassumes it's an input fileglob string. The input is the list of files that match the fileglob. SeeFile::GlobMapperfor more details.
And if $input_filename_or_reference is any other type? You get undef back. That's it. No notes. π
The $output_filename_or_reference parameter
This one controls the destination of the uncompressed data. It can take one of these forms:
- A filename β if
$output_filename_or_referenceis a simple scalar, it gets treated as a filename. That file gets opened for writing and the uncompressed data gets written into it. - A filehandle β if
$output_filename_or_referenceis a filehandle, the uncompressed data gets written to it. The string'-'can be used as an alias for standard output. - A scalar reference β if
$output_filename_or_referenceis a scalar reference, the uncompressed data gets stored in$$output_filename_or_reference. - An Array Reference β if
$output_filename_or_referenceis an array reference, the uncompressed data gets pushed onto the array. π₯ - An Output FileGlob β if
$output_filename_or_referenceis a string delimited by the characters"<"and">",unxzassumes it's an output fileglob string. The output is the list of files that match the fileglob.
β οΈ Real talk though: when$output_filename_or_referenceis a fileglob string,$input_filename_or_referencemust also be a fileglob string. Anything else is an error. Periodt.
See File::GlobMapper for more details.
And if $output_filename_or_reference is any other type, undef gets returned. Again. Consistency is iconic. π
π Notes π
When $input_filename_or_reference maps to multiple compressed files/buffers and $output_filename_or_reference is a single file/buffer, then after uncompression, $output_filename_or_reference will contain a concatenation of all the uncompressed data from each of the input files/buffers. Like a group chat where everyone's messages just keep stacking up. π«Ά
π Optional Parameters π
The optional parameters for the one-shot function unxz are (for the most part) identical to the ones used with the OO interface defined over in "Constructor Options". The exceptions are listed below:
AutoClose => 0|1β this option applies to any input or output data streams handed tounxzthat are filehandles.
If AutoClose is specified and the value is true, it results in all input and/or output filehandles being closed once unxz has completed. Cleanup on aisle you. π§Ή
This parameter defaults to 0.
BinModeOut => 0|1β this option is now a no-op. All files will be written in binmode. It's retired, it's living its best life, let it rest. πAppend => 0|1β the behaviour of this option depends on the type of output data stream. Let's break it down:- A Buffer β if
Appendis enabled, all uncompressed data gets appended to the end of the output buffer. Otherwise the output buffer gets cleared before any uncompressed data is written to it. - A Filename β if
Appendis enabled, the file gets opened in append mode. Otherwise the contents of the file, if any, get truncated before any uncompressed data is written to it. - A Filehandle β if
Appendis enabled, the filehandle gets positioned to the end of the file via a call toseekbefore any uncompressed data is written to it. Otherwise the file pointer does not move.
- A Buffer β if
When Append is specified and set to true, it appends all uncompressed data to the output data stream.
So: when the output is a filehandle, it carries out a seek to the eof before writing any uncompressed data. If the output is a filename, it gets opened for appending. If the output is a buffer, all uncompressed data gets appended to the existing buffer. Additive queen. π
Conversely, when Append is not specified, or it's present and set to false, it operates like this:
When the output is a filename, it truncates the contents of the file before writing any uncompressed data. If the output is a filehandle, its position is not changed. If the output is a buffer, it gets wiped before any uncompressed data is output. Fresh start energy. β¨
Defaults to 0.
MultiStream => 0|1β if the input file/buffer contains multiple compressed data streams, this option uncompresses the whole lot as a single data stream.
Defaults to 0.
TrailingData => $scalarβ returns the data, if any, that's sitting right there immediately after the compressed data stream once uncompression is complete.
This option is your girl when there's useful info immediately following the compressed data stream and you don't know the length of the compressed data stream.
If the input is a buffer, trailingData returns everything from the end of the compressed data stream to the end of the buffer.
If the input is a filehandle, trailingData returns the data that's left in the filehandle input buffer once the end of the compressed data stream has been reached. You can then use the filehandle to read the rest of the input file. Handy. π
Don't bother using trailingData if the input is a filename. Just don't. π
And if you already know the length of the compressed data stream before you start uncompressing, you can dodge the whole trailingData situation by setting the InputLength option instead. Foresight is a look. π
π OneShot Examples π
Wanna read the contents of the file file1.txt.xz and write the uncompressed data to the file file1.txt? Say less:
use strict ;
use warnings ;
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
my $input = "file1.txt.xz";
my $output = "file1.txt";
unxz $input => $output
or die "unxz failed: $UnXzError\n";
Wanna read from an existing Perl filehandle, $input, and write the uncompressed data to a buffer, $buffer? Also say less:
use strict ;
use warnings ;
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
use IO::File ;
my $input = IO::File->new( "<file1.txt.xz" )
or die "Cannot open 'file1.txt.xz': $!\n" ;
my $buffer ;
unxz $input => \$buffer
or die "unxz failed: $UnXzError\n";
Wanna uncompress all files in the directory /my/home that match *.txt.xz and store the compressed data in the same directory? Glob girlies rise:
use strict ;
use warnings ;
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
unxz '</my/home/*.txt.xz>' => '</my/home/#1.txt>'
or die "unxz failed: $UnXzError\n";
And if you wanna compress each file one at a time, this does the trick:
use strict ;
use warnings ;
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
for my $input ( glob "/my/home/*.txt.xz" )
{
my $output = $input;
$output =~ s/.xz// ;
unxz $input => $output
or die "Error compressing '$input': $UnXzError\n";
}
π OO Interface
Okay bestie, this is where it gets good. If you like your objects cute and your filehandles even cuter, this is the interface for you. π«Ά
π Constructor
The format of the constructor for IO::Uncompress::UnXz is shown below:
my $z = IO::Uncompress::UnXz->new( $input [OPTS] )
or die "IO::Uncompress::UnXz failed: $UnXzError\n";
So here's the tea: the constructor takes one mandatory parameter, $input (defined below, keep reading π), plus zero or more OPTS, which are all defined in "Constructor Options".
It hands you back an IO::Uncompress::UnXz object if everything slays, and undef if it flops. When it flops, the variable $UnXzError will be holding the error message for you. β¨
If you're running Perl 5.005 or better β and honestly, who isn't β the object $z that comes back from IO::Uncompress::UnXz can be used exactly like an IO::File filehandle. That means every normal input file operation works on $z. For example, to read a line from a compressed file/buffer you can use either of these forms:
$line = $z->getline();
$line = <$z>;
Below is a simple example of using the OO interface to read the compressed file myfile.xz and write its contents to stdout. It's giving hello world but make it compression. π
my $filename = "myfile.xz";
my $z = IO::Uncompress::UnXz->new($filename)
or die "IO::Uncompress::UnXz failed: $UnXzError\n";
while (<$z>) {
print $_;
}
$z->close();
See "EXAMPLES" for further examples. π
The mandatory parameter $input is used to determine the source of the compressed data. This parameter can take one of three forms. Three! A little trio. π
A filename
If the $input parameter is a scalar, it is assumed to be a filename. This file will be opened for reading and the compressed data will be read from it. π
A filehandle
If the $input parameter is a filehandle, the compressed data will be read from it. The string '-' can be used as an alias for standard input. Yes, the classic little dash. π
A scalar reference
If $input is a scalar reference, the compressed data will be read from $$input. π¦
β¨ Constructor Options β¨
Okay so before we get into the list: the option names defined below are case insensitive and can be optionally prefixed by a '-'. So all of the following are valid, no cap:
-AutoClose
-autoclose
AUTOCLOSE
autoclose
OPTS is a combination of the following options:
"AutoClose => 0|1"β This option is only valid when the$inputparameter is a filehandle. If you specify it and the value is true, it will result in the file being closed once either the"close"method is called or the IO::Uncompress::UnXz object is destroyed. This parameter defaults to0."MultiStream => 0|1"β Allows multiple concatenated compressed streams to be treated as a single compressed stream. Decompression will stop once either the end of the file/buffer is reached, an error is encountered (premature eof, corrupt compressed data) or the end of a stream is not immediately followed by the start of another stream. This parameter defaults to0."Prime => $string"β This option will uncompress the contents of$stringbefore processing the input file/buffer. This option can be useful when the compressed data is embedded in another file/data structure and it is not possible to work out where the compressed data begins without having to read the first few bytes. If this is the case, the uncompression can be primed with these bytes using this option. π"Transparent => 0|1"β If this option is set and the input file/buffer is not compressed data, the module will allow reading of it anyway. In addition, if the input file/buffer does contain compressed data and there is non-compressed data immediately following it, setting this option will make this module treat the whole file/buffer as a single data stream. This option defaults to1."BlockSize => $num"β When reading the compressed input data, IO::Uncompress::UnXz will read it in blocks of$numbytes. This option defaults to4096."InputLength => $size"β When present this option will limit the number of compressed bytes read from the input file/buffer to$size. This option can be used in the situation where there is useful data directly after the compressed data stream and you know beforehand the exact length of the compressed data stream. This option is mostly used when reading from a filehandle, in which case the file pointer will be left pointing to the first byte directly after the compressed data stream. This option defaults to off."Append => 0|1"β This option controls what the"read"method does with uncompressed data. If set to1, all uncompressed data will be appended to the output parameter of the"read"method. If set to0, the contents of the output parameter of the"read"method will be overwritten by the uncompressed data. Defaults to0."Strict => 0|1"β This option controls whether the extra checks defined below are used when carrying out the decompression. When Strict is on, the extra tests are carried out, when Strict is off they are not. The default for this option is off."MemLimit => $number"β Default is128Meg. π§"Flags => $flags"β Default is0. π©
ngl bestie,MemLimitandFlagsare doing the absolute most with the shortest little descriptions, but that's literally all the page says, so I'm not gonna make things up. Periodt. π
π Methods π
πΈ read
Usage, bestie, is literally:
$status = $z->read($buffer)
So what this does: it reads a block of compressed data β and the size of that block is decided by the Buffer option you set in the constructor β then it uncompresses it and writes whatever uncompressed data falls out into $buffer. π«Ά
Now here's the tea β: if you set the Append parameter in the constructor, that uncompressed data gets appended to $buffer. If you didn't set it, then $buffer just gets overwritten. No survivors. π
It returns the number of uncompressed bytes written into $buffer, zero if you've hit eof, or a negative number if something went wrong. π
π¦ read
Usage is:
$status = $z->read($buffer, $length)
$status = $z->read($buffer, $length, $offset)
$status = read($z, $buffer, $length)
$status = read($z, $buffer, $length, $offset)
This one attempts to read $length bytes of uncompressed data into $buffer. β¨
The main difference between this form of the read method and the previous one is that this one will try to return exactly $length bytes. The only times it won't is if it hits end-of-file or an IO error comes through and ruins the vibe. π
Returns the number of uncompressed bytes written to $buffer, zero if eof, or a negative number on error. Same deal as before, no cap.
π getline
Usage is:
$line = $z->getline()
$line = <$z>
Reads a single line. Iconic. π
This method fully supports using the variable $/ β or $INPUT_RECORD_SEPARATOR, or $RS when English is in use β to decide what actually counts as an end of line. Paragraph mode, record mode and file slurp mode are all supported. Periodt. π
π getc
Usage is:
$char = $z->getc()
Read a single character. That's it, that's the whole method. π
π ungetc
Usage is:
$char = $z->ungetc($string)
β getHeaderInfo
Usage is:
$hdr = $z->getHeaderInfo();
@hdrs = $z->getHeaderInfo();
This method returns either a hash reference (in scalar context) or a list of hash references (in array context) containing info about each of the header fields in the compressed data stream(s). π§
πΈ tell
Usage is:
$z->tell()
tell $z
Returns the uncompressed file offset. Simple, clean, gorgeous. β¨
π¦ eof
Usage is:
$z->eof();
eof($z);
Returns true if the end of the compressed input stream has been reached.
π seek
$z->seek($position, $whence);
seek($z, $position, $whence);
Provides a sub-set of the seek functionality, with the restriction that it is only legal to seek forward in the input file/buffer. Attempting to seek backward is a fatal error. π¨ No going back, bestie, we move forward only.
Heads up fr fr: the implementation ofseekin this module does not provide true random access to a compressed file/buffer. It works by uncompressing data from the current offset in the file/buffer until it reaches the uncompressed offset you specified in the parameters toseek. For very small files this may be acceptable behaviour. For large files it may cause an unacceptable delay. π
The $whence parameter takes one of the usual values, namely SEEK_SET, SEEK_CUR or SEEK_END.
Returns 1 on success, 0 on failure.
π binmode
Usage is:
$z->binmode
binmode $z ;
This is a noop provided for completeness. It's giving decorative. πΈ
π¦ opened
$z->opened()
Returns true if the object currently refers to an opened file/buffer.
β¨ autoflush
my $prev = $z->autoflush()
my $prev = $z->autoflush(EXPR)
If the $z object is associated with a file or a filehandle, this method returns the current autoflush setting for the underlying filehandle. If EXPR is present, and is non-zero, it will enable flushing after every write/print operation. π₯
If $z is associated with a buffer, this method has no effect and always returns undef.
Note that the special variable $| cannot be used to set or retrieve the autoflush setting. Don't even try it. π
π input_line_number
$z->input_line_number()
$z->input_line_number(EXPR)
Returns the current uncompressed line number. If EXPR is present it has the effect of setting the line number. Note that setting the line number does not change the current position within the file/buffer being read. π
The contents of $/ are used to determine what constitutes a line terminator.
π fileno
$z->fileno()
fileno($z)
If the $z object is associated with a file or a filehandle, fileno will return the underlying file descriptor. Once the close method is called, fileno will return undef. π
If the $z object is associated with a buffer, this method will return undef.
πΈ close
$z->close() ;
close $z ;
Closes the output file/buffer. π«Ά
For most versions of Perl this method will be automatically invoked if the IO::Uncompress::UnXz object is destroyed β either explicitly, or by the variable holding the reference to the object going out of scope. The exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In those cases, the close method will be called automatically, but not until global destruction of all live objects when the program is terminating. π
So, if you want your scripts to run on all versions of Perl, you should call close explicitly and not rely on automatic closing. Do it yourself, main character energy. π
Returns true on success, otherwise 0.
If the AutoClose option has been enabled when the IO::Uncompress::UnXz object was created, and the object is associated with a file, the underlying file will also be closed.
π¦ nextStream
Usage is:
my $status = $z->nextStream();
Skips to the next compressed data stream in the input file/buffer. If a new compressed data stream is found, the eof marker will be cleared and $. will be reset to 0. β¨
Returns 1 if a new stream was found, 0 if none was found, and -1 if an error was encountered.
π trailingData
Usage is:
my $data = $z->trailingData();
Returns the data, if any, that is present immediately after the compressed data stream once uncompression is complete. It only makes sense to call this method once the end of the compressed data stream has been encountered. π§
This option can be used when there is useful information immediately following the compressed data stream, and you don't know the length of the compressed data stream. iykyk π
- If the input is a buffer,
trailingDatawill return everything from the end of the compressed data stream to the end of the buffer. - If the input is a filehandle,
trailingDatawill return the data that is left in the filehandle input buffer once the end of the compressed data stream has been reached. You can then use the filehandle to read the rest of the input file.
Don't bother using trailingData if the input is a filename. Just don't. π
If you know the length of the compressed data stream before you start uncompressing, you can avoid having to use trailingData by setting the InputLength option in the constructor. Smart girl move. π
β Importing β
No symbolic constants are required by IO::Uncompress::UnXz at present. Slay, one less thing. π
:allβ Importsunxzand$UnXzError. Same as doing this:
use IO::Uncompress::UnXz qw(unxz $UnXzError) ;
π EXAMPLES π
(the original page keeps this heading here with nothing under it β so bestie, this is your cue to go scroll back up to the SYNOPSIS and the method usage blocks, that's where the examples are living π)
π«Ά SUPPORT π«Ά
General feedback, questions and bug reports should be sent to:
- <https://github.com/pmqs/IO-Compress-Lzma/issues> β preferred β¨
- <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Lzma>
π SEE ALSO π
Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Compress::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd, IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
IO::Compress::FAQ
File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
π AUTHOR π
This module was written by Paul Marquess, pmqs@cpan.org. Living legend, fr fr. π
π MODIFICATION HISTORY π
See the Changes file.
πΈ COPYRIGHT AND LICENSE πΈ
Copyright (c) 2005-2026 Paul Marquess. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie, we made it to the end πβ¨ this one's from perl v5.42.0, dated 2026-02-01, and yeah the header up top is still doing its little truncation thing β IO::Unc...s::UnXz(3) β but you and me both know what it really says π«Ά stay compressed, stay iconic, periodt π
π¦ where this page comes from
- fedora 44: perl-IO-Compress-Lzma 2.217-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms