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

Problem with big zip files #258

Closed
vivkvv opened this issue May 30, 2022 · 5 comments · Fixed by #262
Closed

Problem with big zip files #258

vivkvv opened this issue May 30, 2022 · 5 comments · Fixed by #262
Labels
bug Something isn't working help wanted Extra attention is needed zip64 Support for 64bit zip

Comments

@vivkvv
Copy link

vivkvv commented May 30, 2022

I have big_zip.raw file. It has size ~ 6Gb. Then I zip its and big_zip.zip has size ~ 20 Mb.

The following code does not work:

    struct zip_t *zip = zip_open("big_zip.zip", 0, 'r');

    auto cnt = zip_entries_total(zip);

    auto err = zip_entry_openbyindex(zip, 0);

    const char *name = zip_entry_name(zip);
    int isdir = zip_entry_isdir(zip);
    size_t bufsize = zip_entry_size(zip); // right size = ~6Gb
    unsigned int crc32 = zip_entry_crc32(zip);

    void *buf = (void*) new unsigned int[bufsize] {1}; // in 64bit version works ok

    bufsize = zip_entry_read(zip, (void **)&buf, &bufsize);

zip_entry_read here returns 0

@kuba--
Copy link
Owner

kuba-- commented May 31, 2022

@vivkvv - I assume you have just one 6GB entry.
So, my first question is - can you allocate 12 GB? Because, actually this is what you're doing.
zip_entry_read allocates memory for you and returns new allocated output buffer and buffer size (look at readme or docs:

zip/src/zip.h

Line 307 in 203ef13

extern ZIP_EXPORT ssize_t zip_entry_read(struct zip_t *zip, void **buf,
).

If you want to pre-allocate memory, that's fine, but I'd suggest to use zip_entry_noallocread (

zip/src/zip.h

Line 327 in 203ef13

extern ZIP_EXPORT ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf,
).

Apart from memory leak (what maybe is not a problem in your case), I'm guessing, it was a problem to allocate another 6GB.

@vivkvv
Copy link
Author

vivkvv commented Jun 1, 2022

@kuba--
Thanks. Of course, a memory leak is my mistake. But real problem is that function mz_zip_reader_extract_to_heap has the lines

  comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);

and the last one returns 4294967295 (0xffffff). It is wrong because it is needed to take size from additional fields in this case.
And then function mz_zip_reader_extract_to_mem_no_alloc returns an error

  if (buf_size < needed_size)
    return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);

because buf_size is 4294967295, but needed_size is 5998805513.

Call stack is

mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive * pZip, unsigned int file_index, void * pBuf, unsigned __int64 buf_size, unsigned int flags, void * pUser_read_buf, unsigned __int64 user_read_buf_size) Line 6499
mz_zip_reader_extract_to_mem(mz_zip_archive * pZip, unsigned int file_index, void * pBuf, unsigned __int64 buf_size, unsigned int flags) Line 6631
mz_zip_reader_extract_to_heap(mz_zip_archive * pZip, unsigned int file_index, unsigned __int64 * pSize, unsigned int flags) Line 6669
zip_entry_read(zip_t * zip, void * * buf, unsigned __int64 * bufsize) Line 1445

@kuba--
Copy link
Owner

kuba-- commented Jun 2, 2022

Ok, thanks for trouble shooting - looks like 32bit issue. I'll take a look.

@kuba-- kuba-- added bug Something isn't working help wanted Extra attention is needed zip64 Support for 64bit zip labels Jun 2, 2022
@kuba-- kuba-- linked a pull request Jun 4, 2022 that will close this issue
@kuba--
Copy link
Owner

kuba-- commented Jun 4, 2022

@vivkvv - I've upgraded some miniz internals, what should fix your problem.
Please take a look PR: https://github.com/kuba--/zip/pull/262 or test your file against the branch:
https://github.com/kuba--/zip/tree/fix-258

@vivkvv
Copy link
Author

vivkvv commented Jun 5, 2022

Thanks. I've checked and it works now.

@vivkvv vivkvv closed this as completed Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed zip64 Support for 64bit zip
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants