用Python解决解压zip文件中文乱码问题

ldhs5953 8年前
   <p>在Linux下,解压电脑上的.zip文件时,有时候由于编码问题,中文文件名或者文件夹名会出现乱码……</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/b1b01ed5d4f54a699c9450ec2188d8fc.png"></p>    <p>为了方便,于是自己写了一个python的解压.zip文件的脚本,亲测可用。</p>    <p>脚本myunzip.py:</p>    <pre>  <code class="language-python">#!usr/bin/env python2  # -*- coding utf-8    import os  import sys  import zipfile    print "processing File " + sys.argv[1]    file = zipfile.ZipFile(sys.argv[1], "r");  for name in file.namelist():      utf8name=name.decode('gbk')      print "Extracting " + utf8name      pathname = os.path.dirname(utf8name)      if not os.path.exists(pathname) and pathname != "":          os.makedirs(pathname)      data = file.read(name);      if not os.path.exists(utf8name):          fo = open(utf8name, "w")          fo.write(data)          fo.close  file.close()</code></pre>    <p>解压文件时,直接执行:</p>    <pre>  <code class="language-python">$ python myunzip.py xxx.zip</code></pre>    <p>或者,先增加可执行权限,然后执行:</p>    <pre>  <code class="language-python">$ chmod +x myunzip.py  $ ./myunzip.py xxx.zip</code></pre>    <p>Example:</p>    <p><img src="https://simg.open-open.com/show/277373834679b3ce7bb8670a45f1a78d.png"></p>    <p> </p>    <p>来自:http://www.jianshu.com/p/35369813826a</p>    <p> </p>