English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Solution to Chinese Character Garbled when Unzipping Fedora

Foreword

Many times, compressing files in Windows works fine, but it is common to encounter garbled characters in Linux. In the past, under Ubuntu, using `unzip` -O GBK filename.zip` can handle it. After switching to Fedora, I haven't found any乱码 compressed files for the time being. Late at night, I downloaded a CD of a book and encountered乱码 again. The method I tried before didn't work. Looked at the help of unzip, no-O that parameter == Just found a way to solve it with python, let's share it.

Create a file with a `.py` suffix, and directly copy and paste the code:

#!/usr/bin/env python
# -*- 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()

Execute the unpacking of the zip file, and the lovely Chinese characters come out.

python filename.py needs to be unpacked filename.zip

Summary

Alright, that's how this problem was solved so simply. Did everyone learn? I hope this article can bring a certain amount of help to everyone's learning or work. If you have any questions, you can leave a message for communication.

You May Also Like