In this article I’m going to explain how to create the zip file using C#. As we know .net does not support the compression by default we need to use the third part open source or paid control this purpose.  In this article I’m going to review open source compressing and encryption software we could use in our .net application.

Compression system will be very handy invention especially for internet user. Using compression we could save the huge space. If we send the lesser sized data we could save the bandwidth and achieved the faster download speed. We could compress the files when we are sending the file over the internet using Email, FTP etc.  If you are sending the crucial financial data then it is better to encrypt with the password. These are all the extracted features of zip file or compression over the internet.

  1. DotNetZip(http://www.dotnetzip.codeplex.com/): It is opensource library, DotNetZip is an easy-to-use, FAST, FREE class library and toolset for manipulating zip files or folders. Zip and Unzip is easy: with DotNetZip, .NET applications written in VB, C# – any .NET language – can easily create, read, extract, or update zip files. For Mono or MS .NET. it works on .net framework system, mobile devices. Asp.net example to compress the string and download it as zip file.  Zip file will be password protected by password “Password1”.
  2. using Ionic.Zip;
    public void Page_Load (Object sender, EventArgs e)
    {
        try
        {
            Response.Clear();
            Response.BufferOutput= false;
        System.Web.HttpContext c= System.Web.HttpContext.Current;
            String ReadmeText= String.Format("README.TXT\n\nHello!\n\n" +
                                             "This is a zip file that was dynamically generated at {0}\n" +
                                             "by an ASP.NET Page running on the machine named '{1}'.\n" +
                                             "The server type is: {2}\n"+
                                             "The password used: {3}\n",
                                             "Encryption: {4}\n",
                                             System.DateTime.Now.ToString("G"),
                                             System.Environment.MachineName,
                                             c.Request.ServerVariables["SERVER_SOFTWARE"],
                                             tbPassword.Text,
                                             (chkUseAes.Checked)?EncryptionAlgorithm.WinZipAes256.ToString() : "None"
                                             ); 
            string archiveName= String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));         Response.ContentType = "application/zip";         Response.AddHeader("content-disposition", "filename=" + archiveName);         using (ZipFile zip = new ZipFile())         {             // the Readme.txt file will not be password-protected.             zip.AddEntry("Readme.txt", "", ReadmeText, Encoding.Default);             if (!String.IsNullOrEmpty(tbPassword.Text))             {                 zip.Password = "Password1";                 zip.Encryption = EncryptionAlgorithm.WinZipAes256;             }             // filesToInclude is a string[] or List<String>             zip.AddFiles(filesToInclude, "files");             zip.Save(Response.OutputStream);         }         Response.Close();     }     catch (Exception)     {         // Ignored     } }
  3. #zipLib (http://www.icsharpcode.net/opensource/sharpziplib/): #ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this way: “I’ve ported the zip library over to C# because I needed gzip/zip compression and I didn’t want to use libzip.dll or something like this. I want all in pure C#”. The library is released under the GPL hence I have not exposed this library much.

It would be nice if you like to share the library used in your application so far. It may help others.

Cheers
Sandesh

Similar Topics: