floydpink/lzwCompress.js

lzwCompress.unpack expected input type

Closed this issue · 1 comments

First, I would like to thank you about this library.
I am trying to save some disk space storing a big array of JSON (items here). The problem I am facing probably comes from my understanding of Node.js or the library please bare with me for a moment.

I put comments on code I am trying, first I compress items then store results as string, all works well but the size is not shrinked which is normal, so I tried encoding the array of numbers (result of lzwCompress.pack) to a Uint8Array array before saving it to disk as a binary file.
Now, When I read the file back, I feed it directly to lzwCompress.unpack, which does not work. So my problem probably comes from
encoding before saving
encoding after reading
Or the expected type of array from lzwCompress.unpack

const fs = require('fs');
const lzwCompress = require('lzwcompress');

// to compress objects
const compressed = lzwCompress.pack(items);
// to uncomress
const original = lzwCompress.unpack(compressed);

// This way there is no gain in memory but the contrary
fs.writeFileSync('original.bin', JSON.stringify(items), 'binary');
// because I am saving the array of numbers as string
fs.writeFileSync('compressed1.bin', compressed, 'binary');

// Perhapse I need encoding then,
const _16384 = compressed.length;
const buffer = new Uint8Array(_16384); // create a buffer of some length
for (let index = 0; index < _16384; index++) {
  buffer[index] = compressed[index];
}
// Oh very nice, now I see the file is very very compressed!!!
fs.writeFileSync('compressed2.bin', buffer, 'binary');

// but now I want to get my compressed data back
// from what I read, readFileSync returns a Uint8Array already
const encoded = fs.readFileSync('compressed2.bin', 'binary');
const original2 = lzwCompress.unpack(encoded);                // <-------------- HERE IS MY PROBLEM, NORMALLY  unpack ACCEPTS encoded WITHOUT ANY FURTHER TRANSFORMATIONS, RIGHT ???
console.log(original2);               // <-------------- THIS SHOWS GIBBERISH

Thanks a lot

I opted for Zlib, but thanks a lot,
Please feel free to close this or answer for follow users.