Alec Gorge
Alec Gorge

Reputation: 17400

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.

Upvotes: 5

Views: 1752

Answers (3)

Alec Gorge
Alec Gorge

Reputation: 17400

Figured it out finally. You can use the zlib library in Node.js to do this. For example:

var zip = require('zlib');
zip.createDeflate({
    level: zip.Z_DEFAULT_COMPRESSION,
    windowBits: zip.MAX_WBITS
});

or

zip.deflate(buf,callback);

Upvotes: 1

Mario Scheliga
Mario Scheliga

Reputation: 11

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.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533720

You can use GZIPInput/OutputStream instead which is based on Deflator/Inflator AFAIK, Inflator/Deflator is Java specific.

Upvotes: 0

Related Questions