Class SevenZipArchive
Namespace: Aspose.Zip.SevenZip
Assembly: Aspose.Zip.dll (25.8.0)
This class represents 7z archive file. Use it to compose and extract 7z archives.
public class SevenZipArchive : IArchive, IDisposableInheritance
Implements
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 an entry 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
sourceStream is not seekable.
sourceStream is null.
The 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 an entry 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
path is null.
The caller does not have the required permission to access.
The path is empty, contains only white spaces, or contains invalid characters.
Access to file path is denied.
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.
File at path contains a colon (:) in the middle of the string.
The file is not found.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
SevenZipArchive(Stream, SevenZipLoadOptions)
Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class and composes an entry list can be extracted from the archive.
public SevenZipArchive(Stream sourceStream, SevenZipLoadOptions options)Parameters
sourceStream Stream
The source of the archive.
options SevenZipLoadOptions
Options to load existing archive with.
Examples
Extract an encrypted archive. Allow up to 60 seconds to proceed, cancel after that period.
using(CancellationTokenSource cts = new CancellationTokenSource())
{
SevenZipLoadOptions options = new SevenZipLoadOptions(){ DecryptionPassword = "Top$ecr3t", CancellationToken = cts.Token }
cts.CancelAfter(TimeSpan.FromSeconds(60));
using (SevenZipArchive archive = new SevenZipArchive(File.OpenRead("archive.7z"), options))
{
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
sourceStream is not seekable.
sourceStream is null.
The archive contains more than one coder. Now only LZMA compression supported.
SevenZipArchive(string, SevenZipLoadOptions)
Initializes a new instance of the Aspose.Zip.SevenZip.SevenZipArchive class and composes an entry list can be extracted from the archive.
public SevenZipArchive(string path, SevenZipLoadOptions options)Parameters
path string
The fully qualified or the relative path to the archive file.
options SevenZipLoadOptions
Options to load existing archive with.
Examples
Extract an encrypted archive. Allow up to 60 seconds to proceed, cancel after that period.
using(CancellationTokenSource cts = new CancellationTokenSource())
{
SevenZipLoadOptions options = new SevenZipLoadOptions(){ DecryptionPassword = "Top$ecr3t", CancellationToken = cts.Token }
cts.CancelAfter(TimeSpan.FromSeconds(60));
using (SevenZipArchive archive = new SevenZipArchive(File.OpenRead("archive.7z"), options))
{
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
path is null.
The caller does not have the required permission to access.
The path is empty, contains only white spaces, or contains invalid characters.
Access to file path is denied.
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.
File at path contains a colon (:) in the middle of the string.
The file is not found.
The specified path is invalid, such as being on an unmapped drive.
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 an entry 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
parts is null.
parts has no entries.
The caller does not have the required permission to access.
The path to a file is empty, contains only white spaces, or contains invalid characters.
Access to a file is denied.
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.
File at a 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 >
NewEntrySettings
Compression and encryption settings used for newly added Aspose.Zip.SevenZip.SevenZipArchiveEntry items.
public SevenZipEntrySettings NewEntrySettings { get; }Property Value
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
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
The path to directory is invalid, such as being on an unmapped drive.
Archive has been disposed and cannot be used.
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
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");
}Exceptions
Archive has been disposed and cannot be used.
CreateEntry(string, FileInfo, bool, SevenZipEntrySettings)
Create a 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. Individual compression settings is ignored in case of solid compression, see Aspose.Zip.Saving.SevenZipEntrySettings.Solid.
Returns
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
fileInfo is read-only or is a directory.
The specified path is invalid, such as being on an unmapped drive.
The file is already open.
Archive has been disposed and cannot be used.
CreateEntry(string, Stream, SevenZipEntrySettings, FileSystemInfo)
Create a 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. Individual compression settings is ignored in case of solid compression, see Aspose.Zip.Saving.SevenZipEntrySettings.Solid.
fileInfo FileSystemInfo
The metadata of file or folder to be compressed.
Returns
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
Both source and fileInfo are null or source is null and fileInfo stands for directory.
Archive has been disposed and cannot be used.
CreateEntry(string, Stream, SevenZipEntrySettings)
Create a 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. Individual compression settings is ignored in case of solid compression, see Aspose.Zip.Saving.SevenZipEntrySettings.Solid.
Returns
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");
}Exceptions
Archive has been disposed and cannot be used.
CreateEntry(string, string, bool, SevenZipEntrySettings)
Create a 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. Individual compression settings is ignored in case of solid compression, see Aspose.Zip.Saving.SevenZipEntrySettings.Solid.
Returns
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
path is null.
The caller does not have the required permission to access.
The path is empty, contains only white spaces, or contains invalid characters.
Access to file path is denied.
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.
Archive has been disposed and cannot be used.
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), Aspose.Zip.SevenZip.SevenZipArchive.#ctor(System.IO.Stream,System.String), Aspose.Zip.SevenZip.SevenZipArchive.#ctor(System.IO.Stream,Aspose.Zip.SevenZip.SevenZipLoadOptions) or Aspose.Zip.SevenZip.SevenZipArchive.#ctor(System.IO.Stream,Aspose.Zip.SevenZip.SevenZipLoadOptions) constructor.
Exceptions
destinationDirectory is null.
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.
The caller does not have the required permission to access the existing directory.
If the directory does not exist, the path contains a colon character (:) that is not part of a drive label (“C:").
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 (:).
The directory specified by path is a file. -or- The network name is not known.
The archive is corrupted.
Archive has been disposed and cannot be used.
In .NET Framework 4.0 and above: Thrown when the extraction is canceled via the provided cancellation token.
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
output does not support seeking.
output is null.
Encoder failed to compress data.
Archive has been disposed and cannot be used.
Save(string)
Saves archive to a 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 a temporary file.
Exceptions
destinationFileName is null.
The caller does not have the required permission to access.
The destinationFileName is empty, contains only white spaces, or contains invalid characters.
Access to file destinationFileName is denied.
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.
File at destinationFileName contains a colon (:) in the middle of the string.
Archive has been disposed and cannot be used.
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 composes several (n) files filename.7z.001, filename.7z.002, ..., filename.7z.(n).
Cannot make existing archive multi-volume.
Exceptions
This archive was opened from the existing source.
destinationDirectory is null.
The caller does not have the required permission to access the directory.
destinationDirectory contains invalid characters such as “, >, <, or |.
The specified path exceeds the system-defined maximum length.
Archive has been disposed and cannot be used.