javascript 文本压缩/解压缩
假设我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以 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.
你能使用 HTTP 压缩吗?
could you use HTTP compression?
这看起来很有趣:
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.
[老问题,但以防万一其他人偶然发现这个...]
处理这个问题的最佳方法是使用 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.
是的,您不必自己处理压缩/解压缩,浏览器会为您处理,只需为您正在使用的服务器指定服务器参数。
注意:您可以明确指定所有 MimeType(response-format's)
前任。
以下是tomcat服务器设置参数。
为了确保压缩正常工作,您可以检查。
按F12(开发者工具)[我的是Chrome] -> 网络选项卡 -> 开始录音。
单击响应所需数据/文本的请求 URL。
然后转到标题选项卡。
你会看见。
请求/响应标头
使用编码的 。 网络选项卡中针对 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.
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
that it using encoding. and the size column in the network tab againsr the url will be showing the reduced size.
DECOMPRESS JAVASCRIPT
隐藏压缩代码不可能。
使用 /packer/ 压缩的典型 JavaScript 以以下代码开头:
eval
函数计算包含 JavaScript 的字符串参数。 在大多数加壳器中,使用eval
,然后使用document.write
。要解压缩 JavaScript,请将这些方法替换为以下方法之一:
1. 将
eval
替换为alert
(像这样:alert(function(p,a,c,k,e,r)
... 并打开或刷新 html 页面,警报只会在弹出窗口中打印代码)2. 如果 JavaScript 出现在
元素之后,您可以添加 < code>
然后,将
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:
The
eval
function evaluates a string argument that contains JavaScript. In most packers,eval
is used, followed bydocument.write
.To decompress JavaScript, replace these methods by one of the following:
1. Replace
eval
byalert
(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:Then, replace
eval
(…); bydocument.getElementById("code").value=…;
From both options the first is more simple... Good luck :)