forked from waveto/node-compress
-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
42 lines (35 loc) · 1.23 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var gzbz2 = require("./gzbz2");
var util = require("util");
var fs = require("fs");
// Read in our test file
var testfile = process.argv[2] || "test.js";
var enc = process.argv[3];
var data = fs.readFileSync(testfile, enc);
util.puts("Got : " + data.length);
// Set output file
var fd = fs.openSync(testfile + ".gz", "w", 0644);
util.puts("File opened");
// Create gzip stream
var gzip = new gzbz2.Gzip;
gzip.init({level:3});
// Pump data to be gzbz2
var gzdata = gzip.deflate(data, enc); // Do this as many times as required
util.puts("Compressed chunk size : " + gzdata.length);
fs.writeSync(fd, gzdata, 0, gzdata.length, null);
// Get the last bit
var gzlast = gzip.end();
util.puts("Compressed chunk size: " + gzlast.length);
fs.writeSync(fd, gzlast, 0, gzlast.length, null);
fs.closeSync(fd);
util.puts("File closed");
// See if we can uncompress it ok
var gunzip = new gzbz2.Gunzip;
gunzip.init({encoding: enc});
var testdata = fs.readFileSync(testfile + ".gz");
util.puts("Test opened : " + testdata.length);
var inflated = gunzip.inflate(testdata, enc);
util.puts("GZ.inflate.length: " + inflated.length);
gunzip.end(); // no return value
if (data.length != inflated.length) {
util.puts('error! input/output string lengths do not match');
}