Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda