Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript string compression and PHP/Ruby decompression

Is there a Javascript compression and PHP/Ruby decompression library for strings? 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.

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
  if(isset($_POST['c']) && $_POST['c']=='1') {
    $x = decompress($_POST['x']);
  } else {
    $x = $_POST['x'];
  }
  echo $x;
like image 564
Kokizzu Avatar asked Feb 14 '13 08:02

Kokizzu


2 Answers

There are many JS implementations of the most common compression algorithm, Zip.

For example zip.js

Zip is of course also supported in PHP.

like image 75
Denys Séguret Avatar answered Oct 10 '22 01:10

Denys Séguret


That should do it

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

LZW is good for strings that actually contain human readable text

like image 33
naugtur Avatar answered Oct 10 '22 00:10

naugtur