def unzip_files(zip_file, output_path):
try:
zip_dir = os.path.splitext(os.path.basename(zip_file))[0]
zip = zipfile.ZipFile(zip_file, 'r')
if not os.path.isdir(output_path):
os.makedirs(output_path)
for each in zip.namelist():
if not each.endswith('/'):
root, name = os.path.split(each)
try:
root = root.encode('cp437').decode('gbk')
except:
root = root.encode('cp437').decode('utf-8')
try:
name = name.encode('cp437').decode('gbk')
except:
name = name.encode('cp437').decode('utf-8')
directory = os.path.normpath(os.path.join(output_path, zip_dir, root))
if not os.path.isdir(directory):
os.makedirs(directory)
file = open(os.path.join(directory, name), 'wb')
file.write(zip.read(each))
file.close()
zip.close()
except Exception as e:
print(str(e))
output_path = r"C:\Users\59887\Desktop\快来解压我这里" # Set the output path to the desired directory
# Call the unzip_files function to extract the files from the zip
unzip_files(zip_file, output_path)
zipfile解压乱码问题
最新推荐文章于 2024-06-21 14:15:56 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)