DEV Community

Michael Salaverry
Michael Salaverry

Posted on

2 2

Append a file into a zip file in Python

I was trying to find a way to append a file into a zip file in python, but I could not find an easy way to do it.

When using zipfile built into python, using the 'a' append method doesn't overwrite files the way I expected it to. So this python module will overwrite the existing file when appending a file (which to me is the obvious solution).

There's no lack of StackOverflow posts and answers, but all of those were too specific to the specific post/issue.

So I decided to scratch my own itch, and make a library to handle it for myself and others.

https://pypi.org/project/appendzip/0.0.4/

GitHub logo barakplasma / append-zip

append a file into an existing zip file, overwriting the existing file of the same name if needed

append-zip

Appends a file to a zip file, overwriting the existing file there if necessary

Performance

Not efficient; extracts all the files in the zip, copies over the new file, and compresses a brand new zip replacing the original one. You will need enough disk space to duplicate the zip file.

Caveats

for some reason, windows has a different file length after unzipping (by 10-20 bytes). So beware how this works on windows

Getting started

install (on Mac) $ python3 -m pip install appendzip

from appendzip.appendzip import appendzip
# before appendzip calendar.txt in the zip archive test.zip contains 2021-01-02
# before appendzip calendar.txt outside the zip contains 2022-02-03
appendzip(
            pathlib.Path('test.zip'),
            pathlib.Path('calendar.txt'),
            'calendar.txt'
)
# after appendzip calendar.txt inside the zip contains 2022-02-03
# after, there is still only one file in the zip archive test.zip
Enter fullscreen mode Exit fullscreen mode



MIT licensed

example of how to use it:

install (on Mac)
$ python3 -m pip install appendzip

from appendzip.appendzip import appendzip
# before appendzip calendar.txt in the zip archive test.zip contains 2021-01-02
# before appendzip calendar.txt outside the zip contains 2022-02-03
appendzip(
            pathlib.Path('test.zip'),
            pathlib.Path('calendar.txt'),
            'calendar.txt'
)
# after appendzip calendar.txt inside the zip contains 2022-02-03
# after, there is still only one file in the zip archive test.zip
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (1)

Collapse
 
barakplasma profile image
Michael Salaverry •

by the way, I want to fix the import, but I'm having a hard time figuring out setup.cfg

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more