Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Using DotNetZip Library to Compress Data in ASP.Net

, 24 Nov 2011
Rate this:
Please Sign up or sign in to vote.
I recently worked on a project that involved creating encrypted Zip files. Data is captured on a web form and then written to a text file.At first, the task sounded complex. However, if you are aware of the DotNetZip library existence, your task just got easier.You can download the DotNetZip library
I recently worked on a project that involved creating encrypted Zip files. Data is captured on a web form and then written to a text file. At first, the task sounded complex. However, if you are aware of the DotNetZip library existence, your task just got easier.

You can download the DotNetZip library from http://dotnetzip.codeplex.com/ Once you download and install the package, you will need to add a reference to Ionic.Zip.dll or copy the file to your \lib folder, this is all you need to start generating your own zip files.

The best part: DotNetZip is free!!

DotNetZip is well documented and is much easier to use than any other solutions out there including System.IO.Compression and SharpZipLib, after trying both methods I would highly recommend DotNetZip because is incredibly easy to use.

Below is a sample method to illustrate how to easily to compress content that is being passed as a string. The method takes the file name and the content as arguments:

internal MemoryStream ZipString(string aZipFileName, string aContents, string aMode)
{
    try
    {
        //Add these to your AppSettings on Web.Config
        string sZipFilePassword = ConfigurationManager.AppSettings["ZipFilePassword"];
        string sZipFileEncryption = ConfigurationManager.AppSettings["ZipFileStrongEncryption"];

        //Stored the zippped data on this memory stream
        MemoryStream msZippedContent = new MemoryStream();
        // Creating Zip 
        using (var zip = new ZipFile())
        {
            // Add the password protection
            zip.Password = sZipFilePassword;
            if (sZipFileEncryption == "N")
            {
                //PkZipWeak is not a string encryption method, but is supported by any UnZip utility
                zip.Encryption = EncryptionAlgorithm.PkzipWeak;
            }
            else
            {
                //WinZipAes128 and WinZipAes256 is NOT compatible with Windows ZIP
                zip.Encryption = EncryptionAlgorithm.WinZipAes256;
            }

            // Add the desired file to the Zip
            zip.AddEntry(aZipFileName, aContents, Encoding.ASCII);

            // Send the contents of the ZIP back to the output stream
            zip.Save(msZippedContent);
            return msZippedContent;
        }
    }
    catch (Exception genEx)
    {
        Response.Write(genEx.Message);
        return null;
    }

}//ZipString

The method declares a variable of type ZipFile which is available once you add a reference: "using Ionic.Zip;" That declaration creates the shell of the zip file. After that, the process to save the file is simple, add a password if needed, add an encryption method and you can proceed to add an entry to the Zip file by using AddEntry. Of course, you could add multiple files to the same Zip archive. Finally, you can Save the contents of the Zip which in this case are returned by the method as a MemoryStream that could be used for other purpose such as creating an e-mail attachment.

Be aware that if you need to encrypt the contents of the zip, choosing the encryption method is important because the built-in Windows unzip feature can only decrypt PkZip.Weak content. You would need to have WinZip or WinRar Installed on the target workstation to unzip contents encrypted with the WinZipAes128 or WinZipAes256 methods.

Hope this is helpful and feel free to contact me if you have any questions,
Will

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gmtzgtz

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 Nov 2011
Article Copyright 2011 by gmtzgtz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid