简体   繁体   English

使用python zipfile将文件添加到jar中

[英]Add a file to a jar using python zipfile

I need to add a file to jar but when i run the program zipfile delete all file and add a file. 我需要向jar中添加文件,但是当我运行zipfile程序时,请删除所有文件并添加文件。 but i need the other files! 但我需要其他文件!

my code: (this is a test) 我的代码:(这是一个测试)

import zipfile

m= zipfile.ZipFile("test.jar","w")
m.write("test.jar","bgt.class")
m.close()

sorry for my poor english 对不起,我英语不好

You need to open the file in append mode, using a : 您需要打开追加模式的文件,使用a

m = zipfile.ZipFile("test.jar", "a")

You opened the file in w write mode, which clears the file before writing. 您以w写模式打开了文件,该模式会在写之前清除文件。 From the zipfile.ZipFile() documentation : zipfile.ZipFile()文档中

The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, or 'a' to append to an existing file. 模式参数应为'r'以读取现有文件,为'w'以截断并写入新文件,或为'a'以附加到现有文件。 If mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. 如果mode'a'并且文件引用现有的ZIP文件,则将其他文件添加到其中。

Bold emphasis mine. 大胆强调我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM