web-dev-qa-db-ja.com

ziparchive内のファイルを上書きする

_archive.Zip_と2つのファイルがあります:_hello.txt_と_world.txt_

_hello.txt_ファイルをそのコードで新しいファイルで上書きしたい:

_import zipfile

z = zipfile.ZipFile('archive.Zip','a')
z.write('hello.txt')
z.close()  
_

ただし、ファイルは上書きされません。どういうわけか、_hello.txt_の別のインスタンスが作成されます—winzipのスクリーンショットを見てください。

alt text

zipfile.remove()のようなsmthはないので、この問題を処理する最良の方法は何ですか?

28
nukl

python zipfileモジュールでこれを行う方法はありません。新しいZipファイルを作成し、最初のファイルと新しい変更されたファイルからすべてを再圧縮する必要があります。

以下はまさにそれを行うためのいくつかのコードです。ただし、すべてのデータを解凍してから再圧縮するため、効率的ではないことに注意してください。

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)

使用法:

remove_from_Zip('archive.Zip', 'hello.txt')
with zipfile.ZipFile('archive.Zip', 'a') as z:
    z.write('hello.txt')
33
nosklo

Noskloの答えに基づいて構築します。 UpdateableZipFile ZipFileから継承するクラスで、同じインターフェイスを維持しますが、ファイルを上書き(writestrまたはwriteを介して)およびファイルを削除する機能を追加します。

import os
import shutil
import tempfile
from zipfile import ZipFile, Zip_STORED, ZipInfo


class UpdateableZipFile(ZipFile):
    """
    Add delete (via remove_file) and update (via writestr and write methods)
    To enable update features use UpdateableZipFile with the 'with statement',
    Upon  __exit__ (if updates were applied) a new Zip file will override the exiting one with the updates
    """

    class DeleteMarker(object):
        pass

    def __init__(self, file, mode="r", compression=Zip_STORED, allowZip64=False):
        # Init base
        super(UpdateableZipFile, self).__init__(file, mode=mode,
                                                compression=compression,
                                                allowZip64=allowZip64)
        # track file to override in Zip
        self._replace = {}
        # Whether the with statement was called
        self._allow_updates = False

    def writestr(self, zinfo_or_arcname, bytes, compress_type=None):
        if isinstance(zinfo_or_arcname, ZipInfo):
            name = zinfo_or_arcname.filename
        else:
            name = zinfo_or_arcname
        # If the file exits, and needs to be overridden,
        # mark the entry, and create a temp-file for it
        # we allow this only if the with statement is used
        if self._allow_updates and name in self.namelist():
            temp_file = self._replace[name] = self._replace.get(name,
                                                                tempfile.TemporaryFile())
            temp_file.write(bytes)
        # Otherwise just act normally
        else:
            super(UpdateableZipFile, self).writestr(zinfo_or_arcname,
                                                    bytes, compress_type=compress_type)

    def write(self, filename, arcname=None, compress_type=None):
        arcname = arcname or filename
        # If the file exits, and needs to be overridden,
        # mark the entry, and create a temp-file for it
        # we allow this only if the with statement is used
        if self._allow_updates and arcname in self.namelist():
            temp_file = self._replace[arcname] = self._replace.get(arcname,
                                                                   tempfile.TemporaryFile())
            with open(filename, "rb") as source:
                shutil.copyfileobj(source, temp_file)
        # Otherwise just act normally
        else:
            super(UpdateableZipFile, self).write(filename, 
                                                 arcname=arcname, compress_type=compress_type)

    def __enter__(self):
        # Allow updates
        self._allow_updates = True
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # call base to close Zip file, organically
        try:
            super(UpdateableZipFile, self).__exit__(exc_type, exc_val, exc_tb)
            if len(self._replace) > 0:
                self._rebuild_Zip()
        finally:
            # In case rebuild Zip failed,
            # be sure to still release all the temp files
            self._close_all_temp_files()
            self._allow_updates = False

    def _close_all_temp_files(self):
        for temp_file in self._replace.itervalues():
            if hasattr(temp_file, 'close'):
                temp_file.close()

    def remove_file(self, path):
        self._replace[path] = self.DeleteMarker()

    def _rebuild_Zip(self):
        tempdir = tempfile.mkdtemp()
        try:
            temp_Zip_path = os.path.join(tempdir, 'new.Zip')
            with ZipFile(self.filename, 'r') as Zip_read:
                # Create new Zip with assigned properties
                with ZipFile(temp_Zip_path, 'w', compression=self.compression,
                             allowZip64=self._allowZip64) as Zip_write:
                    for item in Zip_read.infolist():
                        # Check if the file should be replaced / or deleted
                        replacement = self._replace.get(item.filename, None)
                        # If marked for deletion, do not copy file to new zipfile
                        if isinstance(replacement, self.DeleteMarker):
                            del self._replace[item.filename]
                            continue
                        # If marked for replacement, copy temp_file, instead of old file
                        Elif replacement is not None:
                            del self._replace[item.filename]
                            # Write replacement to archive,
                            # and then close it (deleting the temp file)
                            replacement.seek(0)
                            data = replacement.read()
                            replacement.close()
                        else:
                            data = Zip_read.read(item.filename)
                        Zip_write.writestr(item, data)
            # Override the archive with the updated one
            shutil.move(temp_Zip_path, self.filename)
        finally:
            shutil.rmtree(tempdir)

使用例:

with UpdateableZipFile("C:\Temp\Test2.docx", "a") as o:
    # Overwrite a file with a string
    o.writestr("Word/document.xml", "Some data")
    # exclude an exiting file from the Zip
    o.remove_file("Word/fontTable.xml")
    # Write a new file (with no conflict) to the zp
    o.writestr("new_file", "more data")
    # Overwrite a file with a file
    o.write(r"C:\Temp\example.png", "Word/settings.xml")
19
Or Weis