如何使用非Unicode非拉丁文件名提取ZIP文件? [英] How to Extract Zip Files with non-Unicode non-Latin Filenames?

查看:197
本文介绍了如何使用非Unicode非拉丁文件名提取ZIP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时您来到带文件或目录的Zip文件,并在名称中包含一些无法识别的字符,因此无法解压缩。你用什么工具来提取他们在正确转换文件名?

Sometimes you come by Zip files with file or directory with some unrecognized characters in the names so you can't extract them. What tools do you use to extract them in properly converted filenames?

推荐答案

我在网上搜索,但未能找到一个好剧本。所以出于必要,我自己写了一个Python脚本。希望它对某人来说很方便。

I searched the web but failed to find a good script. So out of necessity I wrote a Python script myself. Hope it comes handy for someone.

欢迎任何建议,特别是如果你知道这在某些特殊情况下不起作用。请不要挑上凌乱的编码风格。

Any suggestions are welcome, especially if you know this will not work in some special cases. Please don't pick on the untidy coding style.

import os,sys,zipfile

x, fn = sys.argv
enc = 'utf-8'

zipf = zipfile.ZipFile(fn)
for x in zipf.infolist():
    fn = x.filename
    fne = fn.encode(enc)
    if os.sep in fne:
        path = fne[:fne.rindex(os.sep)]
        if not os.path.exists(path): os.makedirs(path)
    if fne.endswith(os.sep): continue
    f = open(fne,'wb')
    f.write(zipf.open(fn).read())
    f.close()

这篇关于如何使用非Unicode非拉丁文件名提取ZIP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆