Question

DISCLAIMER: Coding novice

C:\Python27\python.exe C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py
<open file 'C:\\Users\\Evolution\\Desktop\\mods\\AT_StalkerCreepers_1.6.4.jar', mode 'r' at 0x026F6A18>
Traceback (most recent call last):
True
<zipfile.ZipFile object at 0x02736EB0>
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 80, in <module>
<open file 'C:\\Users\\Evolution\\Desktop\\mods\\BC_AdditionalPipes2.6.0-BC4.2.1.jar', mode 'r' at 0x026F6AC8>
    o = t.getdirList()
True
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 36, in getdirList
    self.getModinfo(os.path.join(p, o[n]), 'name')
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 46, in getModinfo
    y = zipfile.ZipFile(x)
  File "C:\Python27\lib\zipfile.py", line 766, in __init__
    self._RealGetContents()
  File "C:\Python27\lib\zipfile.py", line 832, in _RealGetContents
    raise BadZipfile("Truncated central directory")
zipfile.BadZipfile: Truncated central directory

Process finished with exit code 1

Code being executed:

def getdirList(self):
    p = 'C:\Users\Evolution\Desktop\mods'
    o = [f for f in listdir(p) if isfile(join(p, f))]
    for mod in range(len(o)):
        print o[mod]
        self.getModinfo(os.path.join(p, o[mod]), 'name')


def getModinfo(self,mod,type):
    """ Types:
    modid, name, description, version, mcversion, url
    updateurl, authors, credits, dependencies """
    x = file(mod)
    print x
    print zipfile.is_zipfile(x)
    y = zipfile.ZipFile(x)
    print y

Fixed up code:

def getdirList(self):
    p = r'C:\Users\Evolution\Desktop\mods'
    o = [f for f in listdir(p) if isfile(join(p, f))]
    for mod in range(len(o)):
        print o[mod]
        self.getModinfo(os.path.join(p, o[mod]), 'name')

def getModinfo(self,mod,type):
    """ Types: modid, name, description, version, mcversion, 
    url, updateurl, authors, credits, dependencies """
    try:
        x = open(mod, 'rb')
        y = zipfile.ZipFile(x)
        z = y.read(self.minfo)
        #print z
        #zz = self.parseIt(z, '%s": ' % type, ',')
        #print zz
    except KeyError:
        print "ERROR: %s has no %s file!" % (x, self.minfo)
        print "%s" % "-" * 50
    x.close()

Now I run a check as you can see that says the zip file is, infact, a zip file. This exception is throw for more than half the files. .jar and .zip

Yes I have manually opened them, they are not corrupt or locked or anything. Is there something I can do or perhaps another library I can use for manipulation of achrives in python?

Thanks.

Was it helpful?

Solution

Zip files are binary files. To handle the binary file correctly you should open the file with binary mode.

Replace following line:

x = file(mod)

with:

x = file(mod, 'rb')

BTW, escape \ in the directory path, or use raw string literal. (This is not a direct cause of the problem, but someday escape sequence will trouble you)

'C:\\Users\\Evolution\\Desktop\\mods' # escape

or

r'C:\Users\Evolution\Desktop\mods'    # raw string literal (`r` prefix)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top