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.
1try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) {
2 try (SevenZipArchive archive = new SevenZipArchive()) {
3 archive.createEntry("data.bin", "file.dat");
4 archive.save(sevenZipFile);
5 }
6} catch (IOException ex) {
7}
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.
1try (SevenZipArchive archive = new SevenZipArchive()) {
2 archive.createEntries("dataDir");
3 archive.save("SevenZip.7z");
4}
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.
1try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(null, new SevenZipAESEncryptionSettings("p@s$")))) {
2 archive.createEntry("data.bin", new ByteArrayInputStream(new byte[] {0x00, (byte)0xFF} ));
3 archive.save("archive.7z");
4}
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.
1try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) {
2 try (SevenZipArchive archive = new SevenZipArchive()) {
3 archive.createEntry("entry1.bin",
4 new ByteArrayInputStream(new byte[] {0x00, (byte)0xFF}),
5 new SevenZipEntrySettings(new SevenZipLZMACompressionSettings(),
6 new SevenZipAESEncryptionSettings("test1")),
7 new File("data1.bin"));
8 archive.save(sevenZipFile);
9 }
10} catch (IOException ex) {
11}
Similarly you can compose 7z archive with BZip2, PPMd and LZMA2 compression method, or store files without compression.
Setting Different Password for Entries
The following code example demonstrates how to create an archive with entries encrypted with different passwords for each entry.
1try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) {
2 File fi1 = new File("data1.bin");
3 File fi2 = new File("data2.bin");
4 File fi3 = new File("data3.bin");
5
6 try (SevenZipArchive archive = new SevenZipArchive()) {
7 archive.createEntry("entry1.bin", fi1, false,
8 new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(),
9 new SevenZipAESEncryptionSettings("test1")));
10 archive.createEntry("entry2.bin", fi2, false,
11 new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(),
12 new SevenZipAESEncryptionSettings("test2")));
13 archive.createEntry("entry3.bin", fi3, false,
14 new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(),
15 new SevenZipAESEncryptionSettings("test3")));
16 archive.save(sevenZipFile);
17 }
18} catch (IOException ex) {
19}
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.
1try (SevenZipArchive archive = new SevenZipArchive("archive.7z")) {
2 archive.extractToDirectory("ExtractionFolder");
3}