Working with 7z Archives
Overview
7-Zip is a file archiver with a high compression ratio. Aspose.ZIP API lets work with creating and managing 7-Zip archives in your applications without the need of any other 3rd party applications. Aspose.ZIP API provides SevenZipArchive class to work with 7-Zip archives. This class provides various methods to perform operations on archives. API provides the SevenZipArchiveEntry class to represents a single file within the 7z archive.
Create a 7-Zip Single Entry
The following code example demonstrates how to create a 7-Zip entry using SevenZipArchive instance.
1 using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
2 {
3 using (var archive = new SevenZipArchive())
4 {
5 archive.CreateEntry("data.bin", "file.dat");
6 archive.Save(sevenZipFile);
7 }
8 }
Create 7-Zip Archive Entries
The SevenZipArchive class provides CreateEntries methods to add files and directories recursively in the given directory given. The following code example demonstrates how to create 7-Zip archive entries.
1 using (SevenZipArchive archive = new SevenZipArchive())
2 {
3 archive.CreateEntries(dataDir);
4 archive.Save("SevenZip.7z");
5 }
7-Zip Encryption Settings
Aspose.ZIP API provides SevenZipAESEncryptionSettings class which provides Settings for AES encryption or decryption for 7z archives. The following code example demonstrates how to provide AES Encryption Settings.
1 using (var archive = new SevenZipArchive(new SevenZipEntrySettings(null, new SevenZipAESEncryptionSettings("p@s$"))))
2 {
3 archive.CreateEntry("data.bin", new MemoryStream(new byte[] { 0x00, 0xFF }));
4 archive.Save("archive.7z");
5 }
The AES-256 is the only possible encryption method for the 7z archive.
7-Zip Archive with LZMA Compression
The following code example demonstrates how to create a 7z archive with LZMA compression and AES encryption.
Steps: Create a 7z Archive with LZMA Compression and AES Encryption via C#
- Open a file stream for the output 7z archive using FileMode.Create.
- Create an instance of the SevenZipArchive class.
- Use the CreateEntry method to add an entry to the archive, specifying:
- The file to be compressed.
- LZMA compression settings using SevenZipLZMACompressionSettings.
- AES encryption settings using SevenZipAESEncryptionSettings (with a password).
- Save the archive using the Save method to write the compressed and encrypted data to the file.
1 using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
2 {
3 using (var archive = new SevenZipArchive())
4 {
5 archive.CreateEntry("entry1.bin",
6 new MemoryStream(new byte[] { 0x00, 0xFF }),
7 new SevenZipEntrySettings(new SevenZipLZMACompressionSettings(),
8 new SevenZipAESEncryptionSettings("test1")),
9 new FileInfo("data1.bin"));
10 archive.Save(sevenZipFile);
11 }
12 }
Similarly you can compose 7z archive with BZip2, PPMd and LZMA2 compression method, or store files without compression.
Solid 7-Zip Archive Composition
Solid compressed archives benefit greatly from the data with high redundancy. It treats all entries as a sinlge data stream. The following code example demonstrates how to create a solid archive from a directory with LZMA2 compression.
1 using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings()){ Solid = true }))
2 {
3 archive.CreateEntries(dataDir);
4 archive.Save("Solid.7z");
5 }
Setting Different Password for Entries
The following code example demonstrates how to create an archive with entries encrypted with different passwords for each entry.
Steps: Create a 7z Archive with LZMA Compression and AES Encryption via C#
- Open a file stream to create the 7z archive using FileMode.Create.
- Prepare the files (FileInfo objects) that you want to add as entries to the archive.
- Create an instance of the SevenZipArchive class.
- For each entry, use the CreateEntry method to add the file to the archive:
- Specify the compression method (in this case, SevenZipStoreCompressionSettings).
- Set a different password for each entry using SevenZipAESEncryptionSettings.
- Save the archive using the Save method to write the encrypted entries to the file.
1 using (FileStream sevenZipFile = File.Open("archive.7z", FileMode.Create))
2 {
3 FileInfo fi1 = new FileInfo("data1.bin");
4 FileInfo fi2 = new FileInfo("data2.bin");
5 FileInfo fi3 = new FileInfo("data3.bin");
6 using (var archive = new SevenZipArchive())
7 {
8 archive.CreateEntry("entry1.bin", fi1, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test1")));
9 archive.CreateEntry("entry2.bin", fi2, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test2")));
10 archive.CreateEntry("entry3.bin", fi3, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test3")));
11 archive.Save(sevenZipFile);
12 }
13 }
Extraction of 7z archives
Now Aspose.ZIP can extract LZMA, LZMA2, BZip2 and PPMd compressed archives.
The following code example demonstrates how to extract 7z archive to directory.
1 using (SevenZipArchive archive = new SevenZipArchive("archive.7z"))
2 {
3 archive.ExtractToDirectory("ExtractionFolder");
4 }