Class SharpSevenZipExtractor
Class to unpack data from archives supported by 7-Zip.
Implements
Inherited Members
Namespace: SharpSevenZip
Assembly: SharpSevenZip.dll
Syntax
public sealed class SharpSevenZipExtractor : SharpSevenZipBase, IDisposable
Examples
using (var extr = new SharpSevenZipExtractor(@"C:\Test.7z")) { extr.ExtractArchive(@"C:\TestDirectory"); }
Constructors
| Edit this page View SourceSharpSevenZipExtractor(Stream)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. Use SharpSevenZipExtractor(string) to extract from disk, though it is not necessary. |
Remarks
The archive format is guessed by the signature.
SharpSevenZipExtractor(Stream, bool)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream, bool leaveOpen)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. Use SharpSevenZipExtractor(string) to extract from disk, though it is not necessary. |
| bool | leaveOpen | Leaves the base stream open. |
Remarks
The archive format is guessed by the signature.
SharpSevenZipExtractor(Stream, bool, InArchiveFormat)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream, bool leaveOpen, InArchiveFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. Use SharpSevenZipExtractor(string) to extract from disk, though it is not necessary. |
| bool | leaveOpen | Leaves the base stream open. |
| InArchiveFormat | format | Manual archive format setup. You SHOULD NOT normally specify it this way. Instead, use SharpSevenZipExtractor(Stream archiveStream), that constructor automatically detects the archive format. |
SharpSevenZipExtractor(Stream, string)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream, string password)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. |
| string | password | Password for an encrypted archive. |
Remarks
The archive format is guessed by the signature.
SharpSevenZipExtractor(Stream, string, bool)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream, string password, bool leaveOpen)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. |
| string | password | Password for an encrypted archive. |
| bool | leaveOpen | Leaves the base stream open. |
Remarks
The archive format is guessed by the signature.
SharpSevenZipExtractor(Stream, string, bool, InArchiveFormat)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(Stream archiveStream, string password, bool leaveOpen, InArchiveFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | archiveStream | The stream to read the archive from. |
| string | password | Password for an encrypted archive. |
| bool | leaveOpen | Leaves the base stream open. |
| InArchiveFormat | format | Manual archive format setup. You SHOULD NOT normally specify it this way. Instead, use SharpSevenZipExtractor(Stream archiveStream, string password), that constructor automatically detects the archive format. |
SharpSevenZipExtractor(string)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(string archiveFullName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
SharpSevenZipExtractor(string, ArchiveFormatInfo)
Initializes a new instance of SharpSevenZipExtractor class from an already probed format.
Declaration
public SharpSevenZipExtractor(string archiveFullName, ArchiveFormatInfo formatInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
| ArchiveFormatInfo | formatInfo | The detection result obtained from TryCheckFormat(string, out ArchiveFormatInfo). Reusing it avoids re-reading the archive signature. A None format falls back to automatic detection. |
SharpSevenZipExtractor(string, InArchiveFormat)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(string archiveFullName, InArchiveFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
| InArchiveFormat | format | Manual archive format setup. You SHOULD NOT normally specify it this way. Instead, use SharpSevenZipExtractor(string archiveFullName), that constructor automatically detects the archive format. |
SharpSevenZipExtractor(string, string)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(string archiveFullName, string password)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
| string | password | Password for an encrypted archive. |
SharpSevenZipExtractor(string, string, ArchiveFormatInfo)
Initializes a new instance of SharpSevenZipExtractor class from an already probed format.
Declaration
public SharpSevenZipExtractor(string archiveFullName, string password, ArchiveFormatInfo formatInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
| string | password | Password for an encrypted archive. |
| ArchiveFormatInfo | formatInfo | The detection result obtained from TryCheckFormat(string, out ArchiveFormatInfo). Reusing it avoids re-reading the archive signature on every password attempt. A None format falls back to automatic detection. |
SharpSevenZipExtractor(string, string, InArchiveFormat)
Initializes a new instance of SharpSevenZipExtractor class.
Declaration
public SharpSevenZipExtractor(string archiveFullName, string password, InArchiveFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| string | archiveFullName | The archive full file name. |
| string | password | Password for an encrypted archive. |
| InArchiveFormat | format | Manual archive format setup. You SHOULD NOT normally specify it this way. Instead, use SharpSevenZipExtractor(string archiveFullName, string password), that constructor automatically detects the archive format. |
Properties
| Edit this page View SourceArchiveFileData
Gets the collection of ArchiveFileInfo with all information about files in the archive
Declaration
public ReadOnlyCollection<ArchiveFileInfo> ArchiveFileData { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyCollection<ArchiveFileInfo> |
ArchiveFileNames
Gets the collection of all file names contained in the archive.
Declaration
public ReadOnlyCollection<string> ArchiveFileNames { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyCollection<string> |
Remarks
Each get recreates the collection
ArchiveProperties
Gets the properties for the current archive
Declaration
public ReadOnlyCollection<ArchiveProperty> ArchiveProperties { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyCollection<ArchiveProperty> |
ErrorFlags
Gets the problems 7-Zip reported for the archive. Anything other than None means the archive cannot be extracted in full, even when extraction itself reports no failure. UnexpectedEnd is also raised when the physical size runs past the stream, which is where 7-Zip's own tools derive it from.
Declaration
public ArchiveErrorFlags ErrorFlags { get; }
Property Value
| Type | Description |
|---|---|
| ArchiveErrorFlags |
ErrorMessage
Gets the error message the format handler supplied, if any.
Declaration
public string? ErrorMessage { get; }
Property Value
| Type | Description |
|---|---|
| string |
FileName
Gets or sets archive full file name
Declaration
public string? FileName { get; }
Property Value
| Type | Description |
|---|---|
| string |
FilesCount
Gets the number of files in the archive
Declaration
[CLSCompliant(false)]
public uint FilesCount { get; }
Property Value
| Type | Description |
|---|---|
| uint |
Format
Gets archive format
Declaration
public InArchiveFormat Format { get; }
Property Value
| Type | Description |
|---|---|
| InArchiveFormat |
HasDataAfterEnd
Gets a value indicating whether the archive declared more packed data than the decoder consumed. The extracted content itself passed its CRC check.
Declaration
public bool HasDataAfterEnd { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsSolid
Gets a value indicating whether the archive is solid
Declaration
public bool IsSolid { get; }
Property Value
| Type | Description |
|---|---|
| bool |
PackedSize
Gets the size of the archive file
Declaration
public long PackedSize { get; }
Property Value
| Type | Description |
|---|---|
| long |
PreserveDirectoryStructure
Gets or sets the value indicating whether to preserve the directory structure of extracted files.
Declaration
public bool PreserveDirectoryStructure { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
PropagateMarkOfTheWeb
Gets or sets a value indicating whether extracted files inherit the archive's
Mark-of-the-Web. The default is true.
Declaration
public bool PropagateMarkOfTheWeb { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
UnpackedSize
Gets the size of unpacked archive data
Declaration
public long UnpackedSize { get; }
Property Value
| Type | Description |
|---|---|
| long |
VolumeFileNames
Gets the list of archive volume file names.
Declaration
public ReadOnlyCollection<string> VolumeFileNames { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyCollection<string> |
WarningFlags
Gets the non-fatal problems 7-Zip reported for the archive. DataAfterEnd is measured against the physical size rather than taken from a handler, which is where 7-Zip's own tools get it from.
Declaration
public ArchiveErrorFlags WarningFlags { get; }
Property Value
| Type | Description |
|---|---|
| ArchiveErrorFlags |
WarningMessage
Gets the warning message the format handler supplied, if any.
Declaration
public string? WarningMessage { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
| Edit this page View SourceBeginExtractArchive(string)
Unpacks the whole archive asynchronously to the specified directory name at the specified priority.
Declaration
public void BeginExtractArchive(string directory)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | The directory where the files are to be unpacked. |
BeginExtractFile(int, Stream)
Unpacks the file asynchronously by its index to the specified stream.
Declaration
public void BeginExtractFile(int index, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
BeginExtractFile(string, Stream)
Unpacks the file asynchronously by its name to the specified stream.
Declaration
public void BeginExtractFile(string fileName, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | The file full name in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
BeginExtractFiles(ExtractFileCallback)
Extracts files from the archive asynchronously, giving a callback the choice what to do with each file. The order of the files is given by the archive. 7-Zip (and any other solid) archives are NOT supported.
Declaration
public void BeginExtractFiles(ExtractFileCallback extractFileCallback)
Parameters
| Type | Name | Description |
|---|---|---|
| ExtractFileCallback | extractFileCallback | The callback to call for each file in the archive. |
BeginExtractFiles(string, params int[])
Unpacks files asynchronously by their indices to the specified directory.
Declaration
public void BeginExtractFiles(string directory, params int[] indexes)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| int[] | indexes | indexes of the files in the archive file table. |
BeginExtractFiles(string, params string[])
Unpacks files asynchronously by their full names to the specified directory.
Declaration
public void BeginExtractFiles(string directory, params string[] fileNames)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| string[] | fileNames | Full file names in the archive file table. |
Check()
Performs the archive integrity test.
Declaration
public bool Check()
Returns
| Type | Description |
|---|---|
| bool | True is the archive is ok; otherwise, false. |
DecompressStream(Stream, Stream, int?, EventHandler<ProgressEventArgs>)
Decompress the specified stream (C# inside)
Declaration
public static void DecompressStream(Stream inStream, Stream outStream, int? inLength, EventHandler<ProgressEventArgs> codeProgressEvent)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | inStream | The source compressed stream |
| Stream | outStream | The destination uncompressed stream |
| int? | inLength | The length of compressed data (null for inStream.Length) |
| EventHandler<ProgressEventArgs> | codeProgressEvent | The event for handling the code progress |
Dispose()
Releases the unmanaged resources used by SharpSevenZipExtractor.
Declaration
public void Dispose()
ExtractArchive(string)
Unpacks the whole archive to the specified directory.
Declaration
public void ExtractArchive(string directory)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | The directory where the files are to be unpacked. |
ExtractArchiveAsync(string)
Unpacks the whole archive asynchronously to the specified directory name at the specified priority.
Declaration
public Task ExtractArchiveAsync(string directory)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | The directory where the files are to be unpacked. |
Returns
| Type | Description |
|---|---|
| Task |
ExtractBytes(byte[])
Decompress byte array compressed with LZMA algorithm (C# inside)
Declaration
public static byte[] ExtractBytes(byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | Byte array to decompress |
Returns
| Type | Description |
|---|---|
| byte[] | Decompressed byte array |
ExtractFile(int, Stream)
Unpacks the file by its index to the specified stream.
Declaration
public void ExtractFile(int index, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
ExtractFile(string, Stream)
Unpacks the file by its name to the specified stream.
Declaration
public void ExtractFile(string fileName, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | The file full name in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
ExtractFileAsync(int, Stream)
Unpacks the file asynchronously by its name to the specified stream.
Declaration
public Task ExtractFileAsync(int index, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
Returns
| Type | Description |
|---|---|
| Task |
ExtractFileAsync(string, Stream)
Unpacks the file asynchronously by its name to the specified stream.
Declaration
public Task ExtractFileAsync(string fileName, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | The file full name in the archive file table. |
| Stream | stream | The stream where the file is to be unpacked. |
Returns
| Type | Description |
|---|---|
| Task |
ExtractFiles(ExtractFileCallback)
Extracts files from the archive, giving a callback the choice what to do with each file. The order of the files is given by the archive. 7-Zip (and any other solid) archives are NOT supported.
Declaration
public void ExtractFiles(ExtractFileCallback extractFileCallback)
Parameters
| Type | Name | Description |
|---|---|---|
| ExtractFileCallback | extractFileCallback | The callback to call for each file in the archive. |
Exceptions
| Type | Condition |
|---|---|
| SharpSevenZipExtractionFailedException | Thrown when trying to extract from solid archives. |
ExtractFiles(string, params int[])
Unpacks files by their indices to the specified directory.
Declaration
public void ExtractFiles(string directory, params int[] indexes)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| int[] | indexes | indexes of the files in the archive file table. |
ExtractFiles(string, params string[])
Unpacks files by their full names to the specified directory.
Declaration
public void ExtractFiles(string directory, params string[] fileNames)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| string[] | fileNames | Full file names in the archive file table. |
ExtractFilesAsync(ExtractFileCallback)
Extracts files from the archive asynchronously, giving a callback the choice what to do with each file. The order of the files is given by the archive. 7-Zip (and any other solid) archives are NOT supported.
Declaration
public Task ExtractFilesAsync(ExtractFileCallback extractFileCallback)
Parameters
| Type | Name | Description |
|---|---|---|
| ExtractFileCallback | extractFileCallback | The callback to call for each file in the archive. |
Returns
| Type | Description |
|---|---|
| Task |
ExtractFilesAsync(string, params int[])
Unpacks files asynchronously by their indices to the specified directory.
Declaration
public Task ExtractFilesAsync(string directory, params int[] indexes)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| int[] | indexes | indexes of the files in the archive file table. |
Returns
| Type | Description |
|---|---|
| Task |
ExtractFilesAsync(string, params string[])
Unpacks files asynchronously by their full names to the specified directory.
Declaration
public Task ExtractFilesAsync(string directory, params string[] fileNames)
Parameters
| Type | Name | Description |
|---|---|---|
| string | directory | Directory where the files are to be unpacked. |
| string[] | fileNames | Full file names in the archive file table. |
Returns
| Type | Description |
|---|---|
| Task |
OpenFileStream(int)
Unpacks the file by its index to the specified stream.
Declaration
public Stream OpenFileStream(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index in the archive file table. |
Returns
| Type | Description |
|---|---|
| Stream | The stream where the file is to be unpacked. |
Events
| Edit this page View SourceExtracting
Occurs when data are being extracted.
Declaration
public event EventHandler<ProgressEventArgs>? Extracting
Event Type
| Type | Description |
|---|---|
| EventHandler<ProgressEventArgs> |
Remarks
Use this event for accurate progress handling and various ProgressBar.StepBy(e.PercentDelta) routines.
ExtractionFinished
Occurs when the archive has been unpacked.
Declaration
public event EventHandler<EventArgs>? ExtractionFinished
Event Type
| Type | Description |
|---|---|
| EventHandler<EventArgs> |
FileExists
Occurs during the extraction when a file already exists.
Declaration
public event EventHandler<FileOverwriteEventArgs>? FileExists
Event Type
| Type | Description |
|---|---|
| EventHandler<FileOverwriteEventArgs> |
FileExtractionFinished
Occurs when a file has been successfully unpacked.
Declaration
public event EventHandler<FileInfoEventArgs>? FileExtractionFinished
Event Type
| Type | Description |
|---|---|
| EventHandler<FileInfoEventArgs> |
FileExtractionStarted
Occurs when a new file is going to be unpacked.
Declaration
public event EventHandler<FileInfoEventArgs>? FileExtractionStarted
Event Type
| Type | Description |
|---|---|
| EventHandler<FileInfoEventArgs> |
Remarks
Occurs when 7-zip engine requests for an output stream for a new file to unpack in.