Zip content of a folder into an encrypted Zip file

2 posts, 0 answers
  1. Ramin
    Ramin avatar
    30 posts
    Member since:
    Sep 2010

    Posted 16 Apr 2014 Link to this post

    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?
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 17 Apr 2014 Link to this post

    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.

     
Back to Top