Joveler.Compression.XZ 1.0.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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
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.3.0 3,587 9/1/2023
4.2.3 176 8/27/2023
4.2.2 1,034 8/6/2023
4.2.1 10,069 2/15/2023
4.2.0 240 2/15/2023
4.1.0 61,759 4/5/2021
4.0.0 9,482 6/6/2020
3.1.1 1,559 10/31/2019
3.1.0 557 10/20/2019
3.0.0 561 10/1/2019
1.2.0 4,579 4/18/2019
1.1.2 1,143 10/30/2018
1.1.1 720 10/29/2018
1.1.0 745 10/18/2018
1.0.0 785 9/30/2018

- Initial release