简体   繁体   English

javascript 文本压缩/解压

[英]javascript text compression/decompression

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.假设我有一个 400K 的文本文件,我想从 javascript 读取它。问题是,我的目标受众的连接速度很慢,所以 400k 可能需要很长时间才能加载。

I suppose I need to compress the file, but well, how can I decompress it via javascript on the client side?我想我需要压缩文件,但是,我怎样才能通过客户端的 javascript 解压呢?

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.需要明确的是,该文件是文本(数据)而不是代码。

You can GZip the text file, and sent it to the browser. 您可以GZip文本文件,并将其发送到浏览器。 That way you wont have to do anything on the client side, the browser itself will decompress it. 这样你就不必在客户端做任何事情,浏览器本身会解压缩它。

你可以使用HTTP压缩吗?

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

A few tests with a LOT of text turned up a pretty good result. 一些带有大量文字的测试结果非常好。

[Old question, but just in case other people stumble across this...] [老问题,但万一其他人偶然发现了这个...]

The best way to deal with this is to use HTTP compression . 处理此问题的最佳方法是使用HTTP压缩 All major web servers and web browsers support this. 所有主要的Web服务器和Web浏览器都支持此功能 In fact, if you're using a web hosting service and your file is a filetype that commonly requires compression (eg css, js, html), then it's probably already being compressed for transport and you're just not aware of it. 事实上,如果您使用的是网络托管服务,并且您的文件是通常需要压缩的文件类型(例如css,js,html),那么它可能已经被压缩用于传输而您只是没有意识到它。

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. 注意:您可以明确指定所有MimeType(响应格式)Ex。

Below is tomcat server setting parameter. 下面是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"
   />

To be sure that the compression is working you can check. 要确保压缩工作正常,您可以检查。 press F12(developer's tool)[mine is Chrome] -> Network tab -> start recording. 按F12(开发人员的工具)[我的是Chrome] - >网络选项卡 - >开始录制。 click the request url that is responding the required data/text. 单击响应所需数据/文本的请求URL。 Then go to the header's tab. 然后转到标题选项卡。

you will see. 你会看见。

REquest/REsponse Headers 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. 并且网络选项卡中的size列再次显示url将显示缩小的大小。

[very old question, but still showing up in searches] [非常老的问题,但仍然出现在搜索中]

I ended up writing a small, self-contained library for the task.我最终为这项任务编写了一个小型的独立库。 It compresses/decompresses JSON data to a string in the printable ASCII range.它将 JSON 数据压缩/解压缩为可打印 ASCII 范围内的字符串。 The size of the minified decompressor is 2410 bytes, and 3177 bytes for the compressor.缩小解压缩器的大小为 2410 字节,压缩器为 3177 字节。

https://github.com/TGlas/json-compression https://github.com/TGlas/json-compression

DECOMPRESS JAVASCRIPT DECOMPRESS JAVASCRIPT

Is IMPOSSIBLE to hide the compressed code. 隐藏压缩代码是不可能的

A typical JavaScript compressed with /packer/ starts with the following code: 使用/ packer /压缩的典型JavaScript使用以下代码启动:

      `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. eval函数计算包含JavaScript的字符串参数。 In most packers, eval is used, followed by document.write . 在大多数打包器中,使用eval ,然后是document.write

To decompress JavaScript, replace these methods by one of the following: 要解压缩JavaScript,请使用以下方法之一替换这些方法:

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 ) 1.通过alert替换eval如下所示: alert(function(p,a,c,k,e,r) ... 并打开或刷新html页面,警报将只是在弹出窗口中打印代码

2. If the JavaScript appears after the <body> element, you can add a <textarea> like so: 2.如果JavaScript出现在<body>元素之后,您可以像这样添加<textarea>

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

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

From both options the first is more simple... Good luck :) 从两个选项中,第一个更简单...祝你好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM