title: decompress gzipped data from multipart/form-data with django
body: <p>I'm having quite a lot of trouble trying to decompress gzipped data in django. I've tried a number of the solutions proposed in <a href="https://stackoverflow.com/questions/15352668/download-and-decompress-gzipped-file-in-memory">Download and decompress gzipped file in memory?</a> but i think i'm running into difficulty in how it interacts with Django</p>
  <p>I'd like to be able to upload <code>data.csv.gz</code> and then if it is a gzip, extract out the compressed data into a django <code>File</code> to continue along its routine (Saving to <code>FileField</code>)</p>
  <p>What I have so far in my serializer</p>
  <pre><code>    def create(self, validated_data):
              file: File = validated_data.get(&quot;file&quot;)
              ext = file.name.split(&quot;.&quot;)[-1].lower()
  
              if ext == &quot;gz&quot;:
                  compressedFile = io.BytesIO()
                  compressedFile.write(file.read())
  
                  decompressed_fname = file.name[:-3]
                  decompressedFile = gzip.GzipFile(fileobj=compressedFile)
                  with open(decompressed_fname, &quot;wb&quot;) as outfile:
                      outfile.write(decompressedFile.read())
  
                  with open(decompressed_fname, &quot;rb&quot;) as outfile:
                      file = File(outfile)
                      ext = decompressed_fname.split(&quot;.&quot;)[-1].lower()
  ...
  </code></pre>
  <p>When I do this, outfile is empty when I check its contents on disk, and throws an error in later routines</p>
  <pre><code>    f.seek(0)
  ValueError: seek of closed file
  </code></pre>
  <p>I get a similar error if I use shutil instead too</p>
  <pre><code>            if ext == &quot;gz&quot;:
                  compressedFile = io.BytesIO()
                  compressedFile.write(file.read())
  
                  decompressed_fname = file.name[:-3]
                  import shutil
                  shutil.copyfileobj(gzip.GzipFile(fileobj=file), open(decompressed_fname, &quot;wb&quot;))
  
                  with open(decompressed_fname, &quot;rb&quot;) as outfile:
                      file = File(outfile)
                      ext = decompressed_fname.split(&quot;.&quot;)[-1].lower()
  </code></pre>
  <p>the curl command i'm using:</p>
  <pre><code>curl http://0.0.0.0:8000/upload/ -X 'POST' -H &quot;Content-Encoding: gzip&quot; -F &quot;input_type=data&quot; -F &quot;file=@data.csv.gz&quot;
  </code></pre>