繁体   English   中英

如何在python中上传文件时读取内存中zip文件的内容?

[英]How to read contents of zip file in memory on a file upload in python?

我有一个用户上传文件时收到的zip文件。 zip本质上包含一个json文件,我想读取和处理它,而不必先创建zip文件,然后解压缩它,然后读取内部文件的内容。

目前我只有更长的过程,如下所示

import json
import zipfile

@csrf_exempt
def get_zip(request):
    try:
        if request.method == "POST":
            try:
                client_file = request.FILES['file']
                file_path = "/some/path/"
                # first dump the zip file to a directory
                with open(file_path + '%s' % client_file.name, 'wb+') as dest:
                        for chunk in client_file.chunks():
                            dest.write(chunk)

                # unzip the zip file to the same directory 
                with zipfile.ZipFile(file_path + client_file.name, 'r') as zip_ref:
                    zip_ref.extractall(file_path)

                # at this point we get a json file from the zip say `test.json`
                # read the json file content
                with open(file_path + "test.json", "r") as fo:
                    json_content = json.load(fo)
                    doSomething(json_content)
                return HttpResponse(0)

            except Exception as e:
                return HttpResponse(1)

如您所见,这涉及最终将zip文件中的内容放入内存的 3 个步骤。 我想要的是获取zip文件的内容并直接加载到内存中。

我确实在堆栈溢出中发现了一些类似的问题,例如https://stackoverflow.com/a/2463819 但是我不确定在什么时候调用帖子中提到的这个操作

我怎样才能做到这一点?

注意:我在后端使用 django。 zip 中总会有一个 json 文件。

zipfile.ZipFile()的第一个参数可以是文件对象而不是路径名。 我认为 Django UploadedFile对象支持这种用法,因此您可以直接从中读取,而不必复制到文件中。

您还可以直接从 zip 存档打开文件,而不是将其解压缩到文件中。

import json
import zipfile

@csrf_exempt
def get_zip(request):
    try:
        if request.method == "POST":
            try:
                client_file = request.FILES['file']
                # unzip the zip file to the same directory 
                with zipfile.ZipFile(client_file, 'r') as zip_ref:
                    first = zip_ref.infolist()[0]
                    with zip_ref.open(first, "r") as fo:
                        json_content = json.load(fo)
                doSomething(json_content)
                return HttpResponse(0)

            except Exception as e:
                return HttpResponse(1)

据我了解,@jason在这里想说的是首先打开一个 zipFile,就像你在这里with zipfile.ZipFile(file_path + client_file.name, 'r') as zip_ref:

class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

  Open a ZIP file, where file can be either a path to a file (a string) or a file-like object.

然后使用BytesIO读取一个类文件对象的字节。 但是从上面你是在r模式而不是rb模式下阅读。 所以改成如下。

with open(filename, 'rb') as file_data:
    bytes_content = file_data.read()
    file_like_object = io.BytesIO(bytes_content)
    zipfile_ob = zipfile.ZipFile(file_like_object)

现在zipfile_ob可以从内存中访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM