从字节创建 zip 文件

Tah*_*bal 2 python zipfile python-3.x

我正在使用客户端发送 zip 文件的字节字符串,JSZip需要在服务器端将其转换回 zip。我试过的代码不起作用。

b = bytearray()
b.extend(map(ord, request.POST.get("zipFile")))

zipPath = 'uploadFile' + str(uuid.uuid4()) + '.zip'
myzip = zipfile.ZipFile(zipPath, 'w') 
with  myzip:
    myzip.write(b)
Run Code Online (Sandbox Code Playgroud)

它给出了错误:

stat: path too long for Windows 
Run Code Online (Sandbox Code Playgroud)

如何将我的字节字符串保存为 zip 文件?

mat*_*ata 7

ZipFile.write(filename, [arcname[, compress_type]])获取要添加到 zip 文件的本地文件的名称。要从 abytearraybytes对象写入数据,您需要改用该ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])方法:

with zipfile.ZipFile(zipPath, 'w'):
    zipFile.write('name_of_file_in_archive', zipContents)
Run Code Online (Sandbox Code Playgroud)

注意:如果request.POST.get("zipFile")已经是bytes(或str在 python2 中),您不需要在将其bytearray写入存档之前将其转换为 a 。