views:

350

answers:

3

I have just created Javascript & CSS handler for optimization file size using both YUI Compressor & GZip or Deflate compressor depend on request header. The following code is used to compressing file by GZipStream class. But I found that it can reduce JQuery 1.3.2(YUI compressed) file size from 58KB -> 54KB only.

public void GZipCompressFile(string filePath, string compressedFilePath)
{
   FileStream sourceFile = File.OpenRead(filePath);
   FileStream destFile = File.Create(compressedFilePath);

   using (GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress))
   {
      int theByte = sourceFile.ReadByte();
      while (theByte != -1)
      {
         theByte = sourceFile.ReadByte();
      }
   }

   sourceFile.Dispose();
   destFile.Dispose();
}

In the other hand, I compress file with 7-Zip by using gz format(Ultra compression mode). Size of compressed file is only 19.3 KB. However, I can't use 7-Zip as default compressor. Because It can't compress file by using Deflate compressor method.

Do you have any component to compress file both GZip & Deflate file type? or Do you have any idea for edit my GZipCompressFile method? Moreover, Do all new browsers(IE8,FF3.5,Opera 9.6, Safari 4) support compression method(both GZip and Deflate)?

A: 

Yeah, the built in compression isn't all that great. Try using the Ionic.zip library which will give you better compression ratios.

Paul Alexander
Can DotNetZip library compress file using GZip and Deflate? Why I can't do that?
Soul_Master
A: 

Please have a look at Helicon Ape mod_gzip module. And here are some guidelines on how to use it.

TonyCool
A: 

Try zlib.net (http://www.componentace.com/zlib%5F.NET.htm) for compression.

I have a page with a content-length of 14588 uncompressed.

.net's native gzip brings it down to 2909 zlib.net's gzip brings it down to 2590

.net's native deflate brings it down to 2891 zlib.net's deflate brings it down to 2559 using the "best compression" setting and 2583 with the default setting.

Its easy too:

using zlib;
    Response.Filter = new ZOutputStream(Response.Filter, zlib.zlibConst.Z_BEST_COMPRESSION);
    Response.AppendHeader("Content-Encoding", "deflate");
David Murdoch