title: Handling corrupted archives in python
body: <p>I have a file compression/decompression system where I first check if the file is compressed or not, if not then I compress this file into a tar archive (tar.gz). Then the compressed archive is uploaded to a server. After uploading, the server decompresses the file and some processing is done on this file.</p>
  
  <p>The problem arises if the compression service shuts down during compressing of a file. As a result the tar archive gets corrupted. </p>
  
  <pre><code>....
  tar = tarfile.open(compress_file_name, "w:gz")
  tar.add(file_path, arcname=file_name)
  tar.close()
  ....
  </code></pre>
  
  <p>And to verify integrity of the file I try</p>
  
  <pre><code>try:
      f = tarfile.open(file_path) # This should throw CRC error but its not
  except Exception, e:
      raise e
  </code></pre>
  
  <p>But In-fact python seems to open the file just fine. To verify whether the file is really ok or not, I tried to open the file <code>tar -xvzf &lt;corrupted_archive&gt;</code> and I got </p>
  
  <pre><code>[~/]$ tar -xvzf &lt;corrupted_archive&gt;
  
  gzip: stdin: not in gzip format
  tar: Child returned status 1
  tar: Error is not recoverable: exiting now
  </code></pre>
  
  <p>Now I can use this same process in python using <code>subprocess.call(['tar','-xvzf',archive_name])</code> but I actually what a pythonic method to verify the integrity of the file. </p>
  
  <p>Any help would be highly appreciated.</p>