kuba-- / zip

A portable, simple zip library written in C
MIT License
1.39k stars 272 forks source link

Fix zip_delete_entries bug #320

Closed howish closed 9 months ago

howish commented 10 months ago

Purpose

Fix bugs in zip_delete_entires which crashed the central directory states.

Elaborate

I discover these bug when trying to use this API along with other entry writing APIs in same zip_open/zip_close section, for example:

    struct zip_t* obj = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
    {
        const char* entname = "bar.txt";
        // Delete the old entries with same names
        zip_delete_entires(obj, &entname, 1);
        // Add the new entry
        zip_entry_open(obj, entname);
        zip_entry_write(obj, "foobar", sizeof("foobar"));
        zip_entry_close(obj);
    }
    zip_close(obj);
kuba-- commented 10 months ago

Purpose

Fix bugs in zip_delete_entires which crashed the central directory states.

Elaborate

I discover these bug when trying to use this API along with other entry writing APIs in same zip_open/zip_close section, for example:

    struct zip_t* obj = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
    {
        const char* entname = "bar.txt";
        // Delete the old entries with same names
        zip_delete_entires(obj, &entname, 1);
        // Add the new entry
        zip_entry_open(obj, entname);
        zip_entry_write(obj, "foobar", sizeof("foobar"));
        zip_entry_close(obj);
    }
    zip_close(obj);

This is not expected use case. You cannot delete and append new elements on the same opened archive. You have to close archive first (to refine central directory), and then you can reopen and append stuff.

kuba-- commented 10 months ago

@jinfeihan57 - can you also take a look into this PR, becaue you mainly implemented delete mode, thanks!

kuba-- commented 9 months ago

I'm closing right now. If you find time to elaborate more about the fix, do not hesitate to re-open the issue. Keep in mind - either you open a zip for reading, writing, appending or deleting. Do not mix operations.