Class Lz4Archive

Class Lz4Archive

Namespace: Aspose.Zip.Lz4
Assembly: Aspose.Zip.dll (25.5.0)

This class represents LZ4 archive file. Use it to extract or compose LZ4 archives.

public class Lz4Archive : IArchive, IDisposable, IArchiveFileEntry

Inheritance

object Lz4Archive

Implements

IArchive , IDisposable , IArchiveFileEntry

Inherited Members

object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Constructors

Lz4Archive(Stream)

Initializes a new instance of the Aspose.Zip.Lz4.Lz4Archive class prepared for decompressing.

public Lz4Archive(Stream sourceStream)

Parameters

sourceStream Stream

The source of the archive.

Examples

Open an archive from a stream and extract it to a MemoryStream

var ms = new MemoryStream();
using (Lz4Archive archive = new Lz4Archive(File.OpenRead("archive.lz4")))
  archive.Open().CopyTo(ms);

Remarks

This constructor does not decompress. See Aspose.Zip.Lz4.Lz4Archive.Open method for decompressing.

Exceptions

ArgumentException

Cannot read from sourceStream

ArgumentNullException

sourceStream is null.

EndOfStreamException

sourceStream is too short.

InvalidDataException

The sourceStream has wrong signature.

Lz4Archive(string)

Initializes a new instance of the Aspose.Zip.Lz4.Lz4Archive class.

public Lz4Archive(string path)

Parameters

path string

The path to the archive file.

Examples

Open an archive from file by path and extract it to a MemoryStream

var ms = new MemoryStream();
using (Lz4Archive archive = new Lz4Archive("archive.lz4"))
  archive.Open().CopyTo(ms);

Remarks

This constructor does not decompress. See Aspose.Zip.Lz4.Lz4Archive.Open method for decompressing.

Exceptions

ArgumentNullException

path is null.

SecurityException

The caller does not have the required permission to access

ArgumentException

The path is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file path is denied.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

NotSupportedException

File at path contains a colon (:) in the middle of the string.

EndOfStreamException

The file is too short.

InvalidDataException

Data in the file has the wrong signature.

Lz4Archive(Lz4ArchiveSetting)

Initializes a new instance of the Aspose.Zip.Lz4.Lz4Archive class prepared for compressing.

public Lz4Archive(Lz4ArchiveSetting settings = null)

Parameters

settings Lz4ArchiveSetting

The setting of the composed archive.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Whether managed resources should be disposed.

Extract(string)

Extracts the archive to the file by path.

public FileInfo Extract(string path)

Parameters

path string

The path to destination file. If the file already exists, it will be overwritten.

Returns

FileInfo

Info of an extracted file.

Exceptions

EndOfStreamException

Source stream is too short.

InvalidDataException

Wrong bytes found while decoding.

NotSupportedException

This LZ4 version is not supported.

Extract(Stream)

Extracts the archive to the stream provided.

public void Extract(Stream destination)

Parameters

destination Stream

Destination stream. Must be writable.

Examples

using (var archive = new Lz4Archive("archive.lz4"))
{
     archive.Extract(httpResponseStream);
}

Exceptions

ArgumentException

destination does not support writing.

EndOfStreamException

Source stream is too short.

InvalidDataException

Wrong bytes found while decoding.

NotSupportedException

This LZ4 version is not supported.

InvalidOperationException

The archive is prepared for composition.

ExtractToDirectory(string)

Extracts content of the archive to the directory provided.

public void ExtractToDirectory(string destinationDirectory)

Parameters

destinationDirectory string

The path to the directory to place the extracted files in.

Remarks

If the directory does not exist, it will be created.

Exceptions

ArgumentNullException

destinationDirectory is null.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

SecurityException

The caller does not have the required permission to access the existing directory.

NotSupportedException

If the directory does not exist, the path contains a colon character (:) that is not part of a drive label (“C:").

ArgumentException

destinationDirectory is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the System.IO.Path.GetInvalidPathChars method. -or- path is prefixed with, or contains, only a colon character (:).

IOException

The directory specified by path is a file. -or- The network name is not known.

EndOfStreamException

Source stream is too short.

InvalidDataException

Wrong bytes found while initialize decoding.

InvalidOperationException

The archive is prepared for composition.

Open()

Opens the archive for extraction and provides a stream with archive content.

public Stream Open()

Returns

Stream

The stream that represents the contents of the archive.

Examples

Extracts the archive and copies extracted content to file stream.

using (var archive = new Lz4Archive("archive.lz4"))
{
    using (var extracted = File.Create("data.bin"))
    {
        var unpacked = archive.Open();
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = unpacked.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }            
}

You may use Stream.CopyTo method for .NET 4.0 and higher: `unpacked.CopyTo(extracted);`

Remarks

Read from the stream to get the original content of a file. See examples section.

Exceptions

EndOfStreamException

Source stream is too short.

InvalidDataException

Wrong bytes found while initialize decoding.

InvalidOperationException

The archive is prepared for composition.

Save(Stream)

Saves lz4 archive to the stream provided.

public void Save(Stream output)

Parameters

output Stream

Destination stream.

Examples

using (FileStream lz4File = File.Open("archive.lz4", FileMode.Create))
{
    using (var archive = new Lz4Archive())
    {
        archive.SetSource("data.bin");
        archive.Save(lz4File);
     }
}

Remarks

output must be seekable.

Exceptions

ArgumentNullException

output is null.

ArgumentException

output is not writable.

InvalidOperationException

The archive is prepared for extraction. - or - Source was not supplied.

Save(FileInfo)

Saves lz4 archive to destination file provided.

public void Save(FileInfo destination)

Parameters

destination FileInfo

FileInfo, which will be opened as destination stream.

Examples

using (var archive = new Lz4Archive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save(new FileInfo("archive.lz4"));
}

Exceptions

SecurityException

The caller does not have the required permission to open the destination.

ArgumentException

The file path is empty or contains only white spaces.

FileNotFoundException

The file is not found.

UnauthorizedAccessException

Path to file is read-only or is a directory.

ArgumentNullException

destination is null.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The file is already open.

InvalidOperationException

The archive is prepared for extraction.

Save(string)

Saves archive to the destination file provided.

public void Save(string destinationFileName)

Parameters

destinationFileName string

The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.

Examples

using (var archive = new LZ4Archive())
{
    archive.SetSource("data.bin");
    archive.Save("archive.lz4");
}

Exceptions

ArgumentNullException

destinationFileName is null.

SecurityException

The caller does not have the required permission to access

ArgumentException

The destinationFileName is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file destinationFileName is denied.

PathTooLongException

The specified destinationFileName, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

NotSupportedException

File at destinationFileName contains a colon (:) in the middle of the string.

InvalidOperationException

The archive is prepared for extraction.

SetSource(Stream)

Sets the content to be compressed within the archive.

public void SetSource(Stream source)

Parameters

source Stream

The input stream for the archive.

Examples

using (var archive = new Lz4Archive())
{
    archive.SetSource(new MemoryStream(new byte[] { 0x00, 0xFF }));
    archive.Save("archive.lz4");
}

Exceptions

InvalidOperationException

The archive is prepared for extraction.

SetSource(FileInfo)

Sets the content to be compressed within the archive.

public void SetSource(FileInfo fileInfo)

Parameters

fileInfo FileInfo

The reference to a file to be compressed.

Examples

Open an archive from a stream and extract it to a MemoryStream

using (var archive = new Lz4Archive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save("archive.lz4");
}

Exceptions

InvalidOperationException

The archive is prepared for extraction.

SetSource(TarArchive, TarFormat)

Sets the content to be compressed within the archive.

public void SetSource(TarArchive tarArchive, TarFormat format = TarFormat.UsTar)

Parameters

tarArchive TarArchive

Tar archive to be compressed.

format TarFormat

Defines tar header format.

Examples

using (var tarArchive = new TarArchive())
{
    tarArchive.CreateEntry("first.bin", "data1.bin");
    tarArchive.CreateEntry("second.bin", "data2.bin");
    using (var lz4Archive = new Lz4Archive())
    {
        lz4Archive.SetSource(tarArchive);
        lz4Archive.Save("archive.tar.lz4");
    }
}

Remarks

Use this method to compose joint tar.lz4 archive.

SetSource(string)

Sets the content to be compressed within the archive.

public void SetSource(string path)

Parameters

path string

Path to file to be compressed.

Examples

Open an archive from file by path and extract it to a MemoryStream

using (var archive = new Lz4Archive()) 
{
    archive.SetSource("data.bin");
    archive.Save("archive.lz4");
}

Exceptions

ArgumentNullException

path is null.

SecurityException

The caller does not have the required permission to access

ArgumentException

The path is empty, contains only white spaces, or contains invalid characters.

UnauthorizedAccessException

Access to file path is denied.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

NotSupportedException

File at path contains a colon (:) in the middle of the string.

InvalidOperationException

This archive is prepared for extraction.

 English