Joveler.Compression.XZ 1.0.0

There is a newer version of this package available.
See the version list below for details.
Install-Package Joveler.Compression.XZ -Version 1.0.0
dotnet add package Joveler.Compression.XZ --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Joveler.Compression.XZ, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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);
}
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0 3,437 4/5/2021
4.0.0 4,309 6/6/2020
3.1.1 1,222 10/31/2019
3.1.0 287 10/20/2019
3.0.0 297 10/1/2019
1.2.0 2,572 4/18/2019
1.1.2 839 10/30/2018
1.1.1 419 10/29/2018
1.1.0 458 10/18/2018
1.0.0 496 9/30/2018

- Initial release