Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't uncompress correctly #15

Closed
znorman-harris opened this issue Mar 21, 2021 · 1 comment
Closed

Doesn't uncompress correctly #15

znorman-harris opened this issue Mar 21, 2021 · 1 comment

Comments

@znorman-harris
Copy link

I'm not using this library anymore, but I thought you should know it is unable to recreate a Uint8Array.

Here is my modified example from your code and the outputs below which don't match. Also, a bunch of string code is printed out to the console if I try any use anything else (like Gzip or Gunzip).

import { Deflate, Inflate } from 'zlibt2';

// create float data
const datas = [1, 2, 3, 4, 5, 6];
const floats = new Float32Array(datas.length);
for (let i = 0; i < datas.length; i++) {
  floats[i] = datas[i];
}

// represent float as byte
const floatsAsByte = new Uint8Array(floats.buffer);

// compress
const deflate = new Deflate(floatsAsByte, {});
const compress = deflate.compress();

//decompress
const inflate = new Inflate(compress);
const plain = inflate.decompress();

// compare results
console.log(floatsAsByte);
console.log(plain);

And the output:

Uint8Array(24) [
  0, 0, 128, 63, 0, 0,   0, 64,
  0, 0,  64, 64, 0, 0, 128, 64,
  0, 0, 160, 64, 0, 0, 192, 64
]
Uint8Array(11) [
   0, 0, 128, 63, 0,
   0, 0,  64,  0, 0,
  64
]

@MadfishDT
Copy link
Owner

const floats = new Float32Array(datas.length);
     for (let i = 0; i < datas.length; i++) {
         floats[i] = datas[i];
     }
     const floatsAsByte = new Uint8Array(floats.buffer);
     console.log("number array to Uint8Array: ", floatsAsByte);
     // compress
     const deflate = new Zlib.Deflate(floatsAsByte, { compressionType: 0 });
     const compress = deflate.compress();
     //decompress
     const inflate = new Zlib.Inflate(compress);
     const plain = inflate.decompress();

     const decombuffer = new Uint8Array(plain);
     const floats_plain = new Float32Array(decombuffer.buffer);
     console.log(
         "float, uint8 array to decomress to number array: ",
         Array.from(floats_plain)
     );

please use compressionType: 0 option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants