IO::UNCOMPRESS::ANYUNCOMPRESS(3perl)
β¨ NAME β¨
okay bestie, welcome to IO::Uncompress::AnyUncompress(3perl) from the Perl Programmers Reference Guide π. This is IO::Uncompress::AnyUncompress β she uncompresses gzip, zip, bzip2, zstd, xz, lzma, lzip, lzf or lzop file/buffer. No cap, she's the main character of compression detection. π
π¦ SYNOPSIS π¦
here's the summoning circle, fr fr:
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
my $status = anyuncompress $input => $output [,OPTS]
or die "anyuncompress failed: $AnyUncompressError\n";
my $z = IO::Uncompress::AnyUncompress->new( $input [OPTS] )
or die "anyuncompress failed: $AnyUncompressError\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()
$AnyUncompressError ;
# 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 πΈ
okay so this module hands you a Perl interface, and that interface lets you read files/buffers that were compressed by a whole variety of compression libraries. It's giving "one interface to read them all" energy, but make it Perl. π«Ά
The formats she supports are lowkey a whole iconic lineup:
And the auto-detect? The module will auto-detect which, if any, of the supported compression formats is being used. She just knows, bestie. β¨
π Functional Interface π
Okay bestie, so there's a top-level function called anyuncompress that just does the whole thing in one shot β buffers, files, whatever, it handles it. If you want finer control over your uncompression journey though, you gotta go peek at the "OO Interface" section instead. That's the deluxe version. β¨
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
anyuncompress $input_filename_or_reference => $output_filename_or_reference [,OPTS]
or die "anyuncompress failed: $AnyUncompressError\n";
Heads up queen: this functional interface needs Perl5.005 or better. So if you're running some ancient vibe, this is not the function for you. π
β¨ anyuncompress $input_filename_or_reference => $output_filename_or_reference [, OPTS] β¨
So anyuncompress needs at least two parameters β $input_filename_or_reference and $output_filename_or_reference β plus however many optional ones your heart desires (check "Optional Parameters" for those).
π The $input_filename_or_reference parameter π
This parameter is literally the whole vibe check for where your compressed data is coming from. It can be so many things, it's giving main character energy:
- A filename β If
$input_filename_or_referenceis a simple scalar, it's assumed to be a filename, periodt. The file gets opened for reading and the input data comes straight out of it. π - A filehandle β If
$input_filename_or_referenceis a filehandle, the input data gets read from it. And listen β the string'-'works as an alias for standard input. Iconic. π - A scalar reference β If
$input_filename_or_referenceis a scalar reference, the input data is read from$$input_filename_or_reference. - An array reference β If
$input_filename_or_referenceis an array reference, every element in that array must be a filename. The input data gets read from each file in turn. And here's the tea: the complete array gets walked through first to make sure it only contains valid filenames before any data is even uncompressed. Safety first, bestie. π‘οΈ - An Input FileGlob string β If
$input_filename_or_referenceis a string wrapped in<and>characters,anyuncompressis like "oh that's a fileglob string" and treats the input as the list of files matching that fileglob. SeeFile::GlobMapperfor all the deets. π
If$input_filename_or_referenceis literally any other type?undefgets returned. That's it. That's the answer. Sorry bestie. π
π The $output_filename_or_reference parameter π
This one controls where the uncompressed data goes. And it's just as versatile, ngl:
- A filename β If
$output_filename_or_referenceis a simple scalar, it's a filename, no cap. That file gets opened for writing and all the uncompressed data lands in it. β¨ - A filehandle β If
$output_filename_or_referenceis a filehandle, the uncompressed data is written to it. And again, the string'-'is an alias for standard output. π«Ά - A scalar reference β If
$output_filename_or_referenceis a scalar reference, the uncompressed data is 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 wrapped in<and>characters,anyuncompressassumes it's an output fileglob string, and the output is the list of files matching that fileglob.
β οΈ IMPORTANT: When$output_filename_or_referenceis a fileglob string,$input_filename_or_referencemust also be a fileglob string. Anything else? That's an error. Straight up. π
See File::GlobMapper for more details.
And if$output_filename_or_referenceis any other type? Yep βundefgets returned. Same energy as before. π
π¦ Notes π¦
Okay so here's a cute little edge case you need to know about. When $input_filename_or_reference maps to multiple compressed files/buffers, and $output_filename_or_reference is just a single file/buffer β after uncompression, $output_filename_or_reference will contain a concatenation of all the uncompressed data from each of the input files/buffers. It just squishes them all together. The more you know. β
π Optional Parameters π
So the optional parameters for the one-shot anyuncompress are β for the most part β identical to the ones used with the OO interface (head to the "Constructor Options" section for those). But there are exceptions, and here they are, listed below because of course:
"AutoClose => 0|1"β This one applies to any input or output data streams toanyuncompressthat happen to be filehandles. IfAutoCloseis set and it's true, it'll result in all input and/or output filehandles getting closed onceanyuncompresshas completed its little job. This parameter defaults to0. π"BinModeOut => 0|1"β Bestie, this option is now a no-op. All files get written in binmode anyway. It's giving retired legend. π"Append => 0|1"β The behaviour here depends on what kind of output data stream you've got going on:- 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 toseekbefore any uncompressed data is written. Otherwise the file pointer stays exactly where it is, don't move, nope.
- A Buffer β If
When Append is specified and set to true, it appends all uncompressed data to the output data stream. So β filehandle output means a seek to the eof before writing anything. Filename output means opened for appending. Buffer output means everything appended to the existing buffer. β¨
Conversely, when Append is not specified, or it's present and set to false, it operates like this: filename output truncates the file contents before writing anything; filehandle output leaves its position unchanged; buffer output gets wiped clean before any uncompressed data comes out. π§Ή
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. Obsessed. Defaults to0. π₯"TrailingData => $scalar"β Returns the data (if any) that's sitting immediately after the compressed data stream once uncompression is done. This is your bestie when there's useful info right after the compressed stream and you have no clue how long the compressed data 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 whatever's left in the filehandle input buffer once the end of the compressed data stream has been reached. You can then use that filehandle to read the rest of the input file. π - Don't even bother using
trailingDataif the input is a filename. Just don't.
- If the input is a buffer,
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. Slay. π
π OneShot Examples π
π Example 1: file to file π
To read the contents of the file file1.txt.Compressed and write the uncompressed data to the file file1.txt:
use strict ;
use warnings ;
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
my $input = "file1.txt.Compressed";
my $output = "file1.txt";
anyuncompress $input => $output
or die "anyuncompress failed: $AnyUncompressError\n";
π¦ Example 2: filehandle to buffer π¦
To read from an existing Perl filehandle, $input, and write the uncompressed data to a buffer, $buffer:
use strict ;
use warnings ;
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
use IO::File ;
my $input = IO::File->new( "<file1.txt.Compressed" )
or die "Cannot open 'file1.txt.Compressed': $!\n" ;
my $buffer ;
anyuncompress $input => \$buffer
or die "anyuncompress failed: $AnyUncompressError\n";
β¨ Example 3: the fileglob moment β¨
To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory:
use strict ;
use warnings ;
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
or die "anyuncompress failed: $AnyUncompressError\n";
π Example 4: one at a time, queen π
And if you want to compress each file one at a time, this will do the trick:
use strict ;
use warnings ;
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
for my $input ( glob "/my/home/*.txt.Compressed" )
{
my $output = $input;
$output =~ s/.Compressed// ;
anyuncompress $input => $output
or die "Error compressing '$input': $AnyUncompressError\n";
}
π¦ OO Interface π¦
okay bestie, now we're doing the OO interface, and it's lowkey the main character energy of this whole module. β¨
π Constructor π
The constructor for IO::Uncompress::AnyUncompress looks like this:
my $z = IO::Uncompress::AnyUncompress->new( $input [OPTS] )
or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
It takes one mandatory parameter, $input (we'll get to that in a sec), and zero or more OPTS, which are all about "Constructor Options" (spoiler: they're right below). π«Ά
On success you get an IO::Uncompress::AnyUncompress object; on failure you get undef. And if it fails, $AnyUncompressError will hold the error message, so you can die with it like the code above. π
If you're running Perl 5.005 or better, the object $z can be used exactly like an IO::File filehandle. That means all the normal input file operations work 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>;
Here's a simple example of using the OO interface to read the compressed file myfile.Compressed and write its contents to stdout:
my $filename = "myfile.Compressed";
my $z = IO::Uncompress::AnyUncompress->new($filename)
or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\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. It can take one of three forms:
- A filename β if
$inputis a scalar, it's assumed to be a filename. That file gets opened for reading, and the compressed data is read from it. - A filehandle β if
$inputis a filehandle, the compressed data is read from it. The string'-'can be used as an alias for standard input. - A scalar reference β if
$inputis a scalar reference, the compressed data is read from$$input.
β¨ Constructor Options β¨
The option names below are case insensitive and can optionally be prefixed by a '-'. So all of these are valid:
-AutoClose
-autoclose
AUTOCLOSE
autoclose
OPTS is a combination of the following options:
AutoClose => 0|1β this one is only valid when the$inputparameter is a filehandle. If you set it to true, the file will be closed once either theclosemethod is called or theIO::Uncompress::AnyUncompressobject is destroyed. Defaults to0.MultiStream => 0|1β lets multiple concatenated compressed streams 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. Defaults to0.Prime => $stringβ this option will uncompress the contents of$stringbefore processing the input file/buffer. It's useful when the compressed data is embedded in another file/data structure and it's not possible to work out where the compressed data begins without having to read the first few bytes. If that's the case, you can prime the uncompression with those 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. Defaults to1.BlockSize => $numβ when reading the compressed input data,IO::Uncompress::AnyUncompresswill read it in blocks of$numbytes. Defaults to4096.InputLength => $sizeβ when present, this option limits the number of compressed bytes read from the input file/buffer to$size. You'd use this when there's useful data directly after the compressed data stream and you know beforehand the exact length of the compressed data stream. It's mostly used when reading from a filehandle β in that case the file pointer will be left pointing to the first byte directly after the compressed data stream. Defaults to off.Append => 0|1β controls what thereadmethod does with uncompressed data. If set to1, all uncompressed data will be appended to the output parameter of thereadmethod. If set to0, the contents of the output parameter of thereadmethod will be overwritten by the uncompressed data. Defaults to0.Strict => 0|1β 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. Default is off.RawInflate => 0|1β when auto-detecting the compressed format, try to test for raw-deflate (RFC 1951) content using theIO::Uncompress::RawInflatemodule. The reason this is not default behaviour is because RFC 1951 content can only be detected by attempting to uncompress it. This process is error prone and can result in false positives. Defaults to0.UnLzma => 0|1β when auto-detecting the compressed format, try to test for lzma_alone content using theIO::Uncompress::UnLzmamodule. The reason this is not default behaviour is because lzma_alone content can only be detected by attempting to uncompress it. This process is error prone and can result in false positives. Defaults to0.
π Methods π
π read (the first one, yes there's two, we'll get to that)
Usage goes a little something like this:
$status = $z->read($buffer)
So what it 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 that block and dumps the uncompressed data into $buffer. And if you set the Append parameter in the constructor, the uncompressed stuff gets appended onto whatever was already in $buffer. If you didn't set it, $buffer gets fully overwritten. no mercy. π
It returns the number of uncompressed bytes written into $buffer, or zero if you hit eof, or a negative number on error. periodt.
π¦ read (the second one, the glow-up version)
Usage:
$status = $z->read($buffer, $length)
$status = $z->read($buffer, $length, $offset)
$status = read($z, $buffer, $length)
$status = read($z, $buffer, $length, $offset)
This one tries to read $length bytes of uncompressed data into $buffer. β¨
The main character difference between this form and the previous one: this one tries to return exactly $length bytes. The only times it won't is if it hits end-of-file or an IO error shows up. That's it, that's the whole exception list.
Returns the number of uncompressed bytes written to $buffer, zero on eof, negative number on error. same energy as before. π«Ά
π getline
$line = $z->getline()
$line = <$z>
Reads a single line. iconic behaviour honestly.
This method fully supports using the $/ variable (or $INPUT_RECORD_SEPARATOR, or $RS when English is in use β iykyk) to decide what counts as an end of line. Paragraph mode, record mode and file slurp mode? All supported babes. all of them. πΈ
π getc
$char = $z->getc()
Read a single character. just one. tiny.
π§ ungetc
$char = $z->ungetc($string)
β getHeaderInfo
$hdr = $z->getHeaderInfo();
@hdrs = $z->getHeaderInfo();
This method hands you back either a hash reference (in scalar context) or a list of hash references (in array context) containing info about every one of the header fields in the compressed data stream(s). Everything you could possibly wanna know about those headers. π
π tell
$z->tell()
tell $z
Returns the uncompressed file offset. that's the whole vibe.
π₯ eof
$z->eof();
eof($z);
Returns true if the end of the compressed input stream has been reached. congrats, you made it. π
π
seek
$z->seek($position, $whence);
seek($z, $position, $whence);
Gives you a sub-set of the seek functionality, with one big restriction: it is only legal to seek forward in the input file/buffer. Trying to seek backward is a fatal error. do not. I'm serious. π
Also important: the implementation of seek in this module does not give you true random access to a compressed file/buffer. What it actually does is uncompress data from the current offset in the file/buffer until it reaches the uncompressed offset you specified in the seek parameters. For very small files this might be acceptable behaviour. For large files it may cause an unacceptable delay. so like, know what you're getting into.
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
$z->binmode
binmode $z ;
This is a noop, provided for completeness. it does nothing. it's here for the vibes. π
π 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.
β οΈ Heads up bestie: the special variable $| cannot be used to set or retrieve the autoflush setting. don't try it. not today.
π¦ 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. Do note though that setting the line number does not change the current position within the file/buffer being read. it just changes the number. sneaky.
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 gets called, fileno will return undef. gone. poof.
If the $z object is associated with a buffer, this method will return undef too.
π close
$z->close() ;
close $z ;
Closes the output file/buffer.
For most versions of Perl, this method gets automatically invoked if the IO::Uncompress::AnyUncompress object is destroyed β whether that's explicitly or by the variable holding the reference 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 still be called automatically, but not until global destruction of all live objects when the program is terminating. π¬
So therefore: if you want your scripts to run on all versions of Perl, you should call close explicitly and not rely on automatic closing. be proactive babe.
Returns true on success, otherwise 0.
If the AutoClose option was enabled when the IO::Uncompress::AnyUncompress object was created, and the object is associated with a file, the underlying file will also get closed. πͺ
π¦ nextStream
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 gets cleared and $. will be reset to 0. fresh start energy. β¨
Returns 1 if a new stream was found, 0 if none was found, and -1 if an error was encountered.
πΈ trailingData
my $data = $z->trailingData();
Returns the data, if any, that's sitting right there immediately after the compressed data stream once uncompression is complete. It only makes sense to call this method once you've hit the end of the compressed data stream. timing is everything.
This option is useful when there's valuable info immediately following the compressed data stream and you don't know the length of the compressed data stream. mystery data. π
- 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 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 even bother using
trailingDataif the input is a filename. fr fr, skip it.
If you know the length of the compressed data stream before you start uncompressing, you can dodge the whole trailingData situation by setting the InputLength option in the constructor. π‘
ποΈ Importing ποΈ
No symbolic constants are required by IO::Uncompress::AnyUncompress at present. none. zero. zip. π
:allβ Importsanyuncompressand$AnyUncompressError. Same as doing this:
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
β¨ EXAMPLES β¨
(no examples listed in this page, bestie β go check the other IO::Compress manpages for the goods π«Ά)
π«Ά SUPPORT π«Ά
General feedback, questions and bug reports should be sent to <https://github.com/pmqs/IO-Compress/issues> (preferred) or <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
π 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::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::Compress::FAQ
File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
π AUTHOR π
This module was written by Paul Marquess, pmqs@cpan.org. king behaviour honestly. π
π 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 babes, no notes. this was perl v5.44.0, dated 2026-10-17, brought to you by IO::Uncompress::AnyUncompress(3perl) β drink water, call close explicitly, and slay. πβ¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms