|
| 1 | +--- |
| 2 | +title: How to Read Folder's Content from a Protected Archive Using Telerik ZipLibrary |
| 3 | +description: Learn how to pack a folder and subfolders into an encrypted archive using Telerik ZipLibrary while maintaining the correct ZIP structure. |
| 4 | +type: how-to |
| 5 | +page_title: How to Create Password-Protected ZIP Archives with Folder Structure |
| 6 | +meta_title: How to Create Password-Protected ZIP Archives with Folder Structure |
| 7 | +slug: read-folder-encrypted-archive |
| 8 | +tags: zip, library, telerik, document, processing, aes, encryption, archive, folders, password, protection |
| 9 | +res_type: kb |
| 10 | +ticketid: 1702323 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| ZipLibrary|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +This article aims to demonstrate a sample approach how to create a password-protected ZIP archive with Telerik ZipLibrary that maintains the folder structure and includes subfolders and files. Then, extract all files from the encrypted ZIP archive reading the text content of each file. |
| 22 | + |
| 23 | +This knowledge base article also answers the following questions: |
| 24 | +- How to create a ZIP archive with subfolders using Telerik ZipLibrary? |
| 25 | +- How to encrypt a ZIP file using Telerik ZipLibrary? |
| 26 | +- How to extract files from an encrypted ZIP archive? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +### Creating a Password-Protected ZIP Archive |
| 31 | + |
| 32 | +Let's have the following folders structure and we want to zip the root folder with all of its content: |
| 33 | + |
| 34 | +* MyFolder |
| 35 | + * Subfolder1 |
| 36 | + * textFile1.txt |
| 37 | + * textFile2.txt |
| 38 | + * Subfolder2 |
| 39 | + * textFile3.txt |
| 40 | + * textFile4.txt |
| 41 | + |
| 42 | +To create a password-protected ZIP archive with the correct folder structure, perform the following steps: |
| 43 | + |
| 44 | +1. Iterate through all files in the source directory and subdirectories. |
| 45 | +2. Create a separate entry for each file using its relative path. |
| 46 | +3. Apply password protection using `PasswordEncryptionSettings`. |
| 47 | + |
| 48 | +Here is the modified code: |
| 49 | + |
| 50 | +```csharp |
| 51 | + static void Main(string[] args) |
| 52 | + { |
| 53 | + string zipFileName = @"..\..\TestZip.zip"; |
| 54 | + string sourceFolder = @"..\..\MyFolder"; |
| 55 | + |
| 56 | + File.Delete(zipFileName); |
| 57 | + Telerik.Windows.Zip.Extensions.ZipFile.CreateFromDirectory(sourceFolder, zipFileName, TimeSpan.FromSeconds(10)); |
| 58 | + |
| 59 | + string encryptedZipPath = "encrypted.zip"; |
| 60 | + string password = "pw"; |
| 61 | + |
| 62 | + // Open the existing ZIP archive |
| 63 | + using (Stream inputStream = File.OpenRead(zipFileName)) |
| 64 | + using (ZipArchive sourceArchive = ZipArchive.Read(inputStream)) |
| 65 | + { |
| 66 | + // Set AES encryption settings |
| 67 | + PasswordEncryptionSettings aesEncryptionSettings = EncryptionSettings.CreateAesPasswordEncryptionSettings(); |
| 68 | + aesEncryptionSettings.Password = password; |
| 69 | + |
| 70 | + // Create the encrypted ZIP archive |
| 71 | + using (Stream outputStream = File.Open(encryptedZipPath, FileMode.Create)) |
| 72 | + using (ZipArchive encryptedArchive = ZipArchive.Create(outputStream, null, null, aesEncryptionSettings)) |
| 73 | + { |
| 74 | + foreach (ZipArchiveEntry sourceEntry in sourceArchive.Entries) |
| 75 | + { |
| 76 | + using (Stream sourceEntryStream = sourceEntry.Open()) |
| 77 | + using (ZipArchiveEntry encryptedEntry = encryptedArchive.CreateEntry(sourceEntry.FullName)) |
| 78 | + using (Stream encryptedEntryStream = encryptedEntry.Open()) |
| 79 | + { |
| 80 | + sourceEntryStream.CopyTo(encryptedEntryStream); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + File.Delete(zipFileName); |
| 86 | + Process.Start(new ProcessStartInfo() { FileName = encryptedZipPath, UseShellExecute = true }); |
| 87 | + |
| 88 | + bool extracted = ExtractFile(encryptedZipPath, @"..\..\"); |
| 89 | + } |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +### Extracting Files from an Encrypted ZIP Archive |
| 94 | + |
| 95 | +To extract files from an encrypted ZIP archive and read the content, perform the following steps: |
| 96 | + |
| 97 | +1. Use `EncryptionSettings.CreateDecryptionSettings()` for decryption settings. |
| 98 | +2. Handle the `PasswordRequired` event to assign the correct password. |
| 99 | +3. Iterate through the entries and extract the desired files. |
| 100 | + |
| 101 | +Here is the code for extracting files: |
| 102 | + |
| 103 | +```csharp |
| 104 | + public static bool ExtractFile(string zipFileName, string targetPath) |
| 105 | + { |
| 106 | + DecryptionSettings decryptionSettings = EncryptionSettings.CreateDecryptionSettings(); |
| 107 | + decryptionSettings.PasswordRequired += (s, a) => a.Password = "pw"; |
| 108 | + CompressionSettings compressionSettings = null; |
| 109 | + Encoding encoding = null; |
| 110 | + |
| 111 | + try |
| 112 | + { |
| 113 | + using (Stream inputStream = File.OpenRead(zipFileName)) |
| 114 | + using (ZipArchive archive = ZipArchive.Read(inputStream, encoding, compressionSettings, decryptionSettings)) |
| 115 | + { |
| 116 | + foreach (ZipArchiveEntry entry in archive.Entries) |
| 117 | + { |
| 118 | + if (entry.Length == 0) |
| 119 | + { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + byte[] data = new byte[entry.Length]; |
| 124 | + |
| 125 | + using (Stream entryStream = entry.Open()) |
| 126 | + { |
| 127 | + BinaryReader reader = new BinaryReader(entryStream); |
| 128 | + reader.Read(data, 0, data.Length); |
| 129 | + } |
| 130 | + string text = Encoding.UTF8.GetString(data); |
| 131 | + Debug.WriteLine($"EXTRACTED file: {entry.Name}, Content: {text}"); |
| 132 | + |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + catch (Exception ex) |
| 137 | + { |
| 138 | + Console.WriteLine("Error extracting file: " + ex.Message); |
| 139 | + } |
| 140 | + |
| 141 | + return true; |
| 142 | + } |
| 143 | +``` |
| 144 | + |
| 145 | +### See Also |
| 146 | + |
| 147 | +- [Protect ZipArchive]({%slug radziplibrary-protect-ziparchive%}) |
0 commit comments