This is a migrated thread and some comments may be shown as answers.

Zip content of a folder into an encrypted Zip file

7 Answers 1068 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Ramin
Top achievements
Rank 1
Ramin asked on 16 Apr 2014, 10:33 PM
Hi Telerik Team,

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

Sort by
0
Andrey
Telerik team
answered on 17 Apr 2014, 07:26 AM
Hello Ramin,

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,
Andrey Murzov
Telerik
 

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.

 
0
Selvam
Top achievements
Rank 1
answered on 11 Jul 2019, 01:01 PM

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 

0
Tanya
Telerik team
answered on 16 Jul 2019, 07:03 AM
Hi Selvam,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Selvam
Top achievements
Rank 1
answered on 16 Jul 2019, 07:14 AM
Thanks Telerik Team your post very useful. Keep rocks.....
0
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 01 Mar 2021, 08:37 PM

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.

0
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 01 Mar 2021, 10:20 PM

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.

 

0
Peshito
Telerik team
answered on 04 Mar 2021, 12:03 PM

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/.

Test
Top achievements
Rank 1
commented on 10 Feb 2022, 04:06 PM

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())

 

Tanya
Telerik team
commented on 14 Feb 2022, 02:06 PM

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.

Tags
ZipLibrary
Asked by
Ramin
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Selvam
Top achievements
Rank 1
Tanya
Telerik team
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
Peshito
Telerik team
Share this question
or