如何使用 Python 解码编码的 zip 文件?

Ant*_*ony 1 zip base64 pkzip

我有一个 base64 编码的 zip 文件。我能够转换该 zip 文件,然后使用 Windows 命令行提取其内容。我一直在尝试用 Python 做同样的事情,但没有成功。请你帮助我好吗?当我运行以下代码时:

import base64
import codecs
import zlib
import io, zipfile, json, pprint

d = open("data.txt", "rb").read()
#dd = base64.decodestring(d)
#print(dd)
z = zipfile.ZipFile(io.BytesIO(d))
unpack = zlib.decompress(d)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

raise BadZipFile("文件不是 zip 文件") zipfile.BadZipFile:文件不是 zip 文件

data.txt 文件包含 base64 字符串: 在此输入图像描述

Ant*_*ony 6

我的一个朋友帮助了我。我认为在这里发布解决方案可能会帮助很多像我这样的初学者:

def convert(d,name, ex):
    with open('output_file.zip', 'wb') as result:
        result.write(base64.b64decode(d))
    zip_ref = zipfile.ZipFile("output_file.zip", 'r')
    zip_ref.extractall("extracted_file")
    zip_ref.close()
        
    for filename in os.listdir("extracted_file"):
        extracted_file = "extracted_file/"+filename
        shutil.move(extracted_file, "images/"+name+ex)
Run Code Online (Sandbox Code Playgroud)