IO::Unc...:UnLzma(3)
β¨ NAME β¨
IO::Uncompress::UnLzma β Read lzma files/buffers
okay so hi bestie π this whole page is IO::Uncompress::UnLzma(3), and it's literally the module you want when you need to read lzma files or buffers. no cap.
π SYNOPSIS π
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
my $status = unlzma $input => $output [,OPTS]
or die "unlzma failed: $UnLzmaError\n";
my $z = IO::Uncompress::UnLzma->new( $input [OPTS] )
or die "unlzma failed: $UnLzmaError\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()
$UnLzmaError ;
# 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 basically πΈ this module hands you a Perl interface that lets you read lzma files and lzma buffers. that's the whole vibe.
and if you're trying to go the other direction β like writing lzma files/buffers β that's a whole different girl, and her name is the companion module IO::Compress::Lzma. go say hi to her instead. π
π«Ά Functional Interface π«Ά
there's a top-level function called unlzma and she does the "one-shot" thing: uncompression between buffers and/or files, in one single call, no ceremony. but if you want finer control over the whole uncompression process, scroll down to the "OO Interface" section, bestie.
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
unlzma $input_filename_or_reference => $output_filename_or_reference [,OPTS]
or die "unlzma failed: $UnLzmaError\n";
heads up: the functional interface needs Perl5.005 or better. lowkey ancient, highkey fine. β
π unlzma $input_filename_or_reference => $output_filename_or_reference [, OPTS] π
unlzma wants at least two params β $input_filename_or_reference and $output_filename_or_reference β plus zero or more optional params (those live in "Optional Parameters", keep scrolling π).
The $input_filename_or_reference parameter
this one decides where the compressed data is coming from. she's flexible, she can show up in any of these forms:
- A filename β if
$input_filename_or_referenceis a plain simple scalar, it's treated as a filename. the file gets opened for reading and the input data is read out of it. periodt. - A filehandle β if it's a filehandle, the input data is read from it. and you can use the string
'-'as an alias for standard input. iconic. - 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 it's an array reference, every element in that array has to be a filename. the input data is read from each file in turn. and no shortcuts: the complete array gets walked first to make sure it only contains valid filenames before any data is uncompressed. vibe check before the party starts. β¨
- An Input FileGlob string β if
$input_filename_or_referenceis a string wrapped in the characters"<"and">",unlzmaassumes it's an input fileglob string, and the input becomes the list of files matching that fileglob. seeFile::GlobMapperfor more details.
and if$input_filename_or_referenceis literally any other type? you get"undef"back. that's it, that's the deal. π
The $output_filename_or_reference parameter
this one controls where the uncompressed data goes. same energy, different forms:
- A filename β if
$output_filename_or_referenceis a plain simple scalar, it's treated as a filename. the file gets opened for writing and the uncompressed data gets written into it. - A filehandle β if it's a filehandle, the uncompressed data gets written to it. and 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 it's an array reference, the uncompressed data gets pushed onto the array.
- An Output FileGlob β if
$output_filename_or_referenceis a string wrapped in the characters"<"and">",unlzmaassumes it's an output fileglob string, and the output is the list of files matching that fileglob.
β οΈ when$output_filename_or_referenceis a fileglob string,$input_filename_or_referencemust also be a fileglob string. anything else is an error. no exceptions, bestie.
see File::GlobMapper for more details.
and yeah β if$output_filename_or_referenceis any other type,"undef"comes back at you. π
π 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 hold a concatenation of all the uncompressed data from each of the input files/buffers. all of it, stacked up, one after another. it's giving remix album. π
π Optional Parameters π
the optional params for the one-shot unlzma function are (for the most part) identical to the ones used with the OO interface over in the "Constructor Options" section. the exceptions are listed below, and they're kinda special:
AutoClose => 0|1β this one applies to any input or output data streams given tounlzmathat happen to be filehandles. ifAutoCloseis specified and its value is true, it results in all input and/or output filehandles being closed onceunlzmahas completed. and it defaults to0.BinModeOut => 0|1β this option is now a no-op. all files will be written in binmode anyway. it's retired, she's resting. πAppend => 0|1β okay so the behaviour of this one depends entirely on the type of output data stream, so pay attention:- 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 is opened in append mode. otherwise the contents of the file, if there are 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 to"seek"before any uncompressed data is written to it. otherwise the file pointer does not move. not even a little.
- A Buffer β if
when Append is specified and set to true, it appends all uncompressed data to the output data stream. so: if the output is a filehandle, it does 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 the uncompressed data gets appended to the existing buffer. slay.
and 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. bye bye old data. π
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 to0.TrailingData => $scalarβ returns the data, if any, that sits immediately after the compressed data stream once uncompression is complete. this one is your bestie when there's useful info right after the compressed stream and you don't know how long the compressed stream actually is.- if the input is a buffer,
trailingDatareturns everything from the end of the compressed data stream to the end of the buffer. - if the input is a filehandle,
trailingDatareturns the data left in the filehandle input buffer once the end of the compressed data stream has been reached. and then you can use the filehandle to read the rest of the input file. π¦
- if the input is a buffer,
don't bother using trailingData if the input is a filename. fr fr, skip it.
and if you already know the length of the compressed data stream before you start uncompressing, you can dodge trailingData entirely by setting the InputLength option. iykyk. β
π OneShot Examples π
to read the contents of the file file1.txt.lzma and write the uncompressed data to the file file1.txt:
use strict ;
use warnings ;
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
my $input = "file1.txt.lzma";
my $output = "file1.txt";
unlzma $input => $output
or die "unlzma failed: $UnLzmaError\n";
to read from an existing Perl filehandle, $input, and write the uncompressed data to a buffer, $buffer:
use strict ;
use warnings ;
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
use IO::File ;
my $input = IO::File->new( "<file1.txt.lzma" )
or die "Cannot open 'file1.txt.lzma': $!\n" ;
my $buffer ;
unlzma $input => \$buffer
or die "unlzma failed: $UnLzmaError\n";
to uncompress all files in the directory /my/home that match *.txt.lzma and store the compressed data in the same directory:
use strict ;
use warnings ;
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
unlzma '</my/home/*.txt.lzma>' => '</my/home/#1.txt>'
or die "unlzma failed: $UnLzmaError\n";
and if you want to compress each file one at a time, this will do the trick:
use strict ;
use warnings ;
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
for my $input ( glob "/my/home/*.txt.lzma" )
{
my $output = $input;
$output =~ s/.lzma// ;
unlzma $input => $output
or die "Error compressing '$input': $UnLzmaError\n";
}
π¦ OO INTERFACE π¦
okay so you want the ~fancy~ object-oriented route instead of the functional one? valid, let's go π this is the glow-up version where you get a whole object to play with.
β¨ Constructor β¨
here's the shape of it, bestie β this is literally the exact syntax:
my $z = IO::Uncompress::UnLzma->new( $input [OPTS] )
or die "IO::Uncompress::UnLzma failed: $UnLzmaError\n";
so the constructor takes one mandatory parameter, $input (defined below, don't worry), plus zero or more OPTS, which are all listed over in "Constructor Options".
it hands you back an IO::Uncompress::UnLzma object on success πΈ and undef on failure π. if it flops, the variable $UnLzmaError will be holding the error message for you, so go read it.
and here's the iconic part: if you're running Perl 5.005 or better, that $z object you got back can be used exactly like an IO::File filehandle. like, all your normal input file operations just work on it. for example, to read a line from a compressed file/buffer you can do either of these:
$line = $z->getline();
$line = <$z>;
here's a simple little example where we use the OO interface to read the compressed file myfile.lzma and write its contents straight to stdout:
my $filename = "myfile.lzma";
my $z = IO::Uncompress::UnLzma->new($filename)
or die "IO::Uncompress::UnLzma failed: $UnLzmaError\n";
while (<$z>) {
print $_;
}
$z->close();
head over to "EXAMPLES" for even more, iykyk β¨
so about that mandatory $input parameter β it's the thing that tells the module where your compressed data is actually coming from. it can take one of three forms:
π· A filename
if the $input parameter is a scalar, it gets assumed to be a filename. that file gets opened for reading, and the compressed data gets read out of it. simple as π
π· A filehandle
if the $input parameter is a filehandle, the compressed data gets read from it. also, the string '-' can be used as an alias for standard input π«Ά
π· A scalar reference
if $input is a scalar reference, then the compressed data gets read from $$input. yes literally, dereference and all π
π Constructor Options π
first things first: these option names are case insensitive, and you can optionally slap a '-' in front of them. so all of these are totally valid:
-AutoClose
-autoclose
AUTOCLOSE
autoclose
and OPTS is just a combo of the following options:
AutoClose => 0|1β this one is only valid when your$inputparameter is a filehandle. if you set it and the value is true, the file gets closed once either theclosemethod is called or theIO::Uncompress::UnLzmaobject gets destroyed. defaults to0.MultiStream => 0|1β lets multiple concatenated compressed streams be treated as one single compressed stream π decompression stops once either the end of the file/buffer is reached, an error is hit (premature eof, corrupt compressed data), or the end of a stream isn't immediately followed by the start of another stream. defaults to0.Prime => $stringβ this one uncompresses the contents of$stringbefore it processes the input file/buffer. super useful when your compressed data is embedded inside another file/data structure and there's just no way to work out where the compressed data begins without reading the first few bytes first. in that case, you can prime the uncompression with those bytes using this option πTransparent => 0|1β if this is set and the input file/buffer is not compressed data, the module will let you read it anyway π. plus, if the input file/buffer does contain compressed data and there's non-compressed data immediately following it, setting this option makes the module treat the whole entire file/buffer as a single data stream. this one defaults to1.BlockSize => $numβ when it's reading the compressed input data,IO::Uncompress::UnLzmareads it in blocks of$numbytes. defaults to4096.InputLength => $sizeβ when you use this, it limits the number of compressed bytes read from the input file/buffer to$size. this is the move when there's useful data sitting directly after the compressed data stream and you already know the exact length of the compressed data stream beforehand. it's mostly used when reading from a filehandle, in which case the file pointer gets left pointing at the first byte directly after the compressed data stream β¨ this option defaults to off.Append => 0|1β this controls what thereadmethod does with uncompressed data. if you set it to1, all the uncompressed data gets appended to the output parameter of thereadmethod. if you set it to0, the contents of the output parameter of thereadmethod get overwritten by the uncompressed data. defaults to0.Strict => 0|1β this controls whether the extra checks defined below get used while doing the decompression. when Strict is on, the extra tests get carried out; when Strict is off, they don't. the default for this option is off.
Methods π
okay bestie, now we get to the actual moves β every single method you can call on your IO::Uncompress::UnLzma object. all of them. no skips. let's go π«Ά
read π
usage is:
$status = $z->read($buffer)
reads a block of compressed data β and the size of that compressed block is decided by the "Buffer" option you gave the constructor, iykyk β uncompresses it, and writes whatever uncompressed data falls out into $buffer. if you set the "Append" parameter in the constructor, that uncompressed data gets appended to $buffer. otherwise $buffer gets straight-up overwritten, no mercy π
returns the number of uncompressed bytes written to $buffer, or zero if eof, or a negative number on error π
read β¨ (this one takes $length)
usage is:
$status = $z->read($buffer, $length)
$status = $z->read($buffer, $length, $offset)
$status = read($z, $buffer, $length)
$status = read($z, $buffer, $length, $offset)
attempts to read $length bytes of uncompressed data into $buffer.
the main character difference between this form of the "read" method and the previous one is that this one will attempt to return exactly $length bytes. the only circumstances where it won't is if end-of-file or an IO error is encountered.
returns the number of uncompressed bytes written to $buffer, zero if eof, or a negative number on error.
getline πΈ
usage is:
$line = $z->getline()
$line = <$z>
reads a single line.
this method fully supports the use of the $/ variable (or $INPUT_RECORD_SEPARATOR, or $RS when "English" is in use) to determine what constitutes an end of line. paragraph mode, record mode and file slurp mode are all supported π
getc π
usage is:
$char = $z->getc()
read a single character.
ungetc π¦
$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) that contains information about each of the header fields in the compressed data stream(s). very demure, very informative β¨
tell β
usage is:
$z->tell()
tell $z
returns the uncompressed file offset.
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. it is a fatal error to attempt to seek backward. no cap.
note that the implementation of"seek"in 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 specified in the parameters to"seek". 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.
opened π
$z->opened()
returns true if the object currently refers to a 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.
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::UnLzma object is destroyed (either explicitly or by the variable with the reference to the object going out of scope). the exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. in these cases, the "close" method will be called automatically, but not until global destruction of all live objects when the program is terminating.
therefore, if you want your scripts to be able to run on all versions of Perl, you should call "close" explicitly and not rely on automatic closing. fr fr.
returns true on success, otherwise 0.
if the "AutoClose" option has been enabled when the IO::Uncompress::UnLzma 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.
if the input is a buffer, "trailingData" will return everything from the end of the compressed data stream to the end of the buffer.
if the input is a filehandle, "trailingData" will 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 π
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.
Importing π
no symbolic constants are required by IO::Uncompress::UnLzma at present.
:all imports "unlzma" and $UnLzmaError. same as doing this:
use IO::Uncompress::UnLzma qw(unlzma $UnLzmaError) ;
EXAMPLES π
(nothing here bestie β the heading is just vibes β¨)
SUPPORT π«Ά
general feedback/questions/bug reports should be sent to https://github.com/pmqs/IO-Compress-Lzma/issues (preferred) or 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::Compress::Xz, IO::Uncompress::UnXz, 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".
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 a wrap bestie π
this whole thing is perl v5.42.0, dated 2026-02-01, and it's the IO::Unc...:UnLzma(3) manual. go forth and uncompress, you iconic thing π«Άβ¨
π¦ 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