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_reference is 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_reference is 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_reference is a string wrapped in the characters "<" and ">", unlzma assumes it's an input fileglob string, and the input becomes the list of files matching that fileglob. see File::GlobMapper for more details.
and if $input_filename_or_reference is 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_reference is 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_reference is 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_reference is a string wrapped in the characters "<" and ">", unlzma assumes it's an output fileglob string, and the output is the list of files matching that fileglob.
⚠️ when $output_filename_or_reference is a fileglob string, $input_filename_or_reference must 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_reference is 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 to unlzma that happen to be filehandles. if AutoClose is specified and its value is true, it results in all input and/or output filehandles being closed once unlzma has completed. and it defaults to 0.
  • 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 Append is 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 Append is 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 Append is 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.

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 to 0.
  • 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, 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 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. πŸ¦‹
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 $input parameter is a filehandle. if you set it and the value is true, the file gets closed once either the close method is called or the IO::Uncompress::UnLzma object gets destroyed. defaults to 0.
  • 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 to 0.
  • Prime => $string β€” this one uncompresses the contents of $string before 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 to 1.
  • BlockSize => $num β€” when it's reading the compressed input data, IO::Uncompress::UnLzma reads it in blocks of $num bytes. defaults to 4096.
  • 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 the read method does with uncompressed data. if you set it to 1, all the uncompressed data gets appended to the output parameter of the read method. if you set it to 0, the contents of the output parameter of the read method get overwritten by the uncompressed data. defaults to 0.
  • 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 (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