Meziantou.Framework.Compression
1.0.1
Prefix Reserved
dotnet add package Meziantou.Framework.Compression --version 1.0.1
NuGet\Install-Package Meziantou.Framework.Compression -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Meziantou.Framework.Compression" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.Compression" Version="1.0.1" />
<PackageReference Include="Meziantou.Framework.Compression" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Meziantou.Framework.Compression --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Meziantou.Framework.Compression, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Meziantou.Framework.Compression@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Meziantou.Framework.Compression&version=1.0.1
#tool nuget:?package=Meziantou.Framework.Compression&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Meziantou.Framework.Compression
A .NET library, written in C# only, that reads and writes the archive formats and the compressed streams that
System.IO.Compression does not support, with an API similar to ZipArchive and ZipFile.
Features
- 7z: read, create and update archives (LZMA, LZMA2, PPMd, BZip2, Deflate, Deflate64, BCJ/BCJ2/Delta filters, AES-256
encryption, solid blocks, multi-volume archives) with
SevenZipArchiveandSevenZipFile - RAR: read RAR5 and RAR 2.0 to 4.x archives (solid, multi-volume, encrypted, self-extracting) with
RarArchiveandRarFile - zip: read the archives that
System.IO.Compression.ZipArchivecannot (BZip2, LZMA, PPMd, XZ, Zstandard, Shrink, Reduce, Implode, WinZip AES, ZipCrypto, split archives) withZipArchiveandZipFile - zip writing: create and update zip archives compressed with Stored, Deflate, BZip2, LZMA, PPMd, XZ or Zstandard (.NET 11), encrypted with WinZip AES or ZipCrypto, with zip64, to seekable or non-seekable streams
- Any archive: open zip, 7z, RAR and tar archives (compressed with gzip, bzip2, XZ, LZMA, lzip, Zstandard or Unix
compress) whose format is detected from their content with
ArchiveFile, or read them forward only from a stream that is not seekable withArchiveReader - Streams:
XzStream,LzmaStream,LZipStreamandBZip2Streamcompress and decompress likeGZipStream;UnixCompressStreamdecompresses.Zfiles - Safe extraction:
ExtractToDirectoryrejects the paths and the links that leave the destination directory, andArchiveReaderOptions.MaxMemoryUsagelimits the memory of the decoders
Usage
The types are in the Meziantou.Framework.Compression namespace.
Open any archive
// The format is detected from the content: zip, 7z, RAR, or tar compressed with gzip, bzip2, XZ, ...
using (var archive = ArchiveFile.Open(path))
{
foreach (var entry in archive.Entries)
{
if (entry.IsDirectory)
continue;
using var stream = entry.Open();
using var reader = new StreamReader(stream);
var text = reader.ReadToEnd();
Assert.NotNull(text);
}
var hello = archive.GetEntry("hello.txt");
Assert.Equal(16, hello?.Length);
}
ArchiveFile.ExtractToDirectory(path, destination);
Read an archive from a stream that is not seekable
// The stream does not need to be seekable: a network response, a pipe, ...
using var input = File.OpenRead(path);
using var archiveReader = ArchiveReader.Create(input);
Assert.Equal(ArchiveFormat.Tar, archiveReader.Format);
Assert.Equal(CompressionFormat.GZip, archiveReader.CompressionFormat);
while (archiveReader.MoveToNextEntry())
{
var entry = archiveReader.Entry!;
if (entry.FullName.EndsWith(".txt", StringComparison.Ordinal))
{
// The content can be read until the reader moves to the next entry
using var content = archiveReader.OpenEntryStream();
using var copy = new MemoryStream();
content.CopyTo(copy);
Assert.Equal(entry.Length, copy.Length);
}
}
Create a 7z archive
var options = new SevenZipCompressionOptions
{
Method = SevenZipCompressionMethod.Lzma2,
Level = 9,
};
SevenZipFile.CreateFromDirectory(sourceDirectory, archivePath, options);
using var archive = SevenZipFile.OpenRead(archivePath);
Assert.Equal("file.txt", archive.Entries[0].FullName);
Create an encrypted zip archive
var options = new ZipCompressionOptions
{
CompressionMethod = ZipCompressionMethod.BZip2,
Password = "secret",
Encryption = ZipEncryption.Aes256,
};
using var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true, readerOptions: null, options))
{
using var writer = new StreamWriter(archive.CreateEntry("secret.txt").Open());
writer.Write("Encrypted with WinZip AES");
}
stream.Position = 0;
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: false, new ArchiveReaderOptions { Password = "secret" }, compressionOptions: null))
{
var entry = archive.GetEntry("secret.txt")!;
Assert.Equal(ZipEncryption.Aes256, entry.EncryptionMethod);
using var reader = new StreamReader(entry.Open());
Assert.Equal("Encrypted with WinZip AES", reader.ReadToEnd());
}
Compress with XZ
var data = "Hello, XZ! Hello, XZ! Hello, XZ!"u8.ToArray();
using var compressed = new MemoryStream();
using (var xz = new XzStream(compressed, new XzCompressionOptions { Preset = 9, Check = XzCheck.Sha256 }, leaveOpen: true))
{
xz.Write(data);
}
compressed.Position = 0;
using var decompress = new XzStream(compressed, CompressionMode.Decompress);
using var result = new MemoryStream();
decompress.CopyTo(result);
Assert.Equal(data, result.ToArray());
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net11.0 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- System.IO.Hashing (>= 11.0.0)
-
net11.0
- System.IO.Hashing (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.