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

python - Lösung für das Problem der Zerrüttung chinesischer Zeichen beim Entpacken von zip unter Fedora

Einleitung

Oftmals funktioniert die Komprimierung von Dateien unter Windows einwandfrei, aber unter Linux tritt häufig ein Chaos von Zeichensätzen auf. -O GBK filename.zip` can solve it. After switching to Fedora, I haven't found any garbled compressed files for the time being. In the evening, I downloaded a CD of a book and encountered garbled characters again. The method I tried before didn't work. Looked at the help of unzip, didn't}}-O that parameter == Just found a way to solve it with python, let's share it.

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

#!/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 unzip file, and the lovely Chinese characters appear.

python filename.py needs to be extracted filename.zip

Summary

Alright, this problem was solved so simply, did everyone learn it? I hope this article can bring some help to everyone's learning or work. If you have any questions, you can leave comments for communication.

Möchten Sie auch