使用GZip,Deflate压缩javascript文件的最佳组件是什么?

时间:2009-07-01 06:58:33

标签: httphandler http-compression

我刚创建了Javascript&使用YUI Compressor& amp; GZip或Deflate压缩器取决于请求标头。以下代码用于通过GZipStream类压缩文件。但我发现它可以从 58KB - >中减少JQuery 1.3.2(YUI压缩)文件大小。仅54KB

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();
}

另一方面,我使用gz格式(超压缩模式)压缩7-Zip文件。压缩文件的大小仅为 19.3 KB 。但是,我不能使用7-Zip作为默认压缩器。因为它无法使用Deflate压缩器方法压缩文件。

你有压缩文件的任何组件GZip&缩小文件类型?或者你有任何想法编辑我的GZipCompressFile方法吗?此外,所有新浏览器(IE8,FF3.5,Opera 9.6,Safari 4)是否都支持压缩方法(GZip和Deflate)?

3 个答案:

答案 0 :(得分:0)

是的,内置压缩并不是那么好。尝试使用Ionic.zip库,这将为您提供更好的压缩率。

答案 1 :(得分:0)

请查看Helicon Ape mod_gzip模块。 here是关于如何使用它的一些指导原则。

答案 2 :(得分:0)

尝试使用zlib.net(http://www.componentace.com/zlib_.NET.htm)进行压缩。

我有一个内容长度为14588未压缩的页面。

.net的本机gzip将其降至2909 zlib.net的gzip将其降至2590

.net的原生deflate将其降至2891 zlib.net的deflate使用“最佳压缩”设置将其降低到2559,使用默认设置降低到2583.

也很容易:

using zlib;
    Response.Filter = new ZOutputStream(Response.Filter, zlib.zlibConst.Z_BEST_COMPRESSION);
    Response.AppendHeader("Content-Encoding", "deflate");
相关问题