通过Django下载Zip文件,文件解压缩为cpzg

时间:2021-09-10 09:56:19

My goal is to parse a series of strings into a series of text files that are compressed as a Zip file and downloaded by a web app using Django's HTTP Response.

我的目标是将一系列字符串解析为一系列文本文件,这些文件被压缩为Zip文件,并使用Django的HTTP响应由Web应用程序下载。

Developing locally in PyCharm, my method outputs a Zip file called "123.zip" which contains 6 individual files named "123_1", "123_2 etc". containing the letters from my phrase with no problem.

在PyCharm本地开发,我的方法输出一个名为“123.zip”的Zip文件,其中包含6个名为“123_1”,“123_2等”的单个文件。包含我的短语中的字母没有问题。

The issue is when I push the code to my web app and include the Django HTTP Response the file will download but when I go to extract it it produces "123.zip.cpzg". Extracting that in turn gives me 123.zip(1) in a frustrating infinite loop. Any suggestions where I'm going wrong?

问题是当我将代码推送到我的Web应用程序并包含Django HTTP Response文件将下载但是当我去提取它时它产生“123.zip.cpzg”。提取它反过来给了我一个令人沮丧的无限循环中的123.zip(1)。我出错的任何建议?

Code that works locally to produce "123.zip":

在本地工作以生成“123.zip”的代码:

def create_text_files1():
    JobNumber = "123"
    z = zipfile.ZipFile(JobNumber +".zip", mode ="w")
    phrase = "A, B, C, D, EF, G"
    words = phrase.split(",")
    x =0

    for word in words:
        word.encode(encoding="UTF-8")
        x = x + 1
        z.writestr(JobNumber +"_" + str(x) + ".txt", word)
    z.close()

Additional part of the method in my web app:

我的网络应用程序中方法的其他部分:

    response = HTTPResponse(z, content_type ='application/zip')
    response['Content-Disposition'] = "attachment; filename='" + str(jobNumber) + "_AHTextFiles.zip'"

1 个解决方案

#1


0  

Take a closer look at the example provided in this answer.

仔细看看这个答案中提供的示例。

Notice a StringIO is opened, the zipFile is called with the StringIO as a "File-Like Object", and then, crucially, after the zipFile is closed, the StringIO is returned in the HTTPResponse.

注意,打开了一个StringIO,使用StringIO作为“类文件对象”调用zipFile,然后,关键的是,在zipFile关闭后,在HTTPResponse中返回StringIO。

# Open StringIO to grab in-memory ZIP contents
s = StringIO.StringIO()

# The zip compressor
zf = zipfile.ZipFile(s, "w")

# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-co mpressed")

I would recommend a few things in your case.

在你的情况下我会推荐一些东西。

  1. Use BytesIO for forward compatibility
  2. 使用BytesIO实现向前兼容性

  3. Take advantage of ZipFile's built in context manager
  4. 利用ZipFile内置的上下文管理器

  5. In your Content-Disposition, be careful of "jobNumber" vs "JobNumber"
  6. 在您的内容处置中,请注意“jobNumber”与“JobNumber”

Try something like this:

尝试这样的事情:

def print_nozzle_txt(request):
    JobNumber = "123"
    phrase = "A, B, C, D, EF, G"
    words = phrase.split(",")
    x =0

    byteStream = io.BytesIO()

    with zipfile.ZipFile(byteStream, mode='w', compression=zipfile.ZIP_DEFLATED,) as zf:
        for word in words:
            word.encode(encoding="UTF-8")
            x = x + 1
            zf.writestr(JobNumber + "_" + str(x) + ".txt", word)

    response = HttpResponse(byteStream.getvalue(), content_type='application/x-zip-compressed')
    response['Content-Disposition'] = "attachment; filename='" + str(JobNumber) + "_AHTextFiles.zip'"
    return response

#1


0  

Take a closer look at the example provided in this answer.

仔细看看这个答案中提供的示例。

Notice a StringIO is opened, the zipFile is called with the StringIO as a "File-Like Object", and then, crucially, after the zipFile is closed, the StringIO is returned in the HTTPResponse.

注意,打开了一个StringIO,使用StringIO作为“类文件对象”调用zipFile,然后,关键的是,在zipFile关闭后,在HTTPResponse中返回StringIO。

# Open StringIO to grab in-memory ZIP contents
s = StringIO.StringIO()

# The zip compressor
zf = zipfile.ZipFile(s, "w")

# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-co mpressed")

I would recommend a few things in your case.

在你的情况下我会推荐一些东西。

  1. Use BytesIO for forward compatibility
  2. 使用BytesIO实现向前兼容性

  3. Take advantage of ZipFile's built in context manager
  4. 利用ZipFile内置的上下文管理器

  5. In your Content-Disposition, be careful of "jobNumber" vs "JobNumber"
  6. 在您的内容处置中,请注意“jobNumber”与“JobNumber”

Try something like this:

尝试这样的事情:

def print_nozzle_txt(request):
    JobNumber = "123"
    phrase = "A, B, C, D, EF, G"
    words = phrase.split(",")
    x =0

    byteStream = io.BytesIO()

    with zipfile.ZipFile(byteStream, mode='w', compression=zipfile.ZIP_DEFLATED,) as zf:
        for word in words:
            word.encode(encoding="UTF-8")
            x = x + 1
            zf.writestr(JobNumber + "_" + str(x) + ".txt", word)

    response = HttpResponse(byteStream.getvalue(), content_type='application/x-zip-compressed')
    response['Content-Disposition'] = "attachment; filename='" + str(JobNumber) + "_AHTextFiles.zip'"
    return response