Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overwriting file in ziparchive

I have archive.zip with two files: hello.txt and world.txt

I want to overwrite hello.txt file with new one with that code:

import zipfile  z = zipfile.ZipFile('archive.zip','a') z.write('hello.txt') z.close()   

but it won't overwrite file, somehow it creates another instance of hello.txt — take a look at winzip screenshot:

alt text

Since there is no smth like zipfile.remove(), what's the best way to handle this problem?

like image 786
nukl Avatar asked Jan 11 '11 03:01

nukl


People also ask

How do I overwrite an existing file?

Overwriting a File, Part 1 To edit the settings for a file, locate the file you wish to overwrite and hover over the file name. Click the chevron button that appears to the right of the file name and select Overwrite File from the menu.


1 Answers

There's no way to do that with python zipfile module. You have to create a new zip file and recompress everything again from the first file, plus the new modified file.

Below is some code to do just that. But note that it isn't efficient, since it decompresses and then recompresses all data.

import tempfile import zipfile import shutil import os  def remove_from_zip(zipfname, *filenames):     tempdir = tempfile.mkdtemp()     try:         tempname = os.path.join(tempdir, 'new.zip')         with zipfile.ZipFile(zipfname, 'r') as zipread:             with zipfile.ZipFile(tempname, 'w') as zipwrite:                 for item in zipread.infolist():                     if item.filename not in filenames:                         data = zipread.read(item.filename)                         zipwrite.writestr(item, data)         shutil.move(tempname, zipfname)     finally:         shutil.rmtree(tempdir) 

Usage:

remove_from_zip('archive.zip', 'hello.txt') with zipfile.ZipFile('archive.zip', 'a') as z:     z.write('hello.txt') 
like image 83
nosklo Avatar answered Sep 20 '22 20:09

nosklo