Browse Source

add python wrapper for unzipping non-standard character sets

Casey DeLorme 2 years ago
parent
commit
c222699feb
1 changed files with 32 additions and 0 deletions
  1. 32 0
      install/usr/local/bin/uz

+ 32 - 0
install/usr/local/bin/uz

@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+import sys
+import zipfile
+import argparse
+
+parser = argparse.ArgumentParser(description="accept to and from encodings")
+parser.add_argument('-O', metavar='decode', default="shift-jis")
+parser.add_argument('-I', metavar='encode', default="cp437")
+args, files = parser.parse_known_args();
+
+def unzip(filename, encode, decode):
+    code = 0
+    with zipfile.ZipFile(filename) as myzip:
+        for info in myzip.infolist():
+            try:
+                info.filename = info.filename.encode(encode, 'strict').decode(decode, 'strict')
+                myzip.extract(info)
+            except Exception as e:
+                code = 1
+                print("failed to extract {0}: {1}".format(info.filename, e))
+    return code
+
+def main(files, encode, decode):
+    code = 0
+    for file in files:
+        if unzip(file, encode, decode) == 1:
+            code = 1
+    return code
+
+if __name__ == '__main__':
+    sys.exit(main(files, args.I, args.O))