Joveler.Compression.XZ 1.0.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package Joveler.Compression.XZ --version 1.0.0
NuGet\Install-Package Joveler.Compression.XZ -Version 1.0.0
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="Joveler.Compression.XZ" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Joveler.Compression.XZ --version 1.0.0
#r "nuget: Joveler.Compression.XZ, 1.0.0"
#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.
// Install Joveler.Compression.XZ as a Cake Addin
#addin nuget:?package=Joveler.Compression.XZ&version=1.0.0

// Install Joveler.Compression.XZ as a Cake Tool
#tool nuget:?package=Joveler.Compression.XZ&version=1.0.0

Usage

Initialization

Joveler.XZ requires explicit loading of liblzma library.

You must call XZInit.GlobalInit() before using Joveler.XZ.

Put this snippet in your application's init code:

public static void InitNativeLibrary
{
    string libPath = null;
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        switch (RuntimeInformation.ProcessArchitecture)
        {
            case Architecture.X64:
                libPath = Path.Combine("x64", "liblzma.dll");
                break;
            case Architecture.X86:
                libPath = Path.Combine("x86", "liblzma.dll");
                break;
        }
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        switch (RuntimeInformation.ProcessArchitecture)
        {
            case Architecture.X64:
                libPath = Path.Combine("x64", "liblzma.so");
                break;
        }
    }

    if (libPath == null)
        throw new PlatformNotSupportedException();

    XZInit.AssemblyInit(libPath);
}

WARNING: Caller process and callee library must have the same architecture!

Embedded binary

Joveler.XZ comes with sets of static binaries of liblzma 5.2.4.
They will be copied into the build directory at build time.

Platform Binary
Windows x86 $(OutDir)\x86\liblzma.dll
Windows x64 $(OutDir)\x64\liblzma.dll
Linux x64 $(OutDir)\x64\liblzma.so

Custom binary

To use custom liblzma binary instead, call XZInit.GlobalInit() with a path to the custom binary.

NOTES
  • Create an empty file named Joveler.Compression.XZ.Precompiled.Exclude in project directory to prevent copy of package-embedded binary.

Cleanup

To unload liblzma library explicitly, call XZInit.GlobalCleanup().

Compression

XZStream

The stream for .xz file format.

Constructor

public XZStream(Stream stream, LzmaMode mode)
    : this(stream, mode, DefaultPreset, 1, false) { }
public XZStream(Stream stream, LzmaMode mode, uint preset)
    : this(stream, mode, preset, 1, false) { }
public XZStream(Stream stream, LzmaMode mode, uint preset, int threads)
    : this(stream, mode, preset, threads, false) { }
public XZStream(Stream stream, LzmaMode mode, bool leaveOpen)
    : this(stream, mode, 0, 1, leaveOpen) { }
public XZStream(Stream stream, LzmaMode mode, uint preset, bool leaveOpen)
    : this(stream, mode, preset, 1, leaveOpen) { }
public XZStream(Stream stream, LzmaMode mode, uint preset, int threads, bool leaveOpen)
  • Preset

Select a compression preset level. 0 to 9 is allowed. Default value (XZStream.DefaultPreset) is 6.

  • Threads

Specify the number of worker threads to use. Setting threads to a special value 0 makes xz use as many threads as there are CPU cores on the system.

The actual number of threads can be less than threads if the input file is not big enough for threading with the given settings or if using more threads would exceed the memory usage limit.

Threaded decompression is not supported.

WARNING: Beware of high memory usage in high preset or many threads.

Examples

Compress file to xz
using (FileStream fsOrigin = new FileStream("file_origin.bin", FileMode.Open))
using (FileStream fsComp = new FileStream("test.xz", FileMode.Create))
using (XZStream zs = new XZStream(fsComp, LzmaMode.Compress, XZStream.DefaultPreset))
{
    fsOrigin.CopyTo(zs);
}
Decompress file from xz
using (FileStream fsComp = new FileStream("test.xz", FileMode.Create))
using (FileStream fsDecomp = new FileStream("file_decomp.bin", FileMode.Open))
using (XZStream zs = new XZStream(fsComp, LzmaMode.Decompress))
{
    zs.CopyTo(fsDecomp);
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Joveler.Compression.XZ:

Package Downloads
Apache.Avro.File.XZ

XZ compression library for Apache.Avro

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Joveler.Compression.XZ:

Repository Stars
pebakery/pebakery
PEBakery is a script engine that specializes in customizing the Windows Preinstalled Environment (WinPE/WinRE).
Version Downloads Last updated
4.2.1 630 2/15/2023
4.2.0 98 2/15/2023
4.1.0 25,566 4/5/2021
4.0.0 6,071 6/6/2020
3.1.1 1,425 10/31/2019
3.1.0 438 10/20/2019
3.0.0 449 10/1/2019
1.2.0 3,421 4/18/2019
1.1.2 1,009 10/30/2018
1.1.1 588 10/29/2018
1.1.0 631 10/18/2018
1.0.0 667 9/30/2018

- Initial release