7z archives
editRead an archive
SevenZipArchive opens a 7z archive from a seekable stream:
var path = Path.Combine(AppContext.BaseDirectory, "TestData", "SevenZip", "lzma2.7z");
using (var archive = new SevenZipArchive(File.OpenRead(path)))
{
var entry = archive.GetEntry("hello.txt");
Assert.NotNull(entry);
using var reader = new StreamReader(entry.Open());
Assert.StartsWith("Hello, 7z!", reader.ReadToEnd(), StringComparison.Ordinal);
}The entries of a solid block are compressed together. Reading them in the order of the archive decodes the block once; opening an entry before the last one read decodes the block again from its start. SevenZipFile.ExtractToDirectory reads them in order:
SevenZipFile.ExtractToDirectory(archivePath, destinationDirectory, overwriteFiles: true);
Assert.True(File.Exists(Path.Combine(destinationDirectory, "hello.txt")));SevenZipFile.OpenRead opens the volumes of a multi-volume archive when the path ends with .001.
Create an archive
In SevenZipArchiveMode.Create, each entry is compressed as it is written, and the headers are written when the archive is disposed. The stream must be seekable, and one entry can be written at a time:
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);
}SevenZipFile.CreateFromDirectory compresses a directory, with its empty directories, its symbolic links and the times and attributes of the files:
var options = new SevenZipCompressionOptions
{
Method = SevenZipCompressionMethod.Lzma2,
Level = 9,
};
SevenZipFile.CreateFromDirectory(sourceDirectory, archivePath, options);
using var archive = SevenZipFile.OpenRead(archivePath);
Assert.Equal("file.txt", archive.Entries[0].FullName);SevenZipCompressionOptions sets how the entries are compressed:
- Method: LZMA2 (the default), LZMA, PPMd (often the best for text), BZip2, Deflate, or Copy to store the files.
- Level: 0 to 9, as the
-mxoption of 7-Zip; 0 stores the files. DictionarySize replaces the size the level chooses. - Solid: compress the files together, which compresses better but makes reading one file slower. SolidBlockSize and SolidFileCount limit the blocks.
- Filter: the default, SevenZipFilter.Auto, applies the x86 BCJ filter to the executables (
.exe,.dll, ...), by their extension. Choose the filter of another processor, or None.
Protect an archive with a password
Password encrypts the content with AES-256, and EncryptHeaders also encrypts the names of the entries. Read the archive with ArchiveReaderOptions.Password:
using var stream = new MemoryStream();
var compressionOptions = new SevenZipCompressionOptions
{
Password = "secret",
EncryptHeaders = true, // Also encrypt the names of the entries
};
using (var archive = new SevenZipArchive(stream, SevenZipArchiveMode.Create, leaveOpen: true, readerOptions: null, compressionOptions))
{
using var writer = new StreamWriter(archive.CreateEntry("secret.txt").Open());
writer.Write("Top secret");
}
stream.Position = 0;
var readerOptions = new ArchiveReaderOptions { Password = "secret" };
using (var archive = new SevenZipArchive(stream, SevenZipArchiveMode.Read, leaveOpen: true, readerOptions, compressionOptions: null))
{
using var reader = new StreamReader(archive.Entries[0].Open());
Assert.Equal("Top secret", reader.ReadToEnd());
}Update an archive
In SevenZipArchiveMode.Update, add entries with CreateEntry, remove them with Delete, and change their times and attributes. The archive is rewritten when it is disposed: the solid blocks whose entries are all kept are copied as they are, and the entries of the other blocks are compressed again.