[英]Replace/Add a class file to subfolder in jar using python zipfile command
I have a jar file and I have a path which represents a location inside Jar file. 我有一个jar文件,并且有一个路径代表了Jar文件中的位置。
Using this location I need to replace class file inside jar(Add a class file in some cases).I have class file inside another folder which is present where jar is present(This class file i have to move to Jar). 使用这个位置,我需要替换jar文件中的类文件(在某些情况下添加类文件)。我在存在jar的另一个文件夹中有类文件(我必须将该类文件移动到Jar中)。
Code which I am trying to achieve above objective : 我正在尝试实现上述目标的代码:
import zipfile
import os
zf = zipfile.ZipFile(os.path.normpath('D:\mystuff\test.jar'),mode='a')
try:
print('adding testclass.class')
zf.write(os.path.normpath('D:\mystuff\testclass.class'))
finally:
print('closing')
zf.close()
After executing above code when I saw jar below mentioned format: 执行完上面的代码后,当我看到下面提到的格式的jar时:
Jar
|----META-INF
|----com.XYZ
|----Mystuff
|--testclass.class
Actual Output I need is - 我需要的实际输出是-
Jar
|----META-INF
|----com.XYZ
|--ABC
|-testclass.class
How can achieve this using zipfile.write command or any other way in python? 如何使用zipfile.write命令或python中的任何其他方式实现此目的?
I didn't find any params in write command where i can provide destination file location inside Jar/Zip file. 我没有在write命令中找到任何可以在Jar / Zip文件中提供目标文件位置的参数。
ZipFile.write(filename, arcname=None, compress_type=None) ZipFile.write(文件名,弧名=无,compress_type =无)
Specify arcname
to change the name of the file in the archive. 指定
arcname
可以更改存档中文件的名称。
import zipfile
import os
zf = zipfile.ZipFile(os.path.normpath(r'D:\mystuff\test.jar'),mode='a')
try:
print('adding testclass.class')
zf.write(os.path.normpath(r'D:\mystuff\testclass.class'),arcname="com.XYZ/ABC/testclass.class")
finally:
print('closing')
zf.close()
Note: I doubt test.jar
is your real jar name, since you didn't protect your string against special chars and the jar file opened would have been 'D:\\mystuff\\<TAB>est.jar'
(well, it doesn't work :)) 注意:我怀疑
test.jar
是您的真实jar名称,因为您没有保护字符串不受特殊字符的影响,并且打开的jar文件将是'D:\\mystuff\\<TAB>est.jar'
(嗯,它没有不工作:))
EDIT: if you want to add the new file but remove the old one, you have to do differently: you cannot delete from a zipfile, you have to rebuild another one (inspired by Delete file from zipfile with the ZipFile Module ) 编辑:如果您想添加新文件但删除旧文件,则必须做不同的事情:您无法从zip 文件中删除 ,而必须重建另一个文件 (受ZipFile Module从zipfile中删除文件的启发)
import zipfile
import os
infile = os.path.normpath(r'D:\mystuff\test.jar')
outfile = os.path.normpath(r'D:\mystuff\test_new.jar')
zin = zipfile.ZipFile(infile,mode='r')
zout = zipfile.ZipFile(outfile,mode='w')
for item in zin.infolist():
if os.path.basename(item.filename)=="testclass.class":
pass # skip item
else:
# write the item to the new archive
buffer = zin.read(item.filename)
zout.writestr(item, buffer)
print('adding testclass.class')
zout.write(os.path.normpath(r'D:\mystuff\testclass.class'),arcname="com.XYZ/ABC/testclass.class")
zout.close()
zin.close()
os.remove(infile)
os.rename(outfile,infile)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.