Joveler.Compression.XZ 1.0.0

C# pinvoke library for liblzma (XZ Utils).
Targets .Net Standard 2.0, supports Windows and Linux.

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.

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);
}

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);
}

Release Notes

- Initial release

  • .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 History

Version Downloads Last updated
4.0.0 1,418 6/6/2020
3.1.1 1,060 10/31/2019
3.1.0 156 10/20/2019
3.0.0 163 10/1/2019
1.2.0 955 4/18/2019
1.1.2 650 10/30/2018
1.1.1 227 10/29/2018
1.1.0 262 10/18/2018
1.0.0 295 9/30/2018
Show less