简体   繁体   English

Javascript字符串压缩和PHP / Ruby解压缩

[英]Javascript string compression and PHP/Ruby decompression

Is there a Javascript compression and PHP/Ruby decompression library for strings? 是否有字符串的Javascript压缩和PHP / Ruby解压缩库? I need it because I need to send a very long text string using Ajax on a slow upload link to a web server which uses PHP/Ruby as server-side language. 我需要它,因为我需要在一个使用PHP / Ruby作为服务器端语言的Web服务器的慢上传链接上使用Ajax发送一个非常长的文本字符串。

var x = $('#sources').html();
// a very-very long text
var xo = x, o = {};
if(x.length>512*1024) {
  x = compress(x);
  o.c = 1;
}
o.x = x;
$.post('target.php',o,function(res){alert(res==xo)});

On server side (for example, PHP): 在服务器端(例如,PHP):

<?php
  if(isset($_POST['c']) && $_POST['c']=='1') {
    $x = decompress($_POST['x']);
  } else {
    $x = $_POST['x'];
  }
  echo $x;

There are many JS implementations of the most common compression algorithm, Zip. 有许多JS实现最常见的压缩算法Zip。

For example zip.js 例如zip.js

Zip is of course also supported in PHP . Zip当然也支持PHP

That should do it 应该这样做

http://webdevwonders.com/lzw-compression-and-decompression-with-javascript-and-php/ http://webdevwonders.com/lzw-compression-and-decompression-with-javascript-and-php/

LZW is good for strings that actually contain human readable text LZW适用于实际包含人类可读文本的字符串

Assuming that you send your files over http, I would suggest that you let your web server handle this by sending file with gzip content-encoding. 假设您通过http发送文件,我建议您通过发送带有gzip内容编码的文件让您的Web服务器处理此问题。

If for example you use Apache, you can enable mod_deflate 例如,如果您使用Apache,则可以启用mod_deflate

If for some reason you can't change your web server configuration, php also has a built-in gzip handler you could use instead. 如果由于某种原因你不能改变你的web服务器配置,php也有一个你可以使用的内置gzip处理程序。 See: ob_gzhandler 见: ob_gzhandler

Edit: 编辑:

As for client to server, it doesn't look like this is directly supported by any XmlHttpRequest implementations. 至于客户端到服务器,它看起来不像任何XmlHttpRequest实现直接支持。 You could perhaps find a custom gzip compression algorithm for Javascript and then set the request-header to indicate that it's compressed .That way it becomes transparently decoded by the web server and you don't have to do anything special in php. 您也许可以为Javascript找到一个自定义的gzip压缩算法,然后设置请求标头以指示它已经被压缩。这样它就会被Web服务器透明地解码,你不需要在php中做任何特殊的事情。

See this page: JavaScript implementation of Gzip . 看此页: Gzip的JavaScript实现

Running a google search for "ruby decompress string" comes up with this: How to decompress Gzip string in ruby? 运行谷歌搜索“红宝石解压缩字符串”出现这个: 如何解压缩红宝石中的Gzip字符串? which is what you're looking for. 这就是你要找的东西。

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

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