8000 When I closed the file, I found the file was damaged · Issue #381 · zlib-ng/minizip-ng · GitHub
[go: up one dir, main page]

Skip to content

When I closed the file, I found the file was damaged #381

Closed

Description

@zhouyang9967

When I only open and close a compressed package, I find that its size is larger and corrupted.
For Example:
zipFile zf;
char *filePath = "xxx";
zf = zipOpen(filePath, 1);
int err = zipClose(zf,NULL);

I took a look at the code and changed it to solve the problem, but I don't know if the change will affect other parts.
Change the following(mz_zip.c):

    if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
{
err = mz_zip_read_cd(zip);
    if ((mode & MZ_OPEN_MODE_CREATE) == 0)
    {
        //err = mz_zip_read_cd(zip);
        if (err != MZ_OK)
        {
            mz_zip_print("Zip - Error detected reading cd (%"PRId32")", err);
            if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
                err = MZ_OK;
        }
    }
    
    if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
    {
        if (zip->cd_size > 0)
        {
            /* Store central directory in memory */
            err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
            if (err == MZ_OK)
                err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
            if (err == MZ_OK)
                err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
        }
        else
        {
            /* If no central directory, append new zip to end of file */
            err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
        }

        if (zip->disk_number_with_cd > 0)
        {
            /* Move to last disk to begin appending */
            mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
        }
    }
    else
    {
        zip->cd_start_pos = zip->cd_offset;
    }
}       

I put 'err = mz_zip_read_cd(zip)' in front

I also found a problem when I deleted some of the comments in a compressed package with comments and found that its size did not change, But I use winRAR modified, compressed package size will be smaller.Is there any way to reconcile the two?

Activity

  1. nmoinvaz commented on Apr 19, 2019

    @nmoinvaz
    Member

    If you change that, it will not work for some open modes. If you attach the zip file you are having problems with and the repo steps and code then I can try and track it down.

  2. zhouyang9967 commented on Apr 22, 2019

    @zhouyang9967
    Author

    cert-v1.1.1.1-c426f4aca002d697898ceae925a9f85f.zip
    cert-v1.1.1.1-c426f4aca002d697898ceae925a9f85f.zip
    The first is to open the closed zip file, and the second is corrupted,The code performs only the open and close operations as in the example

  3. zhouyang9967 commented on Apr 22, 2019

    @zhouyang9967
    Author

    The above one is modified comment, this one is not modified
    example.zip
    example.zip

  4. nmoinvaz commented on Apr 22, 2019

    @nmoinvaz
    Member

    In both instances, your zip files do not contain a central directory. You can open it in recovery mode using the new api function mz_zip_set_recover and that will create a central directory if set before opening. I can make a slight change that will not try and save the central directory header if the central directory size is zero. It would allow you to at least open and close without corruption. But if you are going to make modifications you will need a central directory. This is list on the Wiki page under Limitations.

  5. added
    fixedIssue or bug has been fixed
    zipZIP file format
    and removed
    need more infoThere is not enough information
    on Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedIssue or bug has been fixedzipZIP file format

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0