python to solve Fedora decompression of zip Chinese messy code method

  • 2020-05-10 18:25:20
  • OfStack

preface

A lot of times it's ok to compress a file under windows, but under Linux, it's common to see scrambled code. It used to be under Ubuntu `unzip -O GBK filename.zip` You can do it. After changing Fedora, no scrambled compressed file was found. Download the CD of 1 book in the evening, encountered again messy code. The previous method was tried without success. Check help of unzip, without the parameter of -O ==, I just found a solution using python, please share.

Create a new file with the suffix '.py ', 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 zip file and the lovely Chinese will come out.


python  The file name .py  File name to unzip .zip

conclusion

Ok, so that's the easy way out of this problem, everyone? I hope this article can help you in your study or work. If you have any questions, please leave a message.


Related articles: