Skip to content
December 13, 2010 / kiranpatils

SharpZipLib is not working When compression is enabled on IIS

Challenge:

Before so many months back I was facing problem related to SharpZipLib. Basically Using SharpZipLib we were zipping a file at runtime and allowing users to save that zipped file at his/her local machine. It works fine on my local box. But when we roll it out on another IIS server it was not working. [General developers problem :)].

When we delved more in to that, then found that another IIS server has been configured for “HTTP Compression“.

Solution:

Okay, so now we were knowing that Issue is due to IIS Server which has HTTP Compression enabled. Unfortunately we can’t disable it as it will affect the performance. So, now we just have one way to make it working which is through code. We tried lot and finally we found a way to do it, it was just one line change in our Response’s ContentType.

Before we see working code, let’s see the code which was having a problem:


private void DownloadZip(byte[ bufferzip)
 {
 // Load Zip file
 if (bufferzip != null && bufferzip.Length > 0)
 {
 HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.ClearHeaders();
 HttpContext.Current.Response.CacheControl = "public";
 HttpContext.Current.Response.ContentType = "application/zip"; //This line was causing a problem!
 HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=\"MyFile.zip\"");
 HttpContext.Current.Response.Flush();
 HttpContext.Current.Response.BinaryWrite(bufferzip);
 HttpContext.Current.Response.End();
 }

}

Now, it’s time to see a working code!


private void DownloadZip(byte[ bufferzip)
 {
 // Load Zip file
 if (bufferzip != null && bufferzip.Length > 0)
 {
 HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.ClearHeaders();
 HttpContext.Current.Response.CacheControl = "public";
 HttpContext.Current.Response.ContentType = "application/octet-stream"; // This is real Hero!
 HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=\"MyFile.zip\"");
 HttpContext.Current.Response.Flush();
 HttpContext.Current.Response.BinaryWrite(bufferzip);
 HttpContext.Current.Response.End();

}

}

Now, if you compare the working and non working code you will see the difference. HttpContext.Current.Response.ContentType = “application/zip”; was causing an issue and changing it to HttpContext.Current.Response.ContentType = “application/octet-stream”; solved it!

Reference links:

http://community.sharpdevelop.net/forums/p/10467/28859.aspx#28859
http://www.jlaforums.com/viewtopic.php?t=7295390
http://support.microsoft.com/kb/841120

Happy Coding!🙂

One Comment

Leave a Comment
  1. Nilesh / Dec 28 2010 3:05 pm

    Bas ne.. share nahi karvanoo ne… btw.. its really good to know..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: