Pages: 1 2
As promised earlier in this installment, I have a utility which I wrote in Python to open archives that may have been malformed when they were created. This works great under nearly any environment Python exists but is targeted specifically toward those platforms where the only unzip utility available is written by Info-Zip. Due to shortcomings in Info-Zip, it is possible to encounter archives that simply don’t extract because they were created in violation of the ZIP standard; unfortunately, nearly every other ZIP-supporting archive application can handle these, so until Info-Zip is fixed (or they state simply that it’ll never be fixed), feel free to use this tool!
Source:
#!/usr/bin/env python # Quick and dirty unzip utility. May contain bugs. # zancarius -at- gmail [com] # Should return 0 on success, 1 or greater on failure. Useful for # shell scripts. # This code has been placed into the public domain as of March 20, 2009. import os, string, sys, zipfile # Main app if __name__ == '__main__': if len(sys.argv) == 1: sys.stdout.write("Usage: unzip.py file [destination directory]\n") sys.exit(0) if not os.path.exists(sys.argv[1]): sys.stderr.write("File not found: %s\n" % sys.argv[1]) sys.exit(1) z = zipfile.ZipFile(sys.argv[1], 'r') for entry in z.namelist(): filename = os.path.basename(entry) # Indicates that the stored entry is only a directory. if not filename: if sys.argv[2]: path = os.path.join(sys.argv[2], entry) if not os.path.exists(path): sys.stdout.write("Creating directory %s (stored)\n" % path) continue path = string.strip(entry[:-len(filename)]) try: path.index("\\") path = string.replace(path, "\\", "/") except ValueError: pass data = z.read(entry) if sys.argv[2]: path = os.path.join(sys.argv[2], path) if not os.path.exists(path): os.makedirs(path) sys.stdout.write("Extracting %s%s (%d bytes)\n" % (path, filename, len(data))) path = os.path.join(path, filename) open(path, 'w').write(data) sys.exit(0) |
Pages: 1 2
Leave a comment