zlib-ng / minizip-ng

Fork of the popular zip manipulation library found in the zlib distribution.
Other
1.25k stars 434 forks source link

Minizip not working properly when compressing file > 4 GB size #793

Closed Neena1414 closed 3 months ago

Neena1414 commented 4 months ago

My program uses minizip(zlib- version 1.2.3) to compress an xml file. When the xml file size is greater than 4 GB(in fact the max value of int 32 - 4294967295) then I find that when I try to extract the compressed file it gave me an unexpected error 0x80004005 (able to extract via 7zip). I believe the zipping did not work properly. If I check the size of the xml file then the size =(total bytes in the file) -(max value of int32). Please see the screenshot.

image (1)

Now coming to the way the file is being zipped I am using below minizip fnctions with these params. If any of you are aware what is going wrong in the minizip compresiion for large files could you please give me suggestions

int returnVal = zipOpenNewFileInZip4(reinterpret_cast(m_pzipFile), fileName, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, NULL, 0, m_uGeneralflag);//Z_DEFLATED-0,Z_DEFAULT_COMPRESSION=-1,MAX_WBITS=15,DEF_MEM_LEVEL=8,Z_DEFAULT_STRATEGY=0

zipWriteInFileInZip(reinterpret_cast(m_pzipFile)// pBuffer, actualBufferSize)===>m-pzipFile void,pBuffer of type char that is being written to compressed file, actualBufferSize --int value that is size of pBuffer zipCloseFileInZip(reinterpret_cast(m_pzipFile))

Nekto89 commented 4 months ago

you have to call write multiple times. Smth like

while (i_size != 0) {
    // minizip-ng has limit in zipWriteInFileInZip
    constexpr auto max_chunk_size = static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max() - 1);
    const auto write_size = std::min(max_chunk_size, i_size);
    i_size -= write_size;
    if (zipWriteInFileInZip(m_pzipFile, ip_buffer, static_cast<std::uint32_t>(write_size)) < 0)
      throw ...;
    ip_buffer += write_size;
  }
nmoinvaz commented 4 months ago

I do not support older version. It is written by somebody else.

Neena1414 commented 4 months ago

you have to call write multiple times. Smth like

while (i_size != 0) {
    // minizip-ng has limit in zipWriteInFileInZip
    constexpr auto max_chunk_size = static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max() - 1);
    const auto write_size = std::min(max_chunk_size, i_size);
    i_size -= write_size;
    if (zipWriteInFileInZip(m_pzipFile, ip_buffer, static_cast<std::uint32_t>(write_size)) < 0)
      throw ...;
    ip_buffer += write_size;
  }

I do read from a file in chunks of 2048 bytes this is a skeleton of the code do { actualBufferSize = pInputStream->read(pBuffer, g_helperBufferSize); if (actualBufferSize < 0) { // Empty stream, nothing to read break; } returnVal = zipWriteInFileInZip(reinterpret_cast(m_pzipFile), pBuffer, actualBufferSize); if (returnVal != ZIP_OK) { ..... } } while (actualBufferSize == g_helperBufferSize); It is past 4GB(max int 32 size that there is a problem--4294967295) I believe I have to switch zip functions to 64 bit versions like zipOpenNewFileInZip4