由于 Windows 下的文件名为 GBK 编码,而 linux 一般为 UTF-8,因此当解压在 Windows 上生成的 zip 文件后,会发现解压出来的文件都是乱码的。网上有个解决方法是使用 unzip 的 -O 选项来指定编码格式,然而不知道为何,我在 archlinux 下的 unzip 并没有这个选项。
好在找到了一个 gbkunzip 脚本,可以解决这个问题。在 archlinux 上,可以通过 yaourt 来安装 gbkunzip
yaourt -S gbkunzip
安装后,直接执行 gbkunzip zip文件 就行了,gbkunzip 实际上就是一段 python 代码,它其实就是对 gbzip module 中 ZipFile 类的一个封装。
cat $(whereis gbkunzip |awk '{print $2}')
#!/usr/bin/env python3
# fileencoding=utf-8
'''
解压 zip 文件,其中的文件名是 GB18030 编码,但系统是 Unicode 编码
'''
import sys
import os
from gbzip import ZipFile
from getpass import getpass
def main():
try:
z = ZipFile(sys.argv[1])
while True:
try:
z.extractall()
except RuntimeError: # encrypted zipfile
passwd = getpass('Enter correct password: ').encode()
z.setpassword(passwd)
else:
break
print('Everything is ok.')
except IndexError:
sys.exit('give me exactly one zipfile to extract.')
if __name__ == '__main__':
main()
