kuba-- / zip

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

Support zip archives not starting at zero offset #346

Closed LemonBoy closed 1 month ago

LemonBoy commented 3 months ago

Zip archives are usually read from the back to the front, making it possible to append them to other files (e.g. executables) in order to provide some kind of embedded filesystem.

This PR makes it possible to read, write, and update such zip files without affecting the existing functionality.

What's not possible is to append an archive to an arbitrary non-zip file. The w switch should be able to do so, but at the moment the documentation states 'w': creates an empty file for writing. without rewinding the stream. I'd amend the documentation and make that mode create a brand new zip archive using the stream cursor as starting offset. Would that be ok?

Once the w issue is sorted some tests can be easily written.

kuba-- commented 3 months ago

btw. appending data to existing archive is possible with a flag, e.g.:

struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
{
    zip_entry_open(zip, "foo-3.txt");
    {
        const char *buf = "Append some data here...\0";
        zip_entry_write(zip, buf, strlen(buf));
    }
    zip_entry_close(zip);
}
zip_close(zip);

If we are talking to append to non-zip archive, it can be pretty nice feature. The only concern I have is potential upgrade of miniz. If we can make it as painless as possible for future upgrades, it could be great.

Thanks,

LemonBoy commented 3 months ago

Fair enough, I've reworked the patch and submitted it to miniz.

Once/when it's merged I'll update this patch too.

LemonBoy commented 3 months ago

The latest commit updated miniz and makes some minimal changes to zip itself, there's a bunch of refactoring to avoid the need to introduce the noreopen and rpb functions in miniz.

If you want to perform the miniz update yourself and re-add whatever patches were applied feel free to do so.

kuba-- commented 2 months ago

@LemonBoy - PTAL, I submitted some changes to your PR. It's mostly related to fix permission tests. So, I also added back external attributes (what is useful on UNIX).

LemonBoy commented 2 months ago

LGTM, I'd advise to submit as many changes as possible to the upstream to simplify further updated down the road.