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 : Archive

Remarks

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
Archive SevenZipArchive

Constructors

NameDescription
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.cs

Initializes a new instance of the SevenZipArchive class that reads an archive.

public SevenZipArchive(Stream stream)
Parameters
stream Stream
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.cs

Initializes a new instance of the SevenZipArchive class.

public SevenZipArchive(Stream stream, SevenZipArchiveMode mode, bool leaveOpen = false)
Parameters
stream Stream
The archive. It must be seekable, readable to read or update an archive, and writable to create or update one.
mode SevenZipArchiveMode
The mode of the archive.
leaveOpen bool
true to leave stream open when the archive is disposed.
Exceptions
InvalidDataException
The archive is corrupted.

SevenZipArchive(Stream, SevenZipArchiveMode, bool, ArchiveReaderOptions?, SevenZipCompressionOptions?)

SevenZipArchive.cs

Initializes a new instance of the SevenZipArchive class.

public SevenZipArchive(Stream stream, SevenZipArchiveMode mode, bool leaveOpen, ArchiveReaderOptions? readerOptions, SevenZipCompressionOptions? compressionOptions)
Parameters
stream Stream
The archive. It must be seekable, readable to read or update an archive, and writable to create or update one.
mode SevenZipArchiveMode
The mode of the archive.
leaveOpen bool
true to leave stream open when the archive is disposed.
readerOptions ArchiveReaderOptions?
The options to read the archive: its password, the volumes of a multi-volume archive (ArchiveReaderOptions.VolumeResolver), and the memory the decoders may allocate.
compressionOptions SevenZipCompressionOptions?
The options to write the archive: the method, the level, the password, ... In SevenZipArchiveMode.Update mode, 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

NameDescription
EntriesGets the entries of the archive, in the order of the archive. The anti-items of 7-Zip (deletion markers) are not listed.
FormatGets the format of the archive.
ModeGets the mode of the archive.

Gets 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; }

Gets the format of the archive.

public override ArchiveFormat Format { get; }

Property value

ArchiveFormat

Gets the mode of the archive.

public SevenZipArchiveMode Mode { get; }

Property value

SevenZipArchiveMode

Methods

NameDescription
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.cs

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.

public SevenZipArchiveEntry CreateEntry(string entryName)

Parameters

entryName string
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.Read mode.

CreateEntryFromFile(string, string)

SevenZipArchive.cs

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.

public SevenZipArchiveEntry CreateEntryFromFile(string sourceFileName, string entryName)

Parameters

sourceFileName string
The path of the file.
entryName string
The path of the entry in the archive, with / separators.

Returns

SevenZipArchiveEntry The new entry.

GetEntry(string)

SevenZipArchive.cs

Gets the entry whose full name is entryName (an ordinal comparison), or null when there is none.

public new SevenZipArchiveEntry? GetEntry(string entryName)

Parameters

entryName string
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.