简体   繁体   中英

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.

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.

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压缩吗?

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.

[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 (eg css, js, html), then it's probably already being compressed for transport and you're just not aware of it.

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.

[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. The size of the minified decompressor is 2410 bytes, and 3177 bytes for the compressor.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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