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

发布于 2024-07-26 01:26:59 字数 975 浏览 3 评论 0原文

我刚刚创建了 Javascript & 使用 YUI 压缩器和 CSS 处理程序来优化文件大小 GZip 或 Deflate 压缩器取决于请求标头。 以下代码用于通过 GZipStream 类压缩文件。 但我发现它可以减少 JQuery 1.3.2(YUI 压缩)文件大小从 58KB -> 仅 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 和 GZip 文件? 压缩文件类型? 或者您对编辑我的 GZipCompressFile 方法有什么想法吗? 此外,所有新浏览器(IE8、FF3.5、Opera 9.6、Safari 4)都支持压缩方法(GZip 和 Deflate)吗?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-08-02 01:26:59

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

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

白鸥掠海 2024-08-02 01:26:59

请查看 Helicon Ape mod_gzip 模块。 这里是一些关于如何使用它。

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

亢潮 2024-08-02 01:26:59

尝试 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");

Try zlib.net (http://www.componentace.com/zlib_.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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文