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:
[sourcecode languag=”csharp”]
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();
}
}
[/sourcecode]
Now, it’s time to see a working code!
[sourcecode languag=”csharp”]
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();
}
}
[/sourcecode]
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! 🙂

Exceeded storage allocation. The server response was: 5.7.0 Our system detected an illegal attachment on your message.

If you are genearting zip file runtime in memory and sending mail at run time. and you faced the error as shown below:
Exceeded storage allocation. The server response was: 5.7.0 Our system detected an illegal attachment on your message.
Then to fix it, you have to do the following change in your code(Pls note I’ve used SharpZipLib and Memory Stream for sending an email at run time using gmail server):
[sourcecode language=”csharp”]
MemoryStream zipFileStream = new MemoryStream();
zipOutputStream = new ZipOutputStream(zipFileStream);
..
..
..
//read from stream and add to zipoutputstream code should go here

..
// Seek to begining — IF YOU DON’ ADD BELOW LINE IT Will give above error
zipFileStream.Seek(0, SeekOrigin.Begin);
mailMessage.Attachments.Add(new Attachment(zipFileStream, "myzipfile.zip"));
[/sourcecode]
if you’ve seen the above code. Below line says MemroyStream to seek to Begining of the stream, So when SMTP Server scans the file before sending it gets perfect file.
zipFileStream.Seek(0, SeekOrigin.Begin);
Happy Mailing!

The Compressed (zipped) Folder is invalid or corrupted with SharpZipLib

I have generated Zip File using SharpZipLib. But when i try to access it using Windows XP extraction tool it shos following error:  Compressed (zipped) Folder is invalid or corrupted and it worked fine with 7zip. So, i did googling and found one nice solution from this link : http://blog.tylerholmes.com/2008/12/windows-xp-unzip-errors-with.html
Solution is — extracted from the above blog only.
[sourcecode language=”csharp”]

ZipEntry entry = new ZipEntry(Path.GetFileName(sourceFilePath));
entry.DateTime = DateTime.Now;
/* By specifying a size, SharpZipLib will turn on/off UseZip64 based on the file sizes. If Zip64 is ON
* some legacy zip utilities (ie. Windows XP) who can’t read Zip64 will be unable to unpack the archive.
* If Zip64 is OFF, zip archives will be unable to support files larger than 4GB. */
entry.Size = new FileInfo(sourceFilePath).Length;
zipStream.PutNextEntry(entry);

[/sourcecode]
If it works say thanks to Tyler Holmes — Above Blog author, for such a nice article!
Happy zipping!

UnSupported Compression method

Challenge:

I was writing a piece of code which generates zip file from the files read from Database and send  as an attachment mail to user. I used SharpZiLib library for that. And i completed writing my logic and mail goes fine to user’s mail box. So, far so good :). But when user tries to extract the attached zip file using 7Zip it shown following error and was working fine with winzip:
UnSupported Compression method for FILENAME.FILEXTENSION ERROR

Solution:

Then i just started going through the code and i just commented following line of cod and it worked!
[sourcecode language=”csharp”]
//Crc32 crc = new Crc32();
//crc.Reset();
//crc.Update(buffer);
//zipEntry.Crc = crc.Value;
[/sourcecode]
Hope this helps you to resolve your problem and say happy zipping!