如何预压缩非常大的 html 文件

发布于 2024-09-05 20:44:40 字数 570 浏览 3 评论 0原文

我需要使用 gzip 或 deflate 预压缩一些非常大的 html/xml/json 文件(大型数据转储)。我从来不想提供未压缩的文件。它们太大并且重复,压缩可能会很好地工作,虽然一些旧的浏览器不支持解压缩,但我的典型客户不会使用它们(尽管如果我可以生成某种“嘿,你需要升级您的浏览器消息)

我会自动生成文件,并且可以轻松生成与每种文件类型对应的 .htaccess 文件。本质上我想要的是 mod_gunzip 的一些始终可用的版本。因为文件很大,而且我会重复提供它们,所以我需要一种方法,允许我在命令行上压缩一次,效果非常好。

我在这个网站和其他网站上找到了一些关于如何使用 gzip 执行此操作的信息,但我想知道是否有人可以指导我如何使用 deflate 执行此操作。完整答案的加分项包括我的 .htaccess 文件应该是什么样子,以及我应该使用(GNU/Linux)来获得最佳压缩的命令行代码。超级奖励积分还解决了如何向不兼容的浏览器发送“抱歉,没有文件给您”消息的答案。

如果我们可以创建一个“预压缩”标签来涵盖这样的问题,那就太好了。

-FT

I need to pre-compress some very large html/xml/json files (large data dumps) using either gzip or deflate. I never want to serve the files uncompressed. They are so large and repetitive that compression will probably work very very well, and while some older browsers cannot support decompression, my typical customers will not be using them (although it would be nice if I could generate some kind of 'hey you need to upgrade your browser' message)

I auto generate the files and I can easily generate .htaccess files to go along with each file type. Essentially what I want is some always on version of mod_gunzip. Because the files are large, and because I will be repeatedly serving them, I need a method that allows me to compress once, really well, on the command line.

I have found some information on this site and others about how to do this with gzip, but I wondered if someone could step me through how to do this with deflate. Bonus points for a complete answer that includes what my .htaccess file should look like, as well as the command line code I should use (GNU/Linux) to obtain optimal compression. Super bonus points for an answer that also addresses how to send "sorry no file for you" message to un-compliant browsers.

would be lovely if we could create a "precompression" tag to cover questions like this.

-FT

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

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

发布评论

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

评论(4

深白境迁sunset 2024-09-12 20:44:40

编辑:找到在mod_mime中添加编码

这有效:

<IfModule mod_mime.c>
 <Files "*.html.gz">
  ForceType text/html
 </Files>
 <Files "*.xml.gz">
  ForceType application/xml
 </Files>
 <Files "*.js.gz">
  ForceType application/javascript
 </Files>
 <Files "*.gz">
  AddEncoding gzip .gz
 </Files>
</IfModule>

文档听起来好像只需要 AddEncoding ,但我没有让它工作。

此外,Lighttpd 的 mod_compression 可以压缩和缓存(压缩的)文件。

Edit: Found AddEncoding in mod_mime

This works:

<IfModule mod_mime.c>
 <Files "*.html.gz">
  ForceType text/html
 </Files>
 <Files "*.xml.gz">
  ForceType application/xml
 </Files>
 <Files "*.js.gz">
  ForceType application/javascript
 </Files>
 <Files "*.gz">
  AddEncoding gzip .gz
 </Files>
</IfModule>

The docs make it sound like only the AddEncoding should be needed, but I didn't get that to work.

Also, Lighttpd's mod_compression can compress and cache (the compressed) files.

给妤﹃绝世温柔 2024-09-12 20:44:40

如果我是你,我会考虑内置文件系统压缩,而不是在 apache 层执行此操作。

在Solaris上,zfs具有透明压缩,使用zfs compress仅压缩文件系统。
同样,Windows 可以压缩文件夹,Apache 将提供内容,而不会注意到它已压缩在磁盘上。
Linux 的文件系统也可以进行透明压缩。

If I were you, I would look at inbuilt filesystem compression instead of doing this at the apache layer.

On solaris zfs has transparent compression, use zfs compress to just compress the filesystem.
Similarly, windows can compress folders, apache will serve the content oblivious to the fact it's compressed on disk.
Linux has filesystems that do transparent compression also.

羁拥 2024-09-12 20:44:40

对于命令行,编译zlib的zpipe: http://www.zlib.net/zpipe.c 然后

zpipe < BIGfile.html > BIGfile.htmlz

例如。

然后使用 Zash 的示例,设置一个过滤器来更改标头。这应该为您提供 RAW deflate 文件,现代浏览器可能支持< /a>.

有关压缩文件的另一种方法,请查看使用 pigz 和 zlib (-z) 或 PKWare zip ( -K) 压缩选项。测试这些内容是否可以通过内容编码设置完成。

For the command line, compile zlib's zpipe: http://www.zlib.net/zpipe.c and then

zpipe < BIGfile.html > BIGfile.htmlz

for example.

Then using Zash's example, set up a filter to change the header. This should provide you with having RAW deflate files, which modern browsers probably support.

For another way to compress files, take a look at using pigz with zlib (-z) or PKWare zip (-K) compression options. Test if these work coming through with Content-Encoding set.

无语# 2024-09-12 20:44:40

无需直接处理 moz_gzip/mod_defalte 即可压缩内容的快速方法是使用 ob_gzhandler 并修改标头(在将任何输出发送到浏览器之前)。

<?php
/* Replace CHANGE_ME with the correct mime type of your large file. 
 i.e: application/json
*/

ob_start ('ob_gzhandler');
header('Content-type: CHANGE_ME; charset: UTF-8');
header('Cache-Control: must-revalidate');
$offset = 60 * 60 * 2 ;
$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s',time() + $offset) . ' GMT';
header($ExpStr);

/* Stuff to generate your large files here */

A quick way to compress content without dealing directly with moz_gzip/mod_defalte is using ob_gzhandler and modifying headers (before any output is send to the browser).

<?php
/* Replace CHANGE_ME with the correct mime type of your large file. 
 i.e: application/json
*/

ob_start ('ob_gzhandler');
header('Content-type: CHANGE_ME; charset: UTF-8');
header('Cache-Control: must-revalidate');
$offset = 60 * 60 * 2 ;
$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s',time() + $offset) . ' GMT';
header($ExpStr);

/* Stuff to generate your large files here */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文