const zlib = require('zlib');
const fs = require('fs');
// generate data
let buf = Buffer.alloc(4 * 240 * 135);
for (let y = 0, i = 0; y < 135; ++y) {
for (let x = 0; x < 240; ++x, ++i) {
buf.writeFloatBE(x*y, i*4);
}
}
// compress
let bufCompress = zlib.deflateSync( buf );
console.log( bufCompress );
// write file
let file = fs.openSync('data.z', 'w+');
fs.writeSync( file, 'test\nabc\n' ); // write some header
fs.writeSync( file, bufCompress, 0, bufCompress.length ); // write compressed data
fs.closeSync( file );
// read file
let readBuf = fs.readFileSync('data.z');
let readArr = readBuf.toString().split('\n');
console.log( readArr[0] );
console.log( readArr[1] );
let offset = readArr[0].length + readArr[1].length + 2;
bufCompress = readBuf.slice(offset);
console.log( bufCompress );
// decompress
let bufUncompress = zlib.inflateSync( bufCompress );
// check data
for (let i = 0; i < 240*135*4; ++i) {
if ( buf[i] != bufUncompress[i] ) {
console.log( 'error' );
}
}
console.log( buf.length );
console.log( bufCompress.length );
console.log( bufUncompress.length );