Skip to content

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

@zhouyang9967

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?

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