Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda