Python solves Chinese garbled characters when decompressing the zip file of Fedora,
Preface
It is common to compress files in windows, but garbled characters appear in Linux. Previously, in Ubuntu`unzip -O GBK filename.zip`
You can do this. After changing Fedora, no garbled compressed files are found. In the evening, I downloaded a book's CD and encountered garbled characters. The previous method failed. After reading the help of unzip, I didn't have the-O parameter =. I just found a solution using python and shared it.
Create a new file with the '. py' suffix and copy and paste the Code directly:
#!/usr/bin/env python# -*- coding: utf-8 -*- import osimport sysimport 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.closefile.close()
Decompress the zip file, and the cute Chinese is displayed.
Python file name. py unzip decompressed file name. Zip
Summary
Well, this is a simple solution. Have you learned it? I hope this article will help you in your study or work. If you have any questions, you can leave a message.