This article introduces how to use python to solve Chinese garbled characters when decompressing the zip file of Fedora. It provides some reference value for you. if you need it, you can refer to it. let's take a look at it.
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.
For more articles about how python solves Chinese garbled characters when decompressing the zip file of Fedora, refer to the PHP Chinese website!