Noah Mendoza
Noah Mendoza

Reputation: 837

Writing data to a zip archive in Python

I've been told in the past that there is simply no easy way to write a string a zip file. It's okay to READ from a zip archive, but if you want to write to a zip file, the best option is to extract it, make the changes, and then zip it back up again. However, the library I am using (openpyxl) accomplishes the feat of writing to a zip file without any extraction. This package uses the writestr() function in the python ZipFile library to make changes. Can someone explain to me how exactly this is possible? I know it has something to do with writing bytes but I can't fine a good explanation.

I'm aware of the vagueness of this question, but that's a circumstance of my lack of knowledge on the topic.

Upvotes: 0

Views: 365

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19497

openpyxl does not modify the files in place because you can't do this with zipfiles. You must extract, modify and archive. We just hide this process in the library.

Upvotes: 1

Related Questions