Ecng.Lzma
1.0.65
dotnet add package Ecng.Lzma --version 1.0.65
NuGet\Install-Package Ecng.Lzma -Version 1.0.65
<PackageReference Include="Ecng.Lzma" Version="1.0.65" />
<PackageVersion Include="Ecng.Lzma" Version="1.0.65" />
<PackageReference Include="Ecng.Lzma" />
paket add Ecng.Lzma --version 1.0.65
#r "nuget: Ecng.Lzma, 1.0.65"
#:package Ecng.Lzma@1.0.65
#addin nuget:?package=Ecng.Lzma&version=1.0.65
#tool nuget:?package=Ecng.Lzma&version=1.0.65
Lzma - LZMA Compression Library
A .NET implementation of the LZMA (Lempel-Ziv-Markov chain-Algorithm) compression algorithm. This library provides stream-based compression and decompression capabilities with a simple, easy-to-use API.
Features
- Stream-based LZMA compression and decompression
- Compatible with .lzma file format
- Full control over compression parameters
- Support for both raw LZMA streams and containerized streams
- Configurable dictionary size, literal context bits, and other encoding properties
Installation
Add a reference to the Lzma project in your solution.
Quick Start
Basic Compression
using System.IO;
using System.IO.Compression;
using Lzma;
// Compress data to a file
using (var fileStream = File.Create("output.lzma"))
using (var lzmaStream = new LzmaStream(fileStream, CompressionMode.Compress, leaveOpen: false))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello, LZMA!");
lzmaStream.Write(data, 0, data.Length);
}
Basic Decompression
using System.IO;
using System.IO.Compression;
using Lzma;
// Decompress data from a file
using (var fileStream = File.OpenRead("output.lzma"))
using (var lzmaStream = new LzmaStream(fileStream, CompressionMode.Decompress, leaveOpen: false))
{
byte[] buffer = new byte[1024];
int bytesRead = lzmaStream.Read(buffer, 0, buffer.Length);
string result = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine(result); // Output: Hello, LZMA!
}
API Reference
LzmaStream Class
The main class for LZMA compression/decompression. Inherits from System.IO.Stream.
Constructors
// Create a compression or decompression stream with default settings
LzmaStream(Stream stream, CompressionMode mode, bool leaveOpen)
// Create a compression stream with specified compression level
LzmaStream(Stream stream, CompressionLevel level, bool leaveOpen)
// Create a compression stream with custom encoder properties
LzmaStream(Stream stream, EncoderProperties properties)
Properties
CanRead- Returns true if the stream is in decompression modeCanWrite- Returns true if the stream is in compression modeCanSeek- Always returns false (seeking is not supported)Length- Gets the length of the decoded data (decompression mode only)
Methods
Read(byte[] buffer, int offset, int count)- Reads and decodes data (decompression mode)Write(byte[] buffer, int offset, int count)- Encodes and writes data (compression mode)Flush()- Flushes the stream and completes compressionClose()- Closes the stream
Advanced Usage
Custom Compression Settings
using Lzma;
// Create custom encoder properties
var encoderProperties = new EncoderProperties(
workingSize: 1u << 12, // Encoding buffer size
dictionarySize: 1u << 24, // 16 MB dictionary
lc: 3, // Literal context bits (0-8)
lp: 0, // Literal position bits (0-4)
pb: 2, // Position bits (0-4)
writeEndMarker: true // Write end marker on close
);
using (var fileStream = File.Create("output.lzma"))
using (var lzmaStream = new LzmaStream(fileStream, encoderProperties))
{
// Compress with custom settings
byte[] data = File.ReadAllBytes("input.txt");
lzmaStream.Write(data, 0, data.Length);
}
EncoderProperties
Controls the compression behavior:
EncoderProperties properties = EncoderProperties.Default;
// Adjust properties
properties.DictionarySize = 1u << 23; // 8 MB dictionary
properties.LC = 3; // Literal context bits
properties.LP = 0; // Literal position bits
properties.PB = 2; // Position bits
properties.WriteEndMarker = true; // Write end marker
Property Descriptions:
WorkingSize- Size of the encoding buffer in bytesDictionarySize- Size of the dictionary in bytes (larger = better compression, more memory)LC- Number of high bits of previous byte to use as context (0-8)LP- Number of low bits of dictionary position to include in literal position state (0-4)PB- Number of low bits of dictionary position to include in position state (0-4)WriteEndMarker- Whether to write an end marker when encoding is complete
Working with Raw LZMA Streams
For raw LZMA streams without container headers, use EncoderStream and DecoderStream directly:
Raw Compression
using Lzma;
using (var fileStream = File.Create("output.raw"))
{
var encoderStream = new EncoderStream(fileStream);
encoderStream.Initialize(EncoderProperties.Default);
byte[] data = System.Text.Encoding.UTF8.GetBytes("Raw LZMA data");
encoderStream.Write(data, 0, data.Length);
encoderStream.Close();
}
Raw Decompression
using Lzma;
using (var fileStream = File.OpenRead("output.raw"))
{
var decoderProperties = DecoderProperties.Default;
var decoderStream = new DecoderStream(fileStream);
decoderStream.Initialize(decoderProperties);
byte[] buffer = new byte[1024];
int bytesRead = decoderStream.Read(buffer, 0, buffer.Length);
string result = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
}
Usage Examples
Compress a File
using System.IO;
using System.IO.Compression;
using Lzma;
public void CompressFile(string inputPath, string outputPath)
{
using (var inputStream = File.OpenRead(inputPath))
using (var outputStream = File.Create(outputPath))
using (var lzmaStream = new LzmaStream(outputStream, CompressionMode.Compress, false))
{
inputStream.CopyTo(lzmaStream);
}
}
Decompress a File
using System.IO;
using System.IO.Compression;
using Lzma;
public void DecompressFile(string inputPath, string outputPath)
{
using (var inputStream = File.OpenRead(inputPath))
using (var lzmaStream = new LzmaStream(inputStream, CompressionMode.Decompress, false))
using (var outputStream = File.Create(outputPath))
{
lzmaStream.CopyTo(outputStream);
}
}
Compress in Memory
using System.IO;
using System.IO.Compression;
using Lzma;
public byte[] CompressData(byte[] data)
{
using (var memoryStream = new MemoryStream())
{
using (var lzmaStream = new LzmaStream(memoryStream, CompressionMode.Compress, true))
{
lzmaStream.Write(data, 0, data.Length);
}
return memoryStream.ToArray();
}
}
public byte[] DecompressData(byte[] compressedData)
{
using (var inputStream = new MemoryStream(compressedData))
using (var lzmaStream = new LzmaStream(inputStream, CompressionMode.Decompress, true))
using (var outputStream = new MemoryStream())
{
lzmaStream.CopyTo(outputStream);
return outputStream.ToArray();
}
}
Compress Text
using System.IO;
using System.IO.Compression;
using System.Text;
using Lzma;
public string CompressString(string text)
{
byte[] data = Encoding.UTF8.GetBytes(text);
using (var memoryStream = new MemoryStream())
{
using (var lzmaStream = new LzmaStream(memoryStream, CompressionMode.Compress, true))
{
lzmaStream.Write(data, 0, data.Length);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
public string DecompressString(string compressedBase64)
{
byte[] compressedData = Convert.FromBase64String(compressedBase64);
using (var inputStream = new MemoryStream(compressedData))
using (var lzmaStream = new LzmaStream(inputStream, CompressionMode.Decompress, true))
using (var outputStream = new MemoryStream())
{
lzmaStream.CopyTo(outputStream);
return Encoding.UTF8.GetString(outputStream.ToArray());
}
}
Important Notes
- Always call
Flush()orClose()on the compression stream to ensure all data is written - The stream does not support seeking (
CanSeekis always false) - For compression, the decompressed size is written to the header automatically
- Larger dictionary sizes provide better compression but require more memory
- The default encoder properties provide a good balance between compression ratio and speed
Performance Tips
- Dictionary Size: Use the largest dictionary size that fits in available memory
- Working Size: Increase for better compression on large files
- Reuse Streams: When compressing multiple small files, consider reusing stream instances
- Buffer Size: Use appropriate buffer sizes when reading/writing data
Target Frameworks
- .NET Standard 2.0
- .NET 6.0
- .NET 10.0
License
This library is distributed under its respective license. Please refer to the source code headers for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Ecng.Lzma:
| Package | Downloads |
|---|---|
|
NyxAssets
Load Nyx-style .spr / .dat (Asset Editor–compatible parsing). |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.65 | 1,966 | 12/21/2025 |
| 1.0.64 | 349 | 12/19/2025 |
| 1.0.63 | 25,430 | 2/26/2025 |
| 1.0.62 | 12,531 | 9/17/2024 |
| 1.0.61 | 13,856 | 4/14/2024 |
| 1.0.60 | 12,689 | 1/2/2024 |
| 1.0.59 | 13,234 | 9/8/2023 |
| 1.0.58 | 7,747 | 6/29/2023 |
| 1.0.57 | 9,967 | 5/8/2023 |
| 1.0.56 | 10,660 | 4/3/2023 |
| 1.0.55 | 6,762 | 3/6/2023 |
| 1.0.54 | 9,888 | 2/9/2023 |
| 1.0.53 | 4,790 | 2/7/2023 |
| 1.0.52 | 928 | 2/4/2023 |
| 1.0.51 | 3,969 | 2/2/2023 |
| 1.0.50 | 3,782 | 1/30/2023 |
| 1.0.49 | 14,480 | 12/30/2022 |
| 1.0.48 | 5,879 | 12/12/2022 |
| 1.0.47 | 15,945 | 11/11/2022 |
| 1.0.46 | 1,065 | 11/11/2022 |
Added comprehensive README.md documentation for all projects