Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js/Javascript Equivalent of Java's Deflater class

I have a Java backend that uses Inflater. I wish to feed data to it via Node.js.

Is there any equivalent to the Deflater class?

EDIT: I should clarify a little. I have tried using https://github.com/dankogai/js-deflate and then base64 encoding the result and passing it to a very simple Java program that base64 decodes it and tries to inflate it (creating a simple emulation of the backend), but I keep getting a exception:

java.util.zip.DataFormatException: unknown compression method
    at java.util.zip.Inflater.inflateBytes(Native Method)
    at java.util.zip.Inflater.inflate(Unknown Source)
    at java.util.zip.Inflater.inflate(Unknown Source)

And I know the Base64 encoding process is working correctly on both ends.

I should also note that I cannot change the Java backend.

like image 990
Alec Gorge Avatar asked Nov 15 '22 03:11

Alec Gorge


1 Answers

You can just play around with https://github.com/waveto/node-compress

var compress=require("./compress");
var sys=require("sys");
var posix=require("posix");

// Create gzip stream
var gzip=new compress.Gzip;
gzip.init();

// Pump data to be compressed
var gzdata1 = gzip.deflate("My data that needs ", "binary"); 
sys.puts("Compressed size : "+gzdata1.length);

var gzdata2 = gzip.deflate("to be compressed. 01234567890.", "binary"); 
sys.puts("Compressed size : "+gzdata2.length);

var gzdata3=gzip.end();
sys.puts("Last bit : "+gzdata3.length);

// Take the output stream, and chop it up into two
var gzdata = gzdata1+gzdata2+gzdata3;
sys.puts("Total compressed size : "+gzdata.length);
var d1 = gzdata.substr(0, 25);
var d2 = gzdata.substr(25);

// Create gunzip stream to decode these
var gunzip = new compress.Gunzip;
gunzip.init();
var data1 = gunzip.inflate(d1, "binary");
var data2 = gunzip.inflate(d2, "binary");
var data3 = gunzip.end();

sys.puts(data1+data2+data3);

for this works fine, but got some issues, as i played with node_pcap. i think this could be good start to look at.

like image 92
Mario Scheliga Avatar answered Nov 16 '22 17:11

Mario Scheliga