Python,解压缩.jar文件,不起作用

时间:2012-08-07 16:51:21

标签: python jar unzip

所以,我正在尝试使用以下代码解压缩.jar文件: 它不会解压缩,只有20/500文件,没有文件夹/图片 当我在filename中输入.zip文件时,会发生同样的事情。 任何一个建议吗?

import zipfile
zfilename = "PhotoVieuwer.jar"
if zipfile.is_zipfile(zfilename):
    print "%s is a valid zip file" % zfilename
else:
    print "%s is not a valid zip file" % zfilename
print '-'*40

zfile = zipfile.ZipFile( zfilename, "r" )

zfile.printdir()
print '-'*40

for info in zfile.infolist():
    fname = info.filename

    data = zfile.read(fname)


    if fname.endswith(".txt"):
        print "These are the contents of %s:" % fname
        print data


    filename = fname
    fout = open(filename, "w")
    fout.write(data)
    fout.close()
    print "New file created --> %s" % filename
    print '-'*40

但是,它不起作用,它可以解压缩500个文件中的10个 任何人都可以帮我解决这个问题吗?

已经感谢了!

我尝试添加,Python告诉我,我得到了这个: 哎呀!无法提交您的修改,因为:

正文仅限于 30000 个字符;您输入了 153562 只有错误是:

Traceback (most recent call last):
  File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
    fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'

解压缩的文件:

amw.Class
amx.Class
amz.Class
ana.Class
ane.Class
anf.Class
ang.Class
ank.Class
anm.Class
ann.Class
ano.Class
anq.Class
anr.Class
anx.Class
any.Class
anz.Class
aob.Class
aoc.Class
aod.Class
aoe.Class

2 个答案:

答案 0 :(得分:3)

此追溯会告诉您需要了解的内容:

Traceback (most recent call last):
  File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
    fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'

错误消息表明文件ClientBrandRetriever.class不存在或目录net/minecraft/client不存在。当打开文件进行编写时,Python会创建它,因此不存在文件不存在的问题。必须是目录不存在的情况。

考虑一下这是有效的

>>> open('temp.txt', 'w') 
<open file 'temp.txt', mode 'w' at 0x015FF0D0>

但这不是,给你几乎相同的回溯:

>>> open('bogus/temp.txt', 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'bogus/temp.txt'

创建目录修复它:

>>> os.makedirs('bogus')
>>> open('bogus/temp.txt', 'w')
<open file 'bogus/temp.txt', mode 'w' at 0x01625D30>

在打开文件之前,您应该检查目录是否存在,并在必要时创建它。

所以要解决您的问题,请替换此

fout = open(filename, 'w')

用这个

head, tail = os.path.split(filename) # isolate directory name
if not os.path.exists(head):         # see if it exists
    os.makedirs(head)                # if not, create it
fout = open(filename, 'w')

答案 1 :(得分:0)

如果python -mzipfile -e PhotoVieuwer.jar dest有效,那么您可以:

import zipfile

with zipfile.ZipFile("PhotoVieuwer.jar") as z:
    z.extractall()