Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Getting information from SpooledTemporaryFile that is a zip

I am uploading a zip file with Fast API and it takes in the files as a SpooledTemporaryFile. I have been trying to copy that file onto disk. I have attempted various things; the two I feel I have made progress in are down below.

  1. I have attempted to use zipfile.Zipfile, when I turn it into one I am unable to unzip it because it says it is not a zip.

  2. I also uploaded a single file that is not zipped and turned the SpooledTemporaryFile into _io.BytesIO, however, when I try to read the content of a single file (not zipped) it returned empty byte (b'').

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'm actually working on something similar and ran into the same issues. The solution I came up with was to use the File option (not the UploadFile one), write the input to another file, then perform the unzip.

Here's my implementation using a TemporaryDirectory and separate directories for input and unzipped files:

@router.post("/test_file/unzip")
def unzip_upload(file: bytes = File(...)):

    with tempfile.TemporaryDirectory() as temp_dir:
        os.chdir(temp_dir)
        os.mkdir('input')
        os.mkdir('unzipped')

        with open("input/zip_file.zip", 'wb') as new_file:
            new_file.write(file)
            # print(f"output of listdir for /input {os.listdir(temp_dir + '/input')}")

            with zipfile.ZipFile("input/zip_file.zip") as zip_file:
                print(f"files in zip: {zip_file.namelist()}")
                zip_file.extractall('unzipped')

        unzipped_files = os.listdir('unzipped')

    return {"unzipped files": unzipped_files}

If you need to use the UploadFile option, I got this to work:

@router.post("/test_uploadfile/unzip")
def unzip_upload(file: UploadFile = File(...)):

    with tempfile.TemporaryDirectory() as temp_dir:
        os.chdir(temp_dir)
        os.mkdir('input')
        os.mkdir('unzipped')

        with open("input/zip_file.zip", 'wb') as new_file:
            new_file.write(file.file._file.getvalue())
            # print(f"output of listdir for /input {os.listdir(temp_dir + '/input')}")

            with zipfile.ZipFile("input/zip_file.zip") as zip_file:
                print(f"files in zip: {zip_file.namelist()}")
                zip_file.extractall('unzipped')

        unzipped_files = os.listdir('unzipped')

    return {"unzipped files": unzipped_files}
over 4 years ago · Santiago Trujillo Report

0

@app.post("/", status_code=200)
async def upload(file: bytes = File(...)):
 zip_file_name = "file.zip"
    
 with open(zip_file_name, 'wb') as zip_file:
  zip_file.write(file)

I switched from accepting a tempfile.TemporaryDirectory() to a bytes as suggested by C H here.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!