From cc2e3511e501c62e7d3002c4d499bd5ccf3d87f5 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Fri, 31 Oct 2025 12:05:53 +0000 Subject: [PATCH 1/3] Added new kb article read-folder-encrypted-archive --- .../read-folder-encrypted-archive.md | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 knowledge-base/read-folder-encrypted-archive.md diff --git a/knowledge-base/read-folder-encrypted-archive.md b/knowledge-base/read-folder-encrypted-archive.md new file mode 100644 index 00000000..c439492d --- /dev/null +++ b/knowledge-base/read-folder-encrypted-archive.md @@ -0,0 +1,139 @@ +--- +title: How to Read Folder's Content from a Protected Archive Using Telerik ZipLibrary +description: Learn how to pack a folder and subfolders into an encrypted archive using Telerik ZipLibrary while maintaining the correct ZIP structure. +type: how-to +page_title: How to Create Password-Protected ZIP Archives with Folder Structure +meta_title: How to Create Password-Protected ZIP Archives with Folder Structure +slug: read-folder-encrypted-archive +tags: zip, library, telerik, document, processing, aes, encryption, archive, folders, password, protection +res_type: kb +ticketid: 1702323 +--- + +## Environment + +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.3.806| ZipLibrary|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +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. + +This knowledge base article also answers the following questions: +- How to create a ZIP archive with subfolders using Telerik ZipLibrary? +- How to encrypt a ZIP file using Telerik ZipLibrary? +- How to extract files from an encrypted ZIP archive? + +## Solution + +### Creating a Password-Protected ZIP Archive + +To create a password-protected ZIP archive with the correct folder structure, perform the following steps: + +1. Iterate through all files in the source directory and subdirectories. +2. Create a separate entry for each file using its relative path. +3. Apply password protection using `PasswordEncryptionSettings`. + +Here is the modified code: + +```csharp + static void Main(string[] args) + { + string zipFileName = @"..\..\TestZip.zip"; + string sourceFolder = @"..\..\MyFolder"; + + File.Delete(zipFileName); + Telerik.Windows.Zip.Extensions.ZipFile.CreateFromDirectory(sourceFolder, zipFileName, TimeSpan.FromSeconds(10)); + + string encryptedZipPath = "encrypted.zip"; + string password = "pw"; + + // Open the existing ZIP archive + using (Stream inputStream = File.OpenRead(zipFileName)) + using (ZipArchive sourceArchive = ZipArchive.Read(inputStream)) + { + // Set AES encryption settings + PasswordEncryptionSettings aesEncryptionSettings = EncryptionSettings.CreateAesPasswordEncryptionSettings(); + aesEncryptionSettings.Password = password; + + // Create the encrypted ZIP archive + using (Stream outputStream = File.Open(encryptedZipPath, FileMode.Create)) + using (ZipArchive encryptedArchive = ZipArchive.Create(outputStream, null, null, aesEncryptionSettings)) + { + foreach (ZipArchiveEntry sourceEntry in sourceArchive.Entries) + { + using (Stream sourceEntryStream = sourceEntry.Open()) + using (ZipArchiveEntry encryptedEntry = encryptedArchive.CreateEntry(sourceEntry.FullName)) + using (Stream encryptedEntryStream = encryptedEntry.Open()) + { + sourceEntryStream.CopyTo(encryptedEntryStream); + } + } + } + } + File.Delete(zipFileName); + //Process.Start(new ProcessStartInfo() { FileName = encryptedZipPath, UseShellExecute = true }); + + bool extracted = ExtractFile(encryptedZipPath, @"..\..\"); + + + } + +``` + +### Extracting Files from an Encrypted ZIP Archive + +To extract files from an encrypted ZIP archive, perform the following steps: + +1. Use `EncryptionSettings.CreateDecryptionSettings()` for decryption settings. +2. Handle the `PasswordRequired` event to assign the correct password. +3. Iterate through the entries and extract the desired files. + +Here is the code for extracting files: + +```csharp + public static bool ExtractFile(string zipFileName, string targetPath) + { + DecryptionSettings decryptionSettings = EncryptionSettings.CreateDecryptionSettings(); + decryptionSettings.PasswordRequired += (s, a) => a.Password = "pw"; + CompressionSettings compressionSettings = null; + Encoding encoding = null; + + try + { + using (Stream inputStream = File.OpenRead(zipFileName)) + using (ZipArchive archive = ZipArchive.Read(inputStream, encoding, compressionSettings, decryptionSettings)) + { + foreach (ZipArchiveEntry entry in archive.Entries) + { + if (entry.Length == 0) + { + continue; + } + + byte[] data = new byte[entry.Length]; + + using (Stream entryStream = entry.Open()) + { + BinaryReader reader = new BinaryReader(entryStream); + reader.Read(data, 0, data.Length); + } + string text = Encoding.UTF8.GetString(data); + Debug.WriteLine($"EXTRACTED file: {entry.Name}, Content: {text}"); + + } + } + } + catch (Exception ex) + { + Console.WriteLine("Error extracting file: " + ex.Message); + } + + return true; + } +``` + +### See Also + +- [Protect ZipArchive]({%slug radziplibrary-protect-ziparchive%}) From 51221f630fc7c7a4d4bbe32558682cf19c5a3085 Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Fri, 31 Oct 2025 15:11:34 +0200 Subject: [PATCH 2/3] link the kb --- knowledge-base/read-folder-encrypted-archive.md | 16 ++++++++++++---- .../radziplibrary/features/protect-ziparchive.md | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/knowledge-base/read-folder-encrypted-archive.md b/knowledge-base/read-folder-encrypted-archive.md index c439492d..acccdc4d 100644 --- a/knowledge-base/read-folder-encrypted-archive.md +++ b/knowledge-base/read-folder-encrypted-archive.md @@ -29,6 +29,16 @@ This knowledge base article also answers the following questions: ### Creating a Password-Protected ZIP Archive +Let's have the following folders structure and we want to : + +* MyFolder + * Subfolder1 + * textFile1.txt + * textFile2.txt + * Subfolder2 + * textFile3.txt + * textFile4.txt + To create a password-protected ZIP archive with the correct folder structure, perform the following steps: 1. Iterate through all files in the source directory and subdirectories. @@ -73,18 +83,16 @@ Here is the modified code: } } File.Delete(zipFileName); - //Process.Start(new ProcessStartInfo() { FileName = encryptedZipPath, UseShellExecute = true }); + Process.Start(new ProcessStartInfo() { FileName = encryptedZipPath, UseShellExecute = true }); bool extracted = ExtractFile(encryptedZipPath, @"..\..\"); - - } ``` ### Extracting Files from an Encrypted ZIP Archive -To extract files from an encrypted ZIP archive, perform the following steps: +To extract files from an encrypted ZIP archive and read the content, perform the following steps: 1. Use `EncryptionSettings.CreateDecryptionSettings()` for decryption settings. 2. Handle the `PasswordRequired` event to assign the correct password. diff --git a/libraries/radziplibrary/features/protect-ziparchive.md b/libraries/radziplibrary/features/protect-ziparchive.md index de1cc363..4a6b78f0 100644 --- a/libraries/radziplibrary/features/protect-ziparchive.md +++ b/libraries/radziplibrary/features/protect-ziparchive.md @@ -142,3 +142,4 @@ In order to open a password-protected __ZipArchive__, you need to pass a __Defau * [Getting Started]({%slug radziplibrary-gettingstarted%}) * [Update ZipArchive]({%slug radziplibrary-update-ziparchive%}) * [ZipLibrary Archive Protection Demo](https://demos.telerik.com/document-processing/ziplibrary/archive_protection) + * [How to Read Folder's Content from a Protected Archive Using Telerik ZipLibrary]({%slug read-folder-encrypted-archive%}) From b0f94d49609c256c9b9af2d64f18ee8ce42618a4 Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Fri, 31 Oct 2025 15:23:47 +0200 Subject: [PATCH 3/3] Update read-folder-encrypted-archive.md --- knowledge-base/read-folder-encrypted-archive.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knowledge-base/read-folder-encrypted-archive.md b/knowledge-base/read-folder-encrypted-archive.md index acccdc4d..1e718242 100644 --- a/knowledge-base/read-folder-encrypted-archive.md +++ b/knowledge-base/read-folder-encrypted-archive.md @@ -29,7 +29,7 @@ This knowledge base article also answers the following questions: ### Creating a Password-Protected ZIP Archive -Let's have the following folders structure and we want to : +Let's have the following folders structure and we want to zip the root folder with all of its content: * MyFolder * Subfolder1