Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file from zipfile with the ZipFile Module

The only way I came up for deleting a file from a zipfile was to create a temporary zipfile without the file to be deleted and then rename it to the original filename.

In python 2.4 the ZipInfo class had an attribute file_offset, so it was possible to create a second zip file and copy the data to other file without decompress/recompressing.

This file_offset is missing in python 2.6, so is there another option than creating another zipfile by uncompressing every file and then recompressing it again?

Is there maybe a direct way of deleting a file in the zipfile, I searched and didn't find anything.

like image 752
RSabet Avatar asked Feb 04 '09 23:02

RSabet


People also ask

How do I delete ZIP file?

1. Right-click the Zip file, and select Move to Trash. 3. Right-click the Zip in Trash, and select Delete Immediately.

Can you delete files within a zip folder?

We can delete files directly under the zip, but can not delete the sub folders or files within them.

What is ZIP file ZIP file in Python?

Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.


1 Answers

The following snippet worked for me (deletes all *.exe files from a Zip archive):

zin = zipfile.ZipFile ('archive.zip', 'r') zout = zipfile.ZipFile ('archve_new.zip', 'w') for item in zin.infolist():     buffer = zin.read(item.filename)     if (item.filename[-4:] != '.exe'):         zout.writestr(item, buffer) zout.close() zin.close() 

If you read everything into memory, you can eliminate the need for a second file. However, this snippet recompresses everything.

After closer inspection the ZipInfo.header_offset is the offset from the file start. The name is misleading, but the main Zip header is actually stored at the end of the file. My hex editor confirms this.

So the problem you'll run into is the following: You need to delete the directory entry in the main header as well or it will point to a file that doesn't exist anymore. Leaving the main header intact might work if you keep the local header of the file you're deleting as well, but I'm not sure about that. How did you do it with the old module?

Without modifying the main header I get an error "missing X bytes in zipfile" when I open it. This might help you to find out how to modify the main header.

like image 120
mdm Avatar answered Oct 23 '22 13:10

mdm