Working with LZ4 Archives

Overview

LZ4 is a compression format focused on high-speed compression and decompression. Aspose.ZIP for .NET API enables you to create and extract LZ4 archives in your applications without relying on third-party tools. The API provides the Lz4Archive class to work with such archives.

An LZ4 archive stores a single data stream. If you need to package several files together, combine TAR archiving with LZ4 compression and create a .tar.lz4 archive.

Compress a File

The following code example demonstrates how to compress a file using the default LZ4 settings:

1    using (Lz4Archive archive = new Lz4Archive())
2    {
3        archive.SetSource("data.bin");
4        archive.Save("data.bin.lz4");
5    }

Extract LZ4 Archive

The following simple code example demonstrates how to open an LZ4 archive and extract its contents:

1    using (Lz4Archive archive = new Lz4Archive("data.bin.lz4"))
2    {
3        archive.Extract("data.bin");
4    }

Extract to Directory

If the archive is opened from a file path, ExtractToDirectory uses the archive file name without the .lz4 extension. For example, data.bin.lz4 is extracted as data.bin. If the archive is opened from a stream, Aspose.ZIP falls back to the default file name File.bin.

1    using (Lz4Archive archive = new Lz4Archive("data.bin.lz4"))
2    {
3        archive.ExtractToDirectory("output");
4    }

Configure LZ4 Frame Options

The Lz4ArchiveSetting class lets you control frame metadata such as content checksum, block checksum, and content size.

 1    using (Lz4Archive archive = new Lz4Archive(new Lz4ArchiveSetting()
 2    {
 3        IncludeContentChecksum = true,
 4        IncludeBlockChecksum = true,
 5        IncludeContentSize = true
 6    }))
 7    {
 8        archive.SetSource("data.bin");
 9        archive.Save("data.bin.lz4");
10    }

IncludeContentSize is applied when the source stream is seekable.

Compose TAR.LZ4 Archive

For multiple files, first pack them into a TAR container and then compress that stream with LZ4:

1    using (TarArchive archive = new TarArchive())
2    {
3        archive.CreateEntry("ptt5", "ptt5");
4        archive.SaveLZ4Compressed("result.tar.lz4", TarFormat.Pax);
5    }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.