I was able to use ZipPackage to zip all the files that I have in a folder into a new zip file.
Note: The folder contains different file types .zip, .pdf, .xlsx .docx and .txt
However a new requirement is to encrypt the main zip file so no one can access the content of the file without having the password.
I have seen the sample below but I don't know how to get it to work with different existing binary files like PDF.
using (Stream stream = File.Open("test.zip", FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null))
{
using (ZipArchiveEntry entry = archive.CreateEntry("text.txt"))
{
StreamWriter writer = new StreamWriter(entry.Open());
writer.WriteLine("Hello world!");
writer.Flush();
}
}
}
Can you help?
7 Answers, 1 is accepted
Thank you for contacting us.
You should use streams to write the data from a file to the archive. For example:
private
void
CreateEncryptedZipFileFromDirectory(
string
sourceDirectoryName,
string
destinationArchiveFileName)
{
char
[] directorySeparatorChar =
new
char
[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
if
(!
string
.IsNullOrEmpty(sourceDirectoryName))
{
using
(FileStream archiveStream = File.Open(
destinationArchiveFileName,
FileMode.Create))
{
// Set password to protect ZIP archive
DefaultEncryptionSettings encryptionSettings =
new
DefaultEncryptionSettings();
encryptionSettings.Password =
"PaSsWoRd"
;
using
(ZipArchive archive =
new
ZipArchive(
archiveStream,
ZipArchiveMode.Create,
true
,
null
,
null
,
encryptionSettings))
{
foreach
(
string
fileName
in
Directory.GetFiles(sourceDirectoryName))
{
using
(FileStream file = File.OpenRead(fileName))
{
int
length = fileName.Length - sourceDirectoryName.Length;
string
entryName = fileName.Substring(sourceDirectoryName.Length, length);
entryName = entryName.TrimStart(directorySeparatorChar);
using
(ZipArchiveEntry entry = archive.CreateEntry(entryName))
{
using
(Stream entryStream = entry.Open())
{
file.CopyTo(entryStream);
}
}
}
}
}
}
}
}
If you have further questions do not hesitate to contact us.
Regards,
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Hi Telerik Team,
We have create one main Folder then sub folder create stored pdf file,xlxs file.How we can zip the main folder. please guide me
For your reference : The main folder name is "BulkFile" Sub folder-1 "AOF",Sub folder-2 "KRA" then pdf file stored to sub folder1,xlxs file file stored to sub folder2.
please share source code or e.g
You can easily achieve that through the extension methods provided by RadZipLibrary and more specifically the CreateFromDirectory() method.For more information on the matter please check the Zip Extensions help topic.
Hope this is helpful.
Regards,
Tanya
Progress Telerik


Could I get a code snippet on how to use the CreateFromDirectory method?
I have Telerik.Windows.Zip referenced in my WPF project but I can't see any "CreateFromDirectory" method?
Documentation you linked seems to be referencing WinForms ... is this not available for WPF? Also suggests the extensions were merged into Telerik.Windows.Zip.
Cheers, Rob.

UPDATE:
Looks like I needed to add the reference to Telerik.Windows.Zip.Extensions even though the documentation says they were merged into single Telerik.Windows.Zip DLL.
It seems the CreateFromDirectory does NOT support any password encryption settings?
I work around that limitation by using the CreateEntryFromFile on the output from CreateFromDirectory ... but is this by design?
Cheers, Rob.
Hello Rob,
I am glad you managed to solve this. You have properly used the Telerik.Windows.Zip.Extensions. Looking at the documentation it says that "In UI for WinForms, the classes are merged in Telerik.WinControls.dll, so you will need to refer that assembly instead of Telerik.Windows.Zip.Extensions.dll." In case this is not what you had in mind, could you refer to the place in the documentation where it is said that the assemblies are merged into one?
As for the CreateFromDirectory method, it does not have an overload that accepts password encryption settings and you figured it right to use the CreateEntryFromFile method instead so you could password protect it. You can find more about protecting ZipArchive here.
Regards,
Peshito
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hi
This product is comfortable with Azure blob or not .
because i use CloudBlobStream (cloudBlockBlob.OpenWrite())
instead of
FileStream archiveStream = File.Open(
destinationArchiveFileName,
FileMode.Create))
this is not working for me .
is there any example to create zip using CloudBlobStream (cloudBlockBlob.OpenWrite())
Hello,
The ZipLibrary works with the base Stream class and since the CloudBlobStream inherits from it, I cannot see a particular reason for unexpected behavior. In case you are experiencing difficulties running the library with CloudBlobStream, I would suggest you open a support ticket where you can privately share the code you are using and we can further investigate the case.
Hope this is useful.