SevenZipArchive Class
A 7z archive, read, created or updated as ZipArchive. Reading supports every method of 7-Zip but the RAR ones (LZMA, LZMA2, PPMd, BZip2, Deflate, Deflate64, the branch filters, BCJ2, Delta, AES-256), solid archives, encrypted headers and multi-volume archives. Writing supports LZMA2, LZMA, PPMd, BZip2, Deflate and Copy, the branch filters, solid folders and AES-256.
public sealed class SevenZipArchive : ArchiveRemarks
An archive in SevenZipArchiveMode.Create mode streams the content of each entry, as it is written, into the current solid folder: one entry can be written at a time. The headers are written when the archive is disposed. An archive in SevenZipArchiveMode.Update mode is rewritten when it is disposed: the folders whose entries are all kept are copied without being decoded, the kept entries of the other folders are recompressed.
Reading the entries of a solid folder in order decodes the folder once. Opening an entry before the last one opened in the same folder decodes the folder again from its start.
The archive is read from the start of the stream. A new archive is written from the current position of the stream.
Example
using var stream = new MemoryStream();
using (var archive = new SevenZipArchive(stream, SevenZipArchiveMode.Create, leaveOpen: true))
{
// Each entry is compressed as it is written, into the current solid folder
var entry = archive.CreateEntry("docs/readme.txt");
using var writer = new StreamWriter(entry.Open());
writer.Write("Hello from 7z");
}
stream.Position = 0;
using (var archive = new SevenZipArchive(stream))
{
Assert.Equal("docs/readme.txt", archive.Entries[0].FullName);
}- Inheritance
ArchiveSevenZipArchive
Constructors
| Name | Description |
|---|---|
| SevenZipArchive(Stream) | Initializes a new instance of the SevenZipArchive class that reads an archive. |
| SevenZipArchive(Stream, SevenZipArchiveMode, bool) | Initializes a new instance of the SevenZipArchive class. |
| SevenZipArchive(Stream, SevenZipArchiveMode, bool, ArchiveReaderOptions?, SevenZipCompressionOptions?) | Initializes a new instance of the SevenZipArchive class. |
SevenZipArchive overloads
SevenZipArchive(Stream)
SevenZipArchive.csInitializes a new instance of the SevenZipArchive class that reads an archive.
public SevenZipArchive(Stream stream)Parameters
streamStream- The archive. It must be readable and seekable.
Exceptions
InvalidDataException- The archive is corrupted.
ArchivePasswordException- The headers are encrypted and the password is missing or wrong.
SevenZipArchive(Stream, SevenZipArchiveMode, bool)
SevenZipArchive.csInitializes a new instance of the SevenZipArchive class.
public SevenZipArchive(Stream stream, SevenZipArchiveMode mode, bool leaveOpen = false)Parameters
streamStream- The archive. It must be seekable, readable to read or update an archive, and writable to create or update one.
modeSevenZipArchiveMode- The mode of the archive.
leaveOpenbooltrueto leavestreamopen when the archive is disposed.
Exceptions
InvalidDataException- The archive is corrupted.
SevenZipArchive(Stream, SevenZipArchiveMode, bool, ArchiveReaderOptions?, SevenZipCompressionOptions?)
SevenZipArchive.csInitializes a new instance of the SevenZipArchive class.
public SevenZipArchive(Stream stream, SevenZipArchiveMode mode, bool leaveOpen, ArchiveReaderOptions? readerOptions, SevenZipCompressionOptions? compressionOptions)Parameters
streamStream- The archive. It must be seekable, readable to read or update an archive, and writable to create or update one.
modeSevenZipArchiveMode- The mode of the archive.
leaveOpenbooltrueto leavestreamopen when the archive is disposed.readerOptionsArchiveReaderOptions?- The options to read the archive: its password, the volumes of a multi-volume archive (
ArchiveReaderOptions.VolumeResolver), and the memory the decoders may allocate. compressionOptionsSevenZipCompressionOptions?- The options to write the archive: the method, the level, the password, ... In
SevenZipArchiveMode.Updatemode, they apply to the new entries, to the recompressed folders, and to the headers.
Exceptions
InvalidDataException- The archive is corrupted.
ArchivePasswordException- The headers are encrypted and the password is missing or wrong.
NotSupportedException- The archive uses a feature that is not supported.
Properties
| Name | Description |
|---|---|
| Entries | Gets the entries of the archive, in the order of the archive. The anti-items of 7-Zip (deletion markers) are not listed. |
| Format | Gets the format of the archive. |
| Mode | Gets the mode of the archive. |
Entries
SevenZipArchive.csGets the entries of the archive, in the order of the archive. The anti-items of 7-Zip (deletion markers) are not listed.
public new IReadOnlyList<SevenZipArchiveEntry> Entries { get; }Property value
Format
SevenZipArchive.csMethods
| Name | Description |
|---|---|
| CreateEntry(string) | Creates an empty entry. A name that ends with / creates a directory. Write the content of a file with SevenZipArchiveEntry.Open; an entry that is not opened is an empty file. |
| CreateEntryFromFile(string, string) | Creates an entry from a file: its content, its times, its attributes and, on Unix, its permissions. A symbolic link is stored as a link (its target), as the -snl switch of 7-Zip. |
| GetEntry(string) | Gets the entry whose full name is entryName (an ordinal comparison), or null when there is none. |
CreateEntry(string)
SevenZipArchive.csCreates an empty entry. A name that ends with / creates a directory. Write the content of a file with SevenZipArchiveEntry.Open; an entry that is not opened is an empty file.
public SevenZipArchiveEntry CreateEntry(string entryName)Parameters
entryNamestring- The path of the entry in the archive, with
/separators.
Returns
SevenZipArchiveEntry The new entry, whose last write time is the current time.
Exceptions
NotSupportedException- The archive is in
SevenZipArchiveMode.Readmode.
CreateEntryFromFile(string, string)
SevenZipArchive.csCreates an entry from a file: its content, its times, its attributes and, on Unix, its permissions. A symbolic link is stored as a link (its target), as the -snl switch of 7-Zip.
public SevenZipArchiveEntry CreateEntryFromFile(string sourceFileName, string entryName)Parameters
sourceFileNamestring- The path of the file.
entryNamestring- The path of the entry in the archive, with
/separators.
Returns
SevenZipArchiveEntry The new entry.
GetEntry(string)
SevenZipArchive.csGets the entry whose full name is entryName (an ordinal comparison), or null when there is none.
public new SevenZipArchiveEntry? GetEntry(string entryName)Parameters
entryNamestring- The path of the entry in the archive, with
/separators. The name of a directory ends with/.
Returns
SevenZipArchiveEntry? The first entry whose SevenZipArchiveEntry.FullName is entryName, or null.