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

Error inflating stream #30

Open
gudlaugu opened this issue Mar 29, 2022 · 1 comment
Open

Error inflating stream #30

gudlaugu opened this issue Mar 29, 2022 · 1 comment

Comments

@gudlaugu
Copy link

I am getting a logic error, "Error inflating stream", when reading larger arrays from an npz archive. After a lot of debugging I found out that by increasing the size of CHUNK in zip.cpp so that the file could be read in a single pass everything worked as expected.

Here is the solution I came up with. Basically replacing the npy_inflate() call in the read_file function in npz.cpp with a single pass inflation. Everything is anyway in memory all the time and there is no need to split the inflation step up into chunks.

   //Use one step inflation, we anyway need to hold everything in memory at some point
   std::vector<std::uint8_t> compressed_bytes(uncompressed_bytes);
   uncompressed_bytes.resize(entry.uncompressed_size);

   //Initialize miniz
   z_stream strm;
   strm.zalloc = Z_NULL;
   strm.zfree = Z_NULL;
   strm.opaque = Z_NULL;
   strm.avail_in = 0;
   strm.next_in = Z_NULL;
   const int WINDOW_BITS = -15;
   auto ret = inflateInit2(&strm, WINDOW_BITS);
   if (ret != Z_OK)
   {
      throw std::logic_error("Unable to initialize inflate algorithm");
   }

   //Inflate in one step
   strm.next_in = &compressed_bytes[0];
   strm.avail_in = compressed_bytes.size();
   strm.next_out = &uncompressed_bytes[0];
   strm.avail_out = uncompressed_bytes.size();

   ret = inflate(&strm, Z_FINISH);
   if (ret != Z_STREAM_END) {
      std::cerr<<strm.msg<<std::endl;
      (void)inflateEnd(&strm);
      throw std::logic_error("Error inflating stream");
   }
   (void)inflateEnd(&strm);
@matajoh
Copy link
Owner

matajoh commented Sep 7, 2022

I've not seen this issue in testing, but I'll see if I can reproduce it. That said, the solution seems fine. If it checks out on my end, I'll make the change.

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