15813

Python issue with Zipfiles

Question:

<strong>DISCLAIMER</strong>: 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.

Answer1:

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') <hr />

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)

Recommend

  • how to make my first Ruby effort more idiomatic
  • LyX — Title, Abstract, Keywords Missing When Using Elsevier Class in View
  • Couchbase: Connection refused
  • Suqueries in select clause with JPA
  • xcode don't localize specific strings
  • How do I signal completion of my dataflow?
  • c# open webrowser in many tab
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • revitapi ironpython ToRoom returns “indexer # object”
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Connect .sks to skscene.h
  • Debug.DrawLine not showing in the GameView
  • GridView breaks while scrolling
  • Jackson Parser: ignore deserializing for type mismatch
  • How to Cache Real-time Data?
  • How to use RequestBodyAdvice
  • Q promise. Difference between .when and .then
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Incrementing object id automatically JS constructor (static method and variable)
  • script to move all files from one location to another location
  • Does CUDA 5 support STL or THRUST inside the device code?
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?