17

I have this zip file structure.

zipfile name = filename.zip

filename>    images>
             style.css
             default.js
             index.html

I want to update just index.html. I tried to update index.html, but then it contains only index.html file in 1.zip file and other files are removed.

This is the code which I tried:

import zipfile

msg = 'This data did not exist in a file before being added to the ZIP file'
zf = zipfile.ZipFile('1.zip', 
                     mode='w',
                     )
try:
    zf.writestr('index.html', msg)
finally:
    zf.close()
    
print zf.read('index.html')

So how can I update only index.html file using Python?

martineau
  • 119,623
  • 25
  • 170
  • 301
user3932031
  • 411
  • 2
  • 5
  • 13
  • What have you tried so far? Show us your code and describe what does and what doesn't work. –  Sep 09 '14 at 07:05

2 Answers2

36

Updating a file in a ZIP is not supported. You need to rebuild a new archive without the file, then add the updated version.

import os
import zipfile
import tempfile

def updateZip(zipname, filename, data):
    # generate a temp file
    tmpfd, tmpname = tempfile.mkstemp(dir=os.path.dirname(zipname))
    os.close(tmpfd)

    # create a temp copy of the archive without filename            
    with zipfile.ZipFile(zipname, 'r') as zin:
        with zipfile.ZipFile(tmpname, 'w') as zout:
            zout.comment = zin.comment # preserve the comment
            for item in zin.infolist():
                if item.filename != filename:
                    zout.writestr(item, zin.read(item.filename))

    # replace with the temp archive
    os.remove(zipname)
    os.rename(tmpname, zipname)

    # now add filename with its new data
    with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
        zf.writestr(filename, data)

msg = 'This data did not exist in a file before being added to the ZIP file'
updateZip('1.zip', 'index.html', msg)

Note that you need contextlib with Python 2.6 and earlier, since ZipFile is also a context manager only since 2.7.

You might want to check if your file actually exists in the archive to avoid an useless archive rebuild.

sebdelsol
  • 1,045
  • 8
  • 15
  • 3
    Just wanted to say thank you, this script helped me a lot! – Babken Vardanyan Jan 27 '18 at 13:21
  • 1
    I do the management of temporary files a little bit differently: first, `from tempfile import NamedTemporaryFile` then to use the temp file I simply put the rest of the function in `with NamedTemporaryFile() as tmp_file:` and get the name: `tmpname = os.path.basename(tmp_file.name)`. In the end, instead of `os.rename(...)` I do `shutil.copyfile(tmpname, filename)`. The temp file is handled and closed in the end by the context manager. – zezollo Jul 15 '20 at 14:58
5

It is not possible to update an existing file. You will need to read the file you want to edit and create a new archive including the file that you edited and the other files that were originally present in the original archive.

Below are some of the questions that might help.

Delete file from zipfile with the ZipFile Module

overwriting file in ziparchive

How do I delete or replace a file in a zip archive

Community
  • 1
  • 1
g4ur4v
  • 3,210
  • 5
  • 32
  • 57