如何使用python [duplicate]更新zip文件中的一个文件

时间:2021-12-07 09:45:47

This question already has an answer here:

这个问题在这里已有答案:

I have this zip file structure.

我有这个zip文件结构。

zipfile name = filename.zip

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 remobved.

我想只更新index.html。我试图更新index.html,但它只包含1.zip文件中的index.html文件,其他文件都被重新驱动。

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?

那么我怎样才能使用Python更新index.html文件?

2 个解决方案

#1


16  

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

不支持更新ZIP中的文件。您需要在没有该文件的情况下重建新存档,然后添加更新的版本。

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.

请注意,您需要使用Python 2.6及更早版本的contextlib,因为ZipFile自2.7以来也只是一个上下文管理器。

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

您可能希望检查文件中是否存在实际存在的文件,以避免无用的存档重建。

#2


1  

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

使用ZipFile模块从zipfile中删除文件

overwriting file in ziparchive

用ziparchive覆盖文件

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

如何删除或替换zip存档中的文件

#1


16  

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

不支持更新ZIP中的文件。您需要在没有该文件的情况下重建新存档,然后添加更新的版本。

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.

请注意,您需要使用Python 2.6及更早版本的contextlib,因为ZipFile自2.7以来也只是一个上下文管理器。

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

您可能希望检查文件中是否存在实际存在的文件,以避免无用的存档重建。

#2


1  

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

使用ZipFile模块从zipfile中删除文件

overwriting file in ziparchive

用ziparchive覆盖文件

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

如何删除或替换zip存档中的文件