Class SevenZipArchive

Class SevenZipArchive

Namespace: Aspose.Zip.SevenZip
Assembly: Aspose.Zip.dll (25.2.0)

This class represents 7z archive file. Use it to compose and extract 7z archives.

public class SevenZipArchive : IArchive, IDisposable

Inheritance

objectSevenZipArchive

Implements

IArchive, IDisposable

Inherited Members

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

Constructors

SevenZipArchive(SevenZipEntrySettings)

Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class with optional settings for its entries.

public SevenZipArchive(SevenZipEntrySettings newEntrySettings = null)

Parameters

newEntrySettings SevenZipEntrySettings

Compression and encryption settings used for newly added Aspose.Zip.SevenZip.SevenZipArchiveEntry items. If not specified, LZMA compression without encryption would be used.

Examples

The following example shows how to compress a single file with default settings: LZMA compression without encryption.

using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
{
    using (var archive = new SevenZipArchive())
    {
        archive.CreateEntry("data.bin", "file.dat");
        archive.Save(sevenZipFile);
    }
}

SevenZipArchive(Stream, string)

Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class and composes entries list can be extracted from the archive.

public SevenZipArchive(Stream sourceStream, string password = null)

Parameters

sourceStream Stream

The source of the archive.

password string

Optional password for decryption. If file names are encrypted it must be present.

Examples

using (SevenZipArchive archive = new SevenZipArchive(File.OpenRead("archive.7z")))
{
    archive.ExtractToDirectory("C:\\extracted");
}

Remarks

This constructor does not decompress any entry. See Aspose.Zip.SevenZip.SevenZipArchive.ExtractToDirectory(System.String,System.String) method for decompressing.

Exceptions

ArgumentException

sourceStream is not seekable.

ArgumentNullException

sourceStream is null.

NotImplementedException

Archive contains more than one coder. Now only LZMA compression supported.

SevenZipArchive(string, string)

Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class and composes entries list can be extracted from the archive.

public SevenZipArchive(string path, string password = null)

Parameters

path string

The fully qualified or the relative path to the archive file.

password string

Optional password for decryption. If file names are encrypted it must be present.

Examples

using (SevenZipArchive archive = new SevenZipArchive("archive.7z"))
{
    archive.ExtractToDirectory("C:\\extracted");
}

Remarks

This constructor does not decompress any entry. See Aspose.Zip.SevenZip.SevenZipArchive.ExtractToDirectory(System.String,System.String) 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.

FileNotFoundException

The file is not found.

DirectoryNotFoundException

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

IOException

The file is already open.

SevenZipArchive(string[], string)

Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class from multi-volume 7z archive and composes entries list can be extracted from the archive.

public SevenZipArchive(string[] parts, string password = null)

Parameters

parts string[]

Paths to each segment of multi-volume 7z archive respecting order

password string

Optional password for decryption. If file names are encrypted it must be present.

Examples

using (SevenZipArchive archive = new SevenZipArchive(new string[] { "multi.7z.001", "multi.7z.002", "multi.7z.003" } ))
{
    archive.ExtractToDirectory("C:\\extracted");
}

Exceptions

ArgumentNullException

parts is null.

ArgumentException

parts has no entries.

SecurityException

The caller does not have the required permission to access.

ArgumentException

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

UnauthorizedAccessException

Access to a file is denied.

PathTooLongException

The specified path to a part, 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.

Properties

Entries

Gets entries of Aspose.Zip.SevenZip.SevenZipArchiveEntry type constituting the archive.

public ReadOnlyCollection<sevenziparchiveentry> Entries { get; }

Property Value

ReadOnlyCollection<SevenZipArchiveEntry&gt;

NewEntrySettings

Compression and encryption settings used for newly added Aspose.Zip.SevenZip.SevenZipArchiveEntry items.

public SevenZipEntrySettings NewEntrySettings { get; }

Property Value

SevenZipEntrySettings

Methods

CreateEntries(DirectoryInfo, bool)

Adds to the archive all files and directories recursively in the directory given.

public SevenZipArchive CreateEntries(DirectoryInfo directory, bool includeRootDirectory = true)

Parameters

directory DirectoryInfo

Directory to compress.

includeRootDirectory bool

Indicates whether to include the root directory itself or not.

Returns

SevenZipArchive

The archive with entries composed.

Examples

using (SevenZipArchive archive = new SevenZipArchive())
{
    DirectoryInfo folder = new DirectoryInfo("C:\folder");
    archive.CreateEntries(folder);
    archive.Save("folder.7z");
}

Exceptions

DirectoryNotFoundException

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

SecurityException

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

CreateEntries(string, bool)

Adds to the archive all files and directories recursively in the directory given.

public SevenZipArchive CreateEntries(string sourceDirectory, bool includeRootDirectory = true)

Parameters

sourceDirectory string

Directory to compress.

includeRootDirectory bool

Indicates whether to include the root directory itself or not.

Returns

SevenZipArchive

The archive with entries composed.

Examples

Compose 7z archive with LZMA2 compression.

using (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings())))
{
    archive.CreateEntries("C:\folder");
    archive.Save("folder.7z");
}

CreateEntry(string, FileInfo, bool, SevenZipEntrySettings)

Create single entry within the archive.

public SevenZipArchiveEntry CreateEntry(string name, FileInfo fileInfo, bool openImmediately = false, SevenZipEntrySettings newEntrySettings = null)

Parameters

name string

The name of the entry.

fileInfo FileInfo

The metadata of file to be compressed.

openImmediately bool

True if open the file immediately, otherwise open the file on archive saving.

newEntrySettings SevenZipEntrySettings

Compression and encryption settings used for added Aspose.Zip.SevenZip.SevenZipArchiveEntry item.

Returns

SevenZipArchiveEntry

Seven Zip entry instance.

Examples

Compose archive with entries encrypted with different passwords each.

using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
{
    FileInfo fi1 = new FileInfo("data1.bin");
    FileInfo fi2 = new FileInfo("data2.bin");
    FileInfo fi3 = new FileInfo("data3.bin");
    using (var archive = new SevenZipArchive())
    {
        archive.CreateEntry("entry1.bin", fi1, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test1")));
        archive.CreateEntry("entry2.bin", fi2, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test2")));
        archive.CreateEntry("entry3.bin", fi3, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test3")));
        archive.Save(sevenZipFile);
    }
}

Remarks

The entry name is solely set within name parameter. The file name provided in fileInfo parameter does not affect the entry name.

If the file is opened immediately with openImmediately parameter it becomes blocked until archive is saved.

Exceptions

UnauthorizedAccessException

fileInfo is read-only or is a directory.

DirectoryNotFoundException

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

IOException

The file is already open.

CreateEntry(string, Stream, SevenZipEntrySettings, FileSystemInfo)

Create single entry within the archive.

public SevenZipArchiveEntry CreateEntry(string name, Stream source, SevenZipEntrySettings newEntrySettings, FileSystemInfo fileInfo)

Parameters

name string

The name of the entry.

source Stream

The input stream for the entry.

newEntrySettings SevenZipEntrySettings

Compression and encryption settings used for added Aspose.Zip.SevenZip.SevenZipArchiveEntry item.

fileInfo FileSystemInfo

The metadata of file or folder to be compressed.

Returns

SevenZipArchiveEntry

SevenZip entry instance.

Examples

Compose archive with LZMA2 compressed encrypted entry.

using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
{
    using (var archive = new SevenZipArchive())
    {
        archive.CreateEntry("entry1.bin", new MemoryStream(new byte[] {0x00, 0xFF}), new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings(), new SevenZipAESEncryptionSettings("test1")), new FileInfo("data1.bin")); 
        archive.Save(sevenZipFile);
    }
}

Remarks

The entry name is solely set within name parameter. The file name provided in fileInfo parameter does not affect the entry name.

fileInfo can refer to System.IO.DirectoryInfo if the entry is directory.

Exceptions

InvalidOperationException

Both source and fileInfo are null or source is null and fileInfo stands for directory.

CreateEntry(string, Stream, SevenZipEntrySettings)

Create single entry within the archive.

public SevenZipArchiveEntry CreateEntry(string name, Stream source, SevenZipEntrySettings newEntrySettings = null)

Parameters

name string

The name of the entry.

source Stream

The input stream for the entry.

newEntrySettings SevenZipEntrySettings

Compression and encryption settings used for added Aspose.Zip.SevenZip.SevenZipArchiveEntry item.

Returns

SevenZipArchiveEntry

Zip entry instance.

Examples

Compose 7z archive with LZMA2 compression and encryption of all entries.

using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings(), new SevenZipAESEncryptionSettings("p@s$"))))
{
    archive.CreateEntry("data.bin", new MemoryStream(new byte[] {0x00, 0xFF} ));
    archive.Save("archive.7z");
}

CreateEntry(string, string, bool, SevenZipEntrySettings)

Create single entry within the archive.

public SevenZipArchiveEntry CreateEntry(string name, string path, bool openImmediately = false, SevenZipEntrySettings newEntrySettings = null)

Parameters

name string

The name of the entry.

path string

The fully qualified name of the new file, or the relative file name to be compressed.

openImmediately bool

True if open the file immediately, otherwise open the file on archive saving.

newEntrySettings SevenZipEntrySettings

Compression and encryption settings used for added Aspose.Zip.SevenZip.SevenZipArchiveEntry item.

Returns

SevenZipArchiveEntry

Zip entry instance.

Examples

using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
{
    using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings())))
    {
        archive.CreateEntry("data.bin", "file.dat");
        archive.Save(sevenZipFile);
    }
}

Remarks

The entry name is solely set within name parameter. The file name provided in path parameter does not affect the entry name.

If the file is opened immediately with openImmediately parameter it becomes blocked until archive is saved.

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.

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.

ExtractToDirectory(string, string)

Extracts all the files in the archive to the directory provided.

public void ExtractToDirectory(string destinationDirectory, string password = null)

Parameters

destinationDirectory string

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

password string

Optional password for content decryption.

Examples

using (var archive = new SevenZipArchive("archive.7z")) 
{ 
   archive.ExtractToDirectory("C:\extracted");
}

Remarks

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

password is used for content decryption only. If file names are encrypted provide password in Aspose.Zip.SevenZip.SevenZipArchive.#ctor(System.String,System.String) or Aspose.Zip.SevenZip.SevenZipArchive.#ctor(System.IO.Stream,System.String) constructor.

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 existing directory.

NotSupportedException

If directory does not exist, 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.

InvalidDataException

Archive is corrupted.

Save(Stream)

Saves 7z archive to the stream provided.

public void Save(Stream output)

Parameters

output Stream

Destination stream.

Examples

using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
{
  using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
  {
    using (var archive = new SevenZipArchive())
    {
      archive.CreateEntry("data", source);
      archive.Save(sevenZipFile);
    }
  }
}

Remarks

output must be seekable.

Exceptions

ArgumentException

output does not support seeking.

ArgumentNullException

output is null.

InvalidOperationException

Encoder failed to compress data.

Save(string)

Saves archive to 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 (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
   using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings())))
   {
      archive.CreateEntry("data", source);
      archive.Save("archive.7z");
   }
}

Remarks

It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to temporary file.

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.

SaveSplit(string, SplitSevenZipArchiveSaveOptions)

Saves multi-volume archive to destination directory provided.

public void SaveSplit(string destinationDirectory, SplitSevenZipArchiveSaveOptions options)

Parameters

destinationDirectory string

The path to the directory where archive segments to be created.

options SplitSevenZipArchiveSaveOptions

Options for archive saving, including file name.

Examples

using (SevenZipArchive archive = new SevenZipArchive())
{
    archive.CreateEntry("entry.bin", "data.bin");
    archive.SaveSplit(@"C:\Folder",  new SplitSevenZipArchiveSaveOptions("volume", 65536));
}

Remarks

This method compose several (n) files filename.7z.001, filename.7z.002, ..., filename.7z.(n).

Can not make existing archive multi-volume.

Exceptions

InvalidOperationException

This archive was opened from existing source.

ArgumentNullException

destinationDirectory is null.

SecurityException

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

ArgumentException

destinationDirectory contains invalid characters such as “, >, <, or |.

PathTooLongException

The specified path exceeds the system-defined maximum length.

 English