Compres...aw::Lzma(3)
π Compres...aw::Lzma π β the Compress::Raw::Lzma page, bestie edition π
(Perl's own User Contributed Perl Documentation, no cap β this is the Compres...aw::Lzma(3) manpage and yes we're doing the whole thing)
β¨ NAME β¨
Compress::Raw::Lzma β the low-level Perl interface to the lzma compression library π¦
β¨ SYNOPSIS β¨
okay so here's the whole vibe up front, no gatekeeping:
use Compress::Raw::Lzma ;
# Encoders
my ($lz, $status) = new Compress::Raw::Lzma::EasyEncoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::AloneEncoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::StreamEncoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::RawEncoder [OPTS]
or die "Cannot create lzma object: $status\n";
$status = $lz->code($input, $output);
$status = $lz->flush($output);
# Decoders
my ($lz, $status) = new Compress::Raw::Lzma::AloneDecoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::AutoDecoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::StreamDecoder [OPTS]
or die "Cannot create lzma object: $status\n";
my ($lz, $status) = new Compress::Raw::Lzma::RawDecoder [OPTS]
or die "Cannot create lzma object: $status\n";
$status = $lz->code($input, $output);
my $version = Compress::Raw::Lzma::lzma_version_number();
my $version = Compress::Raw::Lzma::lzma_version_string();
four encoders, four decoders, two version functions, and two little workhorse methods that do all the actual labour. iconic honestly.
β¨ DESCRIPTION β¨
so Compress::Raw::Lzma is your in-memory compression / uncompression hookup straight to the lzma compression library π
like, yes, the main reason this module exists is so that IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz and IO::Uncompress::UnXz have something to stand on β but bestie, it works perfectly fine on its own for simple compression / uncompression tasks too π«Ά
There are two functions that show up in every single compression and uncompression interface in this module, and they're called code and flush. By default, both of these overwrite whatever data was already sitting in the output buffer parameter π so if your dream is to compress/uncompress into one single buffer and have code and flush just keep appending to it, you gotta turn on the AppendOutput option when you create the compression/decompression object. periodt.
π Compression π
There are four compression interfaces hanging out in this module:
Compress::Raw::Lzma::EasyEncoderCompress::Raw::Lzma::AloneEncoderCompress::Raw::Lzma::StreamEncoderCompress::Raw::Lzma::RawEncoder
π ($z, $status) = new Compress::Raw::Lzma::EasyEncoder [OPTS];
Makes you a brand new xz compression object β¨
If it goes well, it returns the freshly initialised compression object $z and a $status of LZMA_OK β that's in list context. In scalar context you just get the deflation object $z, and nothing else, so know which one you're in, bestie.
If it does not go well, the returned compression object $z will be undef and $status will hold an lzma error code. sad but informative.
Here's the full list of valid options, no skips:
Preset => $presetβ used to choose the compression preset. valid values are0-9andLZMA_PRESET_DEFAULT.0is the fastest compression with the lowest memory usage and the lowest compression.9is the slowest compression with the highest memory usage, but it gives you the best compression. defaults toLZMA_PRESET_DEFAULT. π«Extreme => 0|1β makes the compression a lot slower, but you get a small compression gain out of it. defaults to0.Check => $checkβ used to specify the integrity check used in the xz data stream. valid values areLZMA_CHECK_NONE,LZMA_CHECK_CRC32,LZMA_CHECK_CRC64,LZMA_CHECK_SHA256. defaults toLZMA_CHECK_CRC32. πAppendOutput => 0|1β controls whether the compressed data gets appended to the output buffer in thecodeandflushmethods. defaults to0. (heads up: in versions of this module prior to2.072the default value was incorrectly documented as1β so if you read an old doc, that's why you're confused, it wasn't you π« )BufSize => $numberβ sets the initial size for the output buffer used by the$d->codemethod. if the buffer has to be reallocated to grow bigger, it grows in increments ofBufsize. defaults to16k.
π ($z, $status) = new Compress::Raw::Lzma::AloneEncoder [OPTS];
Creates you a legacy lzma compression object. this format is also known as lzma_alone. it's giving vintage, respectfully. π°οΈ
If it works, you get the initialised compression object $z and a $status of LZMA_OK in list context. In scalar context you get the deflation object $z only.
If it doesn't work, $z is undef and $status holds an lzma error code.
Valid options, the whole list:
Filter => $filterβ the$filteroption must be an object of typeLzma::Filter::Lzma1. SeeLzma::Filter::Lzmain Compress::Raw::Lzma for a definition ofLzma::Filter::Lzma1. if this option isn't present, anLzma::Filter::Lzma1object with default values gets used instead.AppendOutput => 0|1β controls whether the compressed data gets appended to the output buffer in thecodeandflushmethods. defaults to0. (again: in versions of this module prior to2.072the default value was incorrectly documented as1)BufSize => $numberβ sets the initial size for the output buffer used by the$d->codemethod. if the buffer has to be reallocated to increase the size, it grows in increments ofBufsize. defaults to16k.
π ($z, $status) = new Compress::Raw::Lzma::StreamEncoder [OPTS];
Creates you an xz compression object β¨
Success = you get the initialised compression object $z and a $status of LZMA_OK in list context. Scalar context = you get the deflation object $z only.
No success = $z is undef, $status holds an lzma error code. π
The valid options, all of them:
Filter => $filterFilter => [$filter1, $filter2,...]β this option is used to change the behaviour of the StreamEncoder by applying between one andLZMA_FILTERS_MAXfilters to the data stream during compression. See "Filters" for more details on the available filters. if this option is present it must either contain a singleLzma::Filter::Lzmafilter object, or an array reference containing between one andLZMA_FILTERS_MAXfilter objects. if this option is not present, anLzma::Filter::Lzma2object with default values is used. π§Check => $checkβ used to specify the integrity check used in the xz data stream. valid values areLZMA_CHECK_NONE,LZMA_CHECK_CRC32,LZMA_CHECK_CRC64,LZMA_CHECK_SHA256. defaults toLZMA_CHECK_CRC32.AppendOutput => 0|1β controls whether the compressed data is appended to the output buffer in thecodeandflushmethods. defaults to0. (and once more for the people in the back: in versions of this module prior to2.072the default value was incorrectly documented as1)BufSize => $numberβ sets the initial size for the output buffer used by the$d->codemethod. if the buffer has to be reallocated to increase the size, it will grow in increments ofBufsize. defaults to16k.
π ($z, $status) = new Compress::Raw::Lzma::RawEncoder [OPTS];
Low level access to lzma. this is the no-frills, down-to-the-metal one, iykyk. π₯
If successful, it returns the initialised compression object $z and a $status of LZMA_OK in list context. In scalar context, the deflation object $z only.
If not successful, $z will be undef and $status will hold an lzma error code.
The valid options, all four:
Filter => $filterFilter => [$filter1, $filter2,...]β this option changes the behaviour of the RawEncoder by applying between one andLZMA_FILTERS_MAXfilters to the data stream during compression. See "Filters" for more details on the available filters. if present it must either contain a singleLzma::Filter::Lzmafilter object, or an array reference containing between one andLZMA_FILTERS_MAXfilter objects. if it's not present, anLzma::Filter::Lzma2object with default values gets used.AppendOutput => 0|1β controls whether the compressed data is appended to the output buffer in thecodeandflushmethods. defaults to0. (yes, and in versions of this module prior to2.072the default value was incorrectly documented as1β same saga, different encoder π)BufSize => $numberβ sets the initial size for the output buffer used by the$d->codemethod. if the buffer has to be reallocated to increase the size, it will grow in increments ofBufsize. defaults to16k.ForZip => 1/0β this boolean option enables prefixing the compressed data stream with an encoded copy of the filter properties. defaults to0. π
π
$status = $lz->code($input, $output)
Reads what's in $input, compresses it, and writes the compressed data into $output.
Returns LZMA_OK on success, and an lzma error code on failure. β¨
If appendOutput is enabled in the constructor for the lzma object, the compressed data gets appended to $output. If it's not enabled, $output gets truncated before the compressed data is written into it. so watch out for that, bestie.
π
$status = $lz->flush($output, LZMA_FINISH);
Flushes any pending compressed data out to $output. by default, it terminates the compressed data stream β so like, that's the end of the road, no more data after that.
Returns LZMA_STREAM_END on success, and an lzma error code on failure. π¦
okay that's part 1 wrapped, and there's more coming so don't go anywhere π
π Compression examples π
Okay bestie, time for the fun part β actual code. Grab a snack. π
β¨ Simple compression with EasyEncoder β¨
The gateway drug. Preset 6, no extreme mode, in and out. π
use strict;
use warnings;
use Compress::Raw::Lzma;
my ($lz, $status) = Compress::Raw::Lzma::EasyEncoder->new(
Preset => 6,
Extreme => 0,
);
die "Cannot create encoder: $status\n" unless $lz;
my $input = "This is the data to compress " x 100;
my $compressed = '';
$status = $lz->code($input, $compressed);
die "Compression failed: $status\n" unless $status == LZMA_OK;
$status = $lz->flush($compressed);
die "Flush failed: $status\n" unless $status == LZMA_STREAM_END;
print "Original size: ", length($input), "\n";
print "Compressed size: ", length($compressed), "\n";
π¦ Streaming compression with AppendOutput π¦
When your data comes in little pieces like a group chat and you don't wanna hold it all in memory. Set AppendOutput => 1 and keep feeding it. π
use strict;
use warnings;
use Compress::Raw::Lzma;
my ($lz, $status) = Compress::Raw::Lzma::EasyEncoder->new(
AppendOutput => 1,
Preset => LZMA_PRESET_DEFAULT,
);
die "Cannot create encoder: $status\n" unless $lz;
my $compressed = '';
# Compress data in chunks
for my $chunk ("Hello ", "world", "!") {
$status = $lz->code($chunk, $compressed);
die "Compression failed\n" unless $status == LZMA_OK;
}
# Finish compression
$status = $lz->flush($compressed);
die "Flush failed\n" unless $status == LZMA_STREAM_END;
print "Compressed: ", length($compressed), " bytes\n";
πΈ Using custom filters with StreamEncoder πΈ
Now we're in our main character era β hand-crafting a custom LZMA2 filter, every knob exactly where you want it. π
use strict;
use warnings;
use Compress::Raw::Lzma;
# Create a custom LZMA2 filter
my $filter = Lzma::Filter::Lzma2(
DictSize => 1024 * 1024 * 8, # 8MB dictionary
Lc => 3,
Lp => 0,
Pb => 2,
Mode => LZMA_MODE_NORMAL,
Nice => 128,
Mf => LZMA_MF_BT4,
Depth => 512,
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => $filter,
Check => LZMA_CHECK_SHA256,
);
die "Cannot create encoder: $status\n" unless $lz;
my $input = "Custom filter example data";
my $compressed = '';
$status = $lz->code($input, $compressed);
$status = $lz->flush(compressed);
π₯ Using multiple filters (BCJ + LZMA2) π₯
Stacking filters like it's an outfit. BCJ for x86 binaries, LZMA2 for the actual squishing. Iconic behavior, no cap. β¨
use strict;
use warnings;
use Compress::Raw::Lzma;
# Combine BCJ filter for x86 binaries with LZMA2 compression
my @filters = (
Lzma::Filter::X86(), # BCJ filter for x86 code
Lzma::Filter::Lzma2(), # LZMA2 compression
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => \@filters,
Check => LZMA_CHECK_CRC64,
);
die "Cannot create encoder: $status\n" unless $lz;
# ... compress binary data ...
π Uncompression π
Now the other direction, bestie. Going backwards. π«Ά There are four uncompression interfaces chilling in this module:
Compress::Raw::Lzma::AutoDecoderCompress::Raw::Lzma::AloneDecoderCompress::Raw::Lzma::StreamDecoderCompress::Raw::Lzma::RawDecoder
π ($z, $status) = new Compress::Raw::Lzma::AutoDecoder [OPTS] ;
This one's the social butterfly β it can uncompress any of the compressed data streams that this module is capable of creating. Versatile queen. π
If it works, you get the freshly initialised uncompression object, $z, plus a $status of "LZMA_OK" β but only in list context. In scalar context it just hands you the deflation object, $z, and nothing else. Periodt.
If it doesn't work? $z comes back undef and $status holds an lzma error code. Sad but informative. π
Here's the full rundown of valid options:
-MemLimitβ the number of bytes to use when uncompressing. Default is unlimited β go wild, no limits, main character energy. π-Bufsizeβ sets the initial size for the output buffer used by the$i->codemethod. If that output buffer has to be reallocated to grow bigger, it grows in increments ofBufsize. Default is16k.-AppendOutputβ controls how data gets written to the output buffer by the$i->codemethod. Set it to false and the output buffer gets truncated before uncompressed data is written into it. Set it to true and the uncompressed data gets appended to the output buffer by$i->code. Defaults to false.-ConsumeInputβ if set to true, this strips compressed data out of the input buffer of the$i->codemethod as uncompression rolls along. Super handy when you're processing compressed data that's embedded inside another file or buffer β that way whatever immediately follows the compressed stream stays sitting in the input buffer. Defaults to true.-LimitOutputβ this one changes how$i->codebehaves so the memory used by the output buffer can actually be capped. WhenLimitOutputis on, the output buffer size will be either the value of theBufsizeoption or however much memory is already allocated to$outputβ whichever is bigger. Predicting the available output size is genuinely tricky, so don't expect an exact output buffer size, fr fr. WhenLimitOutoutis not specified,$i->codewill use as much memory as it takes to write all the uncompressed data it creates by uncompressing the input buffer. And heads up: ifLimitOutputis enabled, theConsumeInputoption gets enabled too. Defaults to false.
π ($z, $status) = new Compress::Raw::Lzma::AloneDecoder [OPTS] ;
This one un-compresses an lzma_alone data stream. It's a specialist. π
If it works, you get the initialised uncompression object, $z, plus $status of "LZMA_OK" in list context. In scalar context it gives you just the deflation object, $z.
If it doesn't work, $z is undef and $status holds an lzma error code.
Valid options, coming right up:
-MemLimitβ the number of bytes to use when uncompressing. Default is unlimited.-Bufsizeβ sets the initial size for the output buffer used by the$i->codemethod. If the output buffer in this method has to be reallocated to grow, it grows in increments ofBufsize. Default is16k.-AppendOutputβ controls how data is written to the output buffer by the$i->codemethod. Set to false, and the output buffer gets truncated before uncompressed data is written to it. Set to true, and uncompressed data gets appended to the output buffer by$i->code. Defaults to false.-ConsumeInputβ if set to true, it removes compressed data from the input buffer of the$i->codemethod as uncompression progresses. Useful when the compressed data is embedded in another file or buffer, because then the data immediately following the compressed stream stays in the input buffer. Defaults to true.-LimitOutputβ changes the behavior of$i->codeso the amount of memory the output buffer uses can be limited. WhenLimitOutputis used, the size of the output buffer will be either the value ofBufsizeor the memory already allocated to$output, whichever is larger. Predicting the output size is tricky, so don't count on an exact output buffer size, bestie. WhenLimitOutoutisn't specified,$i->codeuses as much memory as it needs to write all the uncompressed data it creates by uncompressing the input buffer. And ifLimitOutputis enabled,ConsumeInputgets enabled too. Defaults to false.
β¨ $status = $z->code($input, $output); β¨
The workhorse. It uncompresses $input and writes the uncompressed data to $output. π
Returns "LZMA_OK" if the uncompression worked but we haven't hit the end of the compressed data stream yet. Returns "LZMA_STREAM_END" on a successful uncompression where the end of the compression stream has been reached. That's the finish line, slay. π
If consumeInput is enabled in the constructor for the lzma object, $input will have all its compressed data removed after uncompression. On an "LZMA_OK" return, that means $input will be an empty string. When it's "LZMA_STREAM_END", $input will either be an empty string or will contain whatever data immediately followed the compressed data stream.
If appendOutput is enabled in the constructor for the lzma object, the uncompressed data gets appended to $output. If it's not enabled, $output gets truncated before the uncompressed data is written to it. π¦
β¨ Uncompression examples β¨
bestie, these are the decoder examples. read them, love them, run them. π
π Simple uncompression with AutoDecoder
use strict;
use warnings;
use Compress::Raw::Lzma;
# $compressed contains xz/lzma compressed data
my $compressed = ...;
my ($lz, $status) = Compress::Raw::Lzma::AutoDecoder->new(
AppendOutput => 1,
ConsumeInput => 1,
);
die "Cannot create decoder: $status\n" unless $lz;
my $uncompressed = '';
$status = $lz->code($compressed, $uncompressed);
die "Decompression failed: $status\n"
unless $status == LZMA_STREAM_END;
print "Uncompressed size: ", length($uncompressed), "\n";
πΈ Streaming uncompression with LimitOutput
use strict;
use warnings;
use Compress::Raw::Lzma;
my ($lz, $status) = Compress::Raw::Lzma::AutoDecoder->new(
LimitOutput => 1,
ConsumeInput => 1,
Bufsize => 4096,
);
die "Cannot create decoder: $status\n" unless $lz;
# Process compressed data in chunks
my $compressed = ...; # your compressed data
my $uncompressed = '';
while (length($compressed) > 0) {
my $output = '';
$status = $lz->code($compressed, $output);
$uncompressed .= $output;
last if $status == LZMA_STREAM_END;
die "Decompression error: $status\n" unless $status == LZMA_OK;
}
print "Total uncompressed: ", length($uncompressed), " bytes\n";
π¦ Uncompressing specific format with AloneDecoder
use strict;
use warnings;
use Compress::Raw::Lzma;
# For legacy .lzma format (lzma_alone)
my ($lz, $status) = Compress::Raw::Lzma::AloneDecoder->new(
MemLimit => 128 * 1024 * 1024, # 128 MB limit
AppendOutput => 0,
);
die "Cannot create decoder: $status\n" unless $lz;
my $compressed = ...; # .lzma format data
my $output = '';
$status = $lz->code($compressed, $output);
die "Decompression failed: $status\n"
unless $status == LZMA_STREAM_END;
π Filters π
okay so here's the tea: some of the Lzma compression interfaces β literally Compress::Raw::Lzma::StreamEncoder & Compress::Raw::Lzma::AloneEncoder β plus the raw lzma uncompression interface all use filters. filters are the little knobs that change the behaviour of compression (and raw uncompression). ποΈβ¨
and every single Lzma Filter is sub-classed from the Lzma::Filter base-class. no cap. π
π Lzma::Filter::Lzma
the Lzma::Filter::Lzma class is the primary compression filter for LZMA algorithms. it gives you fine-grained control over compression parameters. like, extremely fine-grained. π§
there are two subclasses of Lzma::Filter::Lzma, namely Lzma::Filter::Lzma1 and Lzma::Filter::Lzma2.
- the former is typically used with
Compress::Raw::Lzma::AloneEncoder. - the latter with
Compress::Raw::Lzma::StreamEncoder.
β οΈ when you're using Lzma filters, anLzma::Filter::Lzmamust be included, and it must be the last filter in the chain. also there can only be oneLzma::Filter::Lzmafilter in any filter chain. one. single. periodt. π
the Lzma::Filter::Lzma construction takes these options:
DictSize=> $value β dictionary size in bytes. this controls how many bytes of the recently processed uncompressed data is kept in memory. the size of the dictionary must be at leastLZMA_DICT_SIZE_MIN. defaults toLZMA_DICT_SIZE_DEFAULT. π§PresetDict=> $dict β provide an initial dictionary. this value is used to initialize the LZ77 history window. this feature only works correctly with raw encoding and decoding, so you may not be able to decode other formats that have been encoded with a preset dictionary. $dict should contain typical strings that occur in the files being compressed, with the most probable strings near the end of the preset dictionary. if $dict is larger thanDictSize, only the lastDictSizebytes are processed. πLc=> $value β number of literal context bits. how many of the highest bits of the previous uncompressed eight-bit byte (also known as aliteral) are taken into account when predicting the bits of the next literal. $value must be a number betweenLZMA_LCLP_MINandLZMA_LCLP_MAX. heads up: the sum of theLcandLpoptions cannot exceed 4. defaults toLZMA_LC_DEFAULT.Lp=> $value β number of literal position bits. how many of the lowest bits of the current position (number of bytes from the beginning of the uncompressed data) in the uncompressed data is taken into account when predicting the bits of the next literal (a single eight-bit byte). defaults toLZMA_LP_DEFAULT.Pb=> $value β number of position bits. how many of the lowest bits of the current position in the uncompressed data is taken into account when estimating probabilities of matches. a match is a sequence of bytes for which a matching sequence is found from the dictionary and thus can be stored as a distance-length pair. $value must be a number betweenLZMA_PB_MINandLZMA_PB_MAX. defaults toLZMA_PB_DEFAULT.Mode=> $value β the Compression Mode. valid values areLZMA_MODE_FASTandLZMA_MODE_NORMAL. defaults toLZMA_MODE_NORMAL. π₯Nice=> $value β nice length of a match. defaults to64.Mf=> $value β defines which Match Finder to use. valid values areLZMA_MF_HC3,LZMA_MF_HC4,LZMA_MF_BT2,LZMA_MF_BT3andLZMA_MF_BT4. defaults toLZMA_MF_BT4.Depth=> $value β maximum search depth in the match finder. defaults to0.
π Lzma::Filter::BCJ
the sub-classes of Lzma::Filter::BCJ are the Branch/Call/Jump conversion filters. these filters are used to rewrite executable binary code for a number of processor architectures. and the best part? none of these classes take any options. zero. zilch. iconic behaviour honestly. β¨
Lzma::Filter::X86β filter for x86 binaries.Lzma::Filter::PowerPCβ filter for Big endian PowerPC binaries.Lzma::Filter::IA64β filter for IA64 (Itanium) binaries.Lzma::Filter::ARMβ filter for ARM binaries.Lzma::Filter::ARMThumbβ filter for ARMThumb binaries.Lzma::Filter::Sparcβ filter for Sparc binaries.
π Lzma::Filter::Delta
usage is
Lzma::Filter::Delta [OPTS]
Type=> $type β defines the type of Delta calculation. the only available type (and therefore the default) isLZMA_DELTA_TYPE_BYTE.Distance=> $value β defines the Delta Distance. $value must be a number betweenLZMA_DELTA_DIST_MINandLZMA_DELTA_DIST_MAX. default isLZMA_DELTA_DIST_MIN. π
Filter examples πΈ
okay bestie, this is the fun part β real code you can copy-paste into your little .pl file and go absolutely feral with. every example below is verbatim, so don't @ me if you change one character and it explodes π
Using LZMA2 filter with custom dictionary size π
big dictionary = better compression, and that's just the vibe. watch:
use strict;
use warnings;
use Compress::Raw::Lzma;
# Create LZMA2 filter with large dictionary for better compression
my $filter = Lzma::Filter::Lzma2(
DictSize => 16 * 1024 * 1024, # 16 MB dictionary
Lc => 3,
Lp => 0,
Pb => 2,
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => $filter,
Check => LZMA_CHECK_CRC64,
);
die "Cannot create encoder: $status\n" unless $lz;
my $input = "Data to compress...";
my $output = '';
$lz->code($input, $output);
$lz->flush($output);
Using LZMA1 filter for legacy .lzma format π¦
the old-school format, the vintage one, the one your grandma's tar archive uses. respect the elders π
use strict;
use warnings;
use Compress::Raw::Lzma;
# LZMA1 is used for the legacy .lzma format
my $filter = Lzma::Filter::Lzma1(
DictSize => 8 * 1024 * 1024,
Mode => LZMA_MODE_NORMAL,
Mf => LZMA_MF_BT4,
Nice => 273,
);
my ($lz, $status) = Compress::Raw::Lzma::AloneEncoder->new(
Filter => $filter,
);
die "Cannot create encoder: $status\n" unless $lz;
Using preset-based LZMA2 filter β¨
why hand-tune forty parameters when a preset can do it for you? lazy girlies stay winning:
use strict;
use warnings;
use Compress::Raw::Lzma;
# Use preset instead of manually configuring parameters
# Preset 9 = maximum compression
my $filter = Lzma::Filter::Lzma2::Preset(9 | LZMA_PRESET_EXTREME);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => $filter,
);
die "Cannot create encoder: $status\n" unless $lz;
Combining Delta filter with LZMA2 π
delta filter is so underrated. it's giving "data with tiny byte-to-byte changes" energy β think raw images, audio samples. chef's kiss:
use strict;
use warnings;
use Compress::Raw::Lzma;
# Delta filter is useful for data with small changes between bytes
# (e.g., uncompressed images, audio samples)
my @filters = (
Lzma::Filter::Delta(
Type => LZMA_DELTA_TYPE_BYTE,
Distance => 1,
),
Lzma::Filter::Lzma2(
DictSize => 8 * 1024 * 1024,
),
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => \@filters,
Check => LZMA_CHECK_CRC32,
);
die "Cannot create encoder: $status\n" unless $lz;
# Compress image or audio data
my $raw_data = ...;
my $compressed = '';
$lz->code($raw_data, $compressed);
$lz->flush($compressed);
Using BCJ filter for x86 executable compression π
BCJ = Branch/Call/Jump, and no, it does not stand for "bestie coded java" π it makes x86 binaries compress way nicer. periodt:
use strict;
use warnings;
use Compress::Raw::Lzma;
# BCJ (Branch/Call/Jump) filter optimizes compression of x86 binaries
my @filters = (
Lzma::Filter::X86(Offset => 0),
Lzma::Filter::Lzma2(),
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => \@filters,
Check => LZMA_CHECK_SHA256,
);
die "Cannot create encoder: $status\n" unless $lz;
# Read and compress x86 binary
open my $fh, '<', 'program.exe' or die $!;
binmode $fh;
my $binary = do { local $/; <$fh> };
close $fh;
my $compressed = '';
$lz->code($binary, $compressed);
$lz->flush($compressed);
print "Original: ", length($binary), " bytes\n";
print "Compressed: ", length($compressed), " bytes\n";
Using ARM filter for ARM binaries π§
same energy, different architecture. ARM girlies rise:
use strict;
use warnings;
use Compress::Raw::Lzma;
# For ARM executables
my @filters = (
Lzma::Filter::ARM(),
Lzma::Filter::Lzma2(
DictSize => 4 * 1024 * 1024,
),
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => \@filters,
);
die "Cannot create encoder: $status\n" unless $lz;
Maximum compression with multiple filters π₯
stacking filters like stacking accessories. 64 MB dictionary, LZMA_MODE_NORMAL, Nice => 273, Depth => 1024 β she's going through it and I love her for it:
use strict;
use warnings;
use Compress::Raw::Lzma;
# Combine filters for maximum compression
my @filters = (
Lzma::Filter::Delta(Distance => 1),
Lzma::Filter::Lzma2(
DictSize => 64 * 1024 * 1024, # 64 MB
Lc => 4,
Lp => 0,
Pb => 2,
Mode => LZMA_MODE_NORMAL,
Nice => 273,
Mf => LZMA_MF_BT4,
Depth => 1024,
),
);
my ($lz, $status) = Compress::Raw::Lzma::StreamEncoder->new(
Filter => \@filters,
Check => LZMA_CHECK_SHA256,
);
die "Cannot create encoder: $status\n" unless $lz;
Misc π
just a couple of little version-checkers, cute and useful, no notes:
my $version = Compress::Raw::Lzma::lzma_version_number();β hands you the version of the underlying lzma library this module is using at run-time, as a number.my $version = Compress::Raw::Lzma::lzma_version_string();β same thing but as a string, run-time flavor.my $version = Compress::Raw::Lzma::LZMA_VERSION();β the version of the underlying lzma library this module was using at compile-time, as a number. (yes, compile-time. she's different.)my $version = Compress::Raw::Lzma::LZMA_VERSION_STRING();β compile-time version, but as a string. slay.
Constants β¨
bestie these are all exported by this module. no cap, they're just there for you. π
Return/Status Codes π
LZMA_OKβ operation completed successfully. the good ending.LZMA_STREAM_ENDβ end of stream reached, meaning the whole compressed thing has been decoded/encoded. done and dusted.LZMA_NO_CHECKβ the input stream has no integrity check. she's raw, she's unfiltered.LZMA_UNSUPPORTED_CHECKβ the integrity check can't be calculated because this build doesn't support it. not her fault.LZMA_GET_CHECKβ the integrity check type is now available. mystery solved.LZMA_MEM_ERRORβ cannot allocate memory. πLZMA_MEMLIMIT_ERRORβ memory usage limit was reached. she hit the ceiling.LZMA_FORMAT_ERRORβ file format not recognized. who is she.LZMA_OPTIONS_ERRORβ invalid or unsupported options. respectfully, no.LZMA_DATA_ERRORβ data is corrupt. πLZMA_BUF_ERRORβ no progress is possible because the stream is truncated or corrupt. stuck in traffic fr.LZMA_PROG_ERRORβ programming error, and per the docs this should never happen in correct usage. so if you see itβ¦ bestie what did you do π
Flush Modes π
LZMA_RUNβ keep coding. this is the normal-operation one.LZMA_SYNC_FLUSHβ makes all the input available at output. not commonly used with LZMA.LZMA_FULL_FLUSHβ finishes encoding of the current Block. also not commonly used.LZMA_FINISHβ finishes the coding operation, and all input must have been given to the encoder first. no takebacks.
Integrity Check Types π‘οΈ
LZMA_CHECK_NONEβ no Check is calculated. chaotic.LZMA_CHECK_CRC32β CRC32 using the polynomial from IEEE-802.3.LZMA_CHECK_CRC64β CRC64 using the polynomial from ECMA-182.LZMA_CHECK_SHA256β SHA-256 checksum. the strong one.LZMA_CHECK_ID_MAXβ maximum valid Check ID.LZMA_CHECK_SIZE_MAXβ maximum size of any Check algorithm.
Preset and Compression Level β
LZMA_PRESET_DEFAULTβ the default compression preset, which is currently6.LZMA_PRESET_LEVEL_MASKβ mask for the preset level,0-9.LZMA_PRESET_EXTREMEβ the extreme compression preset flag. can be combined with a preset level using bitwise OR, like6 | LZMA_PRESET_EXTREME. go big or go home.
Stream Flags π©
LZMA_TELL_NO_CHECKβ accept streams with no integrity check.LZMA_TELL_UNSUPPORTED_CHECKβ accept streams with an unsupported integrity check.LZMA_TELL_ANY_CHECKβ accept any type of integrity check. she's not picky.LZMA_CONCATENATEDβ enable support for concatenated streams.
Match Finder Types π
LZMA_MF_HC3β Hash Chain with 3 byte hashing.LZMA_MF_HC4β Hash Chain with 4 byte hashing.LZMA_MF_BT2β Binary Tree with 2 byte hashing.LZMA_MF_BT3β Binary Tree with 3 byte hashing.LZMA_MF_BT4β Binary Tree with 4 byte hashing, and it's the default and recommended one. main character energy fr.
Compression Mode β‘
LZMA_MODE_FASTβ fast compression mode.LZMA_MODE_NORMALβ normal compression mode, and it's the default.
Dictionary and Parameter Limits π
LZMA_DICT_SIZE_MINβ minimum dictionary size,4KiB.LZMA_DICT_SIZE_DEFAULTβ default dictionary size.LZMA_LCLP_MINβ minimum forlcandlp, which is0.LZMA_LCLP_MAXβ maximum forlcandlp, which is4.LZMA_LC_DEFAULTβ default number of literal context bits, which is3.LZMA_LP_DEFAULTβ default number of literal position bits, which is0.LZMA_PB_MINβ minimum number of position bits, which is0.LZMA_PB_MAXβ maximum number of position bits, which is4.LZMA_PB_DEFAULTβ default number of position bits, which is2.
BCJ (Branch/Call/Jump) Filters ποΈ
architecture-specific filters for executable code. iykyk:
LZMA_FILTER_X86β filter for x86 (32-bit and 64-bit) binaries.LZMA_FILTER_POWERPCβ filter for Big endian PowerPC binaries.LZMA_FILTER_IA64β filter for IA-64 (Itanium) binaries.LZMA_FILTER_ARMβ filter for ARM binaries.LZMA_FILTER_ARMTHUMBβ filter for ARM-Thumb binaries.LZMA_FILTER_SPARCβ filter for SPARC binaries.
Delta Filter π
LZMA_FILTER_DELTAβ the delta filter ID.LZMA_DELTA_TYPE_BYTEβ byte-wise delta calculation.LZMA_DELTA_DIST_MINβ minimum delta distance, which is1.LZMA_DELTA_DIST_MAXβ maximum delta distance, which is256.
LZMA Filters π
LZMA_FILTER_LZMA2β the LZMA2 filter, used in the.xzformat.LZMA_FILTERS_MAXβ maximum number of filters in a chain, which is4.
Stream Header and Block π¦
LZMA_STREAM_HEADER_SIZEβ size of the Stream Header,12bytes.LZMA_BLOCK_HEADER_SIZE_MINβ minimum size of the Block Header.LZMA_BLOCK_HEADER_SIZE_MAXβ maximum size of the Block Header.LZMA_BACKWARD_SIZE_MINβ minimum Backward Size.
Version Information π·οΈ
LZMA_VERSIONβ encoded version number of liblzma.LZMA_VERSION_MAJORβ major version number.LZMA_VERSION_MINORβ minor version number.LZMA_VERSION_PATCHβ patch version number.LZMA_VERSION_STABILITYβ stability indicator.LZMA_VERSION_STABILITY_STRINGβ stability as a string, e.g."alpha","beta","stable".LZMA_VERSION_STRINGβ the full version string.
SUPPORT π«Ά
got feedback? questions? bugs? general feelings? send them here:
- <https://github.com/pmqs/Compress-Raw-Lzma/issues> β preferred, the fav π
- <https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Lzma> β the other one, still valid, still loved.
π SEE ALSO π
okay bestie, here's the whole friend group π β every single one of these is doing a similar job and they all wanna be in your use statements:
Compress::ZlibIO::Compress::GzipIO::Uncompress::GunzipIO::Compress::DeflateIO::Uncompress::InflateIO::Compress::RawDeflateIO::Uncompress::RawInflateIO::Compress::Bzip2IO::Uncompress::Bunzip2IO::Compress::LzmaIO::Uncompress::UnLzmaIO::Compress::XzIO::Uncompress::UnXzIO::Compress::LzipIO::Uncompress::UnLzipIO::Compress::LzopIO::Uncompress::UnLzopIO::Compress::LzfIO::Uncompress::UnLzfIO::Compress::ZstdIO::Uncompress::UnZstdIO::Uncompress::AnyInflateIO::Uncompress::AnyUncompress
and then the little bonus crew β¨:
IO::Compress::FAQFile::GlobMapperArchive::ZipArchive::TarIO::Zlib
like, so many icons in one lineup, it's giving compression cinematic universe π
π AUTHOR π
this module was written by Paul Marquess, "pmqs@cpan.org". absolute legend behavior, no cap π
π¦ MODIFICATION HISTORY π¦
See the Changes file. that's where all the lore lives, bestie π
π 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. slay, go forth, remix responsibly π
and that's a wrap on the whole page, bestie π«Ά this one was rendered by perl v5.42.0 and dated 2026-03-08, from the page titled Compres...aw::Lzma(3) β thanks for reading all five parts, you're literally the main character πΈβ¨
π¦ where this page comes from
- fedora 44: perl-Compress-Raw-Lzma 2.221-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