Password Protect Zip Files and Archives in C#
Contents
[
Hide
Show
]Aspose.ZIP API lets you compress and decompress files in C# or any .NET langauge without worrying about the underlying file structure. This article shows working with single as well as multiple files compression.
Password Protection with Traditional Encryption
Encrypt Files with Traditional Encryption Scheme
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "CompressWithTraditionalEncryption_out.zip", FileMode.Create))
3{
4 using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
5 {
6 var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$")));
7 archive.CreateEntry("alice29.txt", source1);
8 archive.Save(zipFile);
9 }
10}
Encrypt Files with AES Encryption
Encryption of Files with AES128
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES128_out.zip", FileMode.Create))
3{
4 using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
5 {
6 using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES128))))
7 {
8 archive.CreateEntry("alice29.txt", source1);
9 archive.Save(zipFile);
10 }
11 }
12}
Encryption of Files with AES192
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES192_out.zip", FileMode.Create))
3{
4 using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
5 {
6 using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES192))))
7 {
8 archive.CreateEntry("alice29.txt", source1);
9 archive.Save(zipFile);
10 }
11 }
12}
Encryption of Files with AES256
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES256_out.zip", FileMode.Create))
3{
4 using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
5 {
6 using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES256))))
7 {
8 archive.CreateEntry("alice29.txt", source1);
9 archive.Save(zipFile);
10 }
11 }
12}
Password Protect Directory
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(".\\all_corpus_encrypted_out.zip", FileMode.Create))
3{
4 DirectoryInfo corpus = new DirectoryInfo(".\\CanterburyCorpus");
5 using (var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$"))))
6 {
7 archive.CreateEntries(corpus);
8 archive.Save(zipFile);
Encrypt Multiple Files with Mixed Encryption Techniques
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "CompressWithTraditionalEncryption_out.zip", FileMode.Create))
3{
4 using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
5 {
6 var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$")));
7 archive.CreateEntry("alice29.txt", source1);
8 archive.Save(zipFile);
9 }
10}
Decompression of Password Protected Files
Decompression of Traditionally Password Protected Archives
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream fs = File.OpenRead(dataDir + "CompressWithTraditionalEncryption_out.zip"))
3{
4 using (var extracted = File.Create(dataDir + "alice_extracted_out.txt"))
5 {
6 using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
7 {
8 using (var decompressed = archive.Entries[0].Open())
9 {
10 byte[] b = new byte[8192];
11 int bytesRead;
12 while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
13 {
14 extracted.Write(b, 0, bytesRead);
15 }
16 }
17 }
18 }
19}
Decompress AES Encrypted Archives
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream fs = File.OpenRead(dataDir + "PasswordProtectWithAES256_out.zip"))
3{
4 using (var extracted = File.Create(dataDir + "alice_aesextracted_out.txt"))
5 {
6 using (Archive archive = new Archive(fs))
7 {
8 using (var decompressed = archive.Entries[0].Open("p@s$"))
9 {
10 byte[] b = new byte[8192];
11 int bytesRead;
12 while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
13 {
14 extracted.Write(b, 0, bytesRead);
15 }
16 }
17 }
18 }
19}
Decompress AES Encrypted Stored Archive
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream fs = File.OpenRead(dataDir + "StoreMutlipleFilesWithoutCompressionWithPassword_out.zip"))
3{
4 using (var extracted = File.Create(dataDir + "alice_aesextracted_out.txt"))
5 {
6 using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
7 {
8 using (var decompressed = archive.Entries[0].Open())
9 {
10 byte[] b = new byte[8192];
11 int bytesRead;
12 while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
13 {
14 extracted.Write(b, 0, bytesRead);
15 }
16 }
17 }
18 }
19}
Decompress Encrypted Folder to Directory
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(".\\all_corpus_encrypted.zip", FileMode.Open))
3{
4 new Archive(zipFile, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }).ExtractToDirectory(".\\all_corpus_decrypted");
5}
Decompressing Archives
Decompress Archive having Single File
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream fs = File.OpenRead(dataDir + "CompressSingleFile_out.zip"))
3{
4 using (Archive archive = new Archive(fs))
5 {
6 int percentReady = 0;
7 archive.Entries[0].ExtractionProgressed += (s, e) =>
8 {
9 int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize);
10 if (percent > percentReady)
11 {
12 Console.WriteLine(string.Format("{0}% decompressed", percent));
13 percentReady = percent;
14 }
15 };
16 archive.Entries[0].Extract(dataDir + "alice_extracted_out.txt");
17 }
18}
Decompress Archive having Multiple Files
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "CompressMultipleFiles_out.zip", FileMode.Open))
3{
4 StringBuilder sb = new StringBuilder("Entries are: ");
5 int percentReady = 0;
6 using (Archive archive = new Archive(zipFile,
7 new ArchiveLoadOptions()
8 {
9 EntryListed = (s, e) => { sb.AppendFormat("{0}, ", e.Entry.Name); },
10 EntryExtractionProgressed = (s, e) =>
11 {
12 int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize);
13 if (percent > percentReady)
14 {
15 Console.WriteLine(string.Format("{0}% compressed", percent)); percentReady = percent;
16 }
17 }
18 }))
19 {
20 Console.WriteLine(sb.ToString(0, sb.Length - 2));
21 using (var extracted = File.Create(dataDir + "alice_extracted_out.txt"))
22 {
23 using (var decompressed = archive.Entries[0].Open())
24 {
25 byte[] buffer = new byte[8192];
26 int bytesRead;
27 while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
28 {
29 extracted.Write(buffer, 0, bytesRead);
30 }
31 // Read from decompressed stream to extracting file.
32 }
33 }
34 percentReady = 0;
35 archive.Entries[1].Extract(dataDir + "asyoulik_extracted_out.txt");
36 }
37}
Extract Stored Archive without Compression
1// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
2using (FileStream zipFile = File.Open(dataDir + "StoreMultipleFilesWithoutCompression_out.zip", FileMode.Open))
3{
4 using (Archive archive = new Archive(zipFile))
5 {
6 using (var extracted = File.Create(dataDir + "alice_extracted_store_out.txt"))
7 {
8 using (var decompressed = archive.Entries[0].Open())
9 {
10 byte[] buffer = new byte[8192];
11 int bytesRead;
12 while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
13 {
14 extracted.Write(buffer, 0, bytesRead);
15 }
16 // Read from decompressed stream to extracting file.
17 }
18 }
19
20 using (var extracted = File.Create(dataDir + "asyoulik_extracted_store_out.txt"))
21 {
22 using (var decompressed = archive.Entries[1].Open())
23 {
24 byte[] buffer = new byte[8192];
25 int bytesRead;
26 while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
27 {
28 extracted.Write(buffer, 0, bytesRead);
29 }
30 // Read from decompressed stream to extracting file.
31 }
32 }
33 }
34}