javascript 文本压缩/解压缩

发布于 2024-07-25 09:44:50 字数 236 浏览 2 评论 0原文

假设我有一个 400K 的文本文件,我想从 javascript 中读取它。 问题是,我的目标受众的连接速度很慢,因此 400k 可能需要很长时间才能加载。

我想我需要压缩文件,但是,如何在客户端通过 javascript 解压缩它呢?

值得吗?或者解压所需的时间会抵消下载所节省的时间吗?

更新

需要明确的是,该文件是文本(数据)而不是代码。

Suppose I have a 400K text file which I want to read from a javascript. The problem is, my target audience have a slow connection, so 400k might take too long to load.

I suppose I need to compress the file, but well, how can I decompress it via javascript on the client side?

Is it worth it, or will the time needed for decompression negate the time saved in downloading?

UPDATE

Just to be clear, the file is text (data) not code.

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

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

发布评论

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

评论(6

淡墨 2024-08-01 09:44:50

您可以 GZip 该文本文件,并将其发送到浏览器。 这样你就不必在客户端做任何事情,浏览器本身会解压缩它。

You can GZip the text file, and sent it to the browser. That way you wont have to do anything on the client side, the browser itself will decompress it.

太阳哥哥 2024-08-01 09:44:50

你能使用 HTTP 压缩吗?

could you use HTTP compression?

拥抱没勇气 2024-08-01 09:44:50

这看起来很有趣:
http://rumkin.com/tools/compression/compress_huff.php

一些测试大量的文字得到了很好的结果。

This looks interesting:
http://rumkin.com/tools/compression/compress_huff.php

A few tests with a LOT of text turned up a pretty good result.

依 靠 2024-08-01 09:44:50

[老问题,但以防万一其他人偶然发现这个...]

处理这个问题的最佳方法是使用 HTTP 压缩。 所有主要的网络服务器和网络浏览器都支持这一点。 事实上,如果您正在使用网络托管服务,并且您的文件是通常需要压缩的文件类型(例如 css、js、html),那么它可能已经被压缩以供传输,而您只是没有意识到。

[Old question, but just in case other people stumble across this...]

The best way to deal with this is to use HTTP compression. All major web servers and web browsers support this. In fact, if you're using a web hosting service and your file is a filetype that commonly requires compression (e.g. css, js, html), then it's probably already being compressed for transport and you're just not aware of it.

喜你已久 2024-08-01 09:44:50

是的,您不必自己处理压缩/解压缩,浏览器会为您处理,只需为您正在使用的服务器指定服务器参数。
注意:您可以明确指定所有 MimeType(response-format's)
前任。

以下是tomcat服务器设置参数。

   <Connector port="8443" 
     protocol="org.apache.coyote.http11.Http11Protocol"
           SSLEnabled="true"
           maxThreads="200"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8,application/x-www-form-urlencoded,text/html,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json"
   />

为了确保压缩正常工作,您可以检查。
按F12(开发者工具)[我的是Chrome] -> 网络选项卡 -> 开始录音。
单击响应所需数据/文本的请求 URL。
然后转到标题选项卡。

你会看见。

请求/响应标头

Content-Encoding:gzip

Accept-Encoding: gzip, deflate

使用编码的 。 网络选项卡中针对 url 的尺寸列将显示缩小后的尺寸。

Yes you don't not have to handle compression/decompression yourself browser does it for you only you have to specify server parameter's for the server that you are using.
Note: you can explicitly specify for what all MimeType( response-format's)
Ex.

Below is tomcat server setting parameter.

   <Connector port="8443" 
     protocol="org.apache.coyote.http11.Http11Protocol"
           SSLEnabled="true"
           maxThreads="200"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8,application/x-www-form-urlencoded,text/html,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json"
   />

To be sure that the compression is working you can check.
press F12(developer's tool)[mine is Chrome] -> Network tab -> start recording.
click the request url that is responding the required data/text.
Then go to the header's tab.

you will see.

REquest/REsponse Headers

Content-Encoding:gzip

Accept-Encoding: gzip, deflate

that it using encoding. and the size column in the network tab againsr the url will be showing the reduced size.

留蓝 2024-08-01 09:44:50

DECOMPRESS JAVASCRIPT

隐藏压缩代码不可能

使用 /packer/ 压缩的典型 JavaScript 以以下代码开头:

      `eval(function(p,a,c,k,e,r)`…
      `eval` can simply be replaced by `alert`.

eval 函数计算包含 JavaScript 的字符串参数。 在大多数加壳器中,使用eval,然后使用document.write

要解压缩 JavaScript,请将这些方法替换为以下方法之一:

1.eval 替换为 alert像这样: alert(function(p,a,c,k,e,r)... 并打开或刷新 html 页面,警报只会在弹出窗口中打印代码)

2. 如果 JavaScript 出现在 元素之后,您可以添加 < code>

      `<textarea id="code"></textarea>`

然后,将 eval(...); 替换为 document.getElementById("code").value=...;

这两个选项第一个更简单......祝你好运:)

DECOMPRESS JAVASCRIPT

Is IMPOSSIBLE to hide the compressed code.

A typical JavaScript compressed with /packer/ starts with the following code:

      `eval(function(p,a,c,k,e,r)`…
      `eval` can simply be replaced by `alert`.

The eval function evaluates a string argument that contains JavaScript. In most packers, eval is used, followed by document.write.

To decompress JavaScript, replace these methods by one of the following:

1. Replace eval by alert (Like this: alert(function(p,a,c,k,e,r)and open or refresh the html page, the alert will simply print the code in a popup-window)

2. If the JavaScript appears after the <body> element, you can add a <textarea> like so:

      `<textarea id="code"></textarea>`

Then, replace eval(…); by document.getElementById("code").value=…;

From both options the first is more simple... Good luck :)

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