Reputation: 5067
I'm trying to extract a compressed file from a tar archive using Python 3.6.5.
I'm trying to extract files from a tar archive that contains compressed gz files. I've followed the advice this Stackoverflow answer :
import tarfile,os
import sys
tar = tarfile.open("arXiv_src_9107_001a.tar")
n = 0
for member in tar.getmembers():
#Skip directory labeled at the top
if(n==0):
n=1
continue
f=tar.extractfile(member)
print(member)
content=f.read()
print("{} has {} newlines".format(member, content.count("\n")))
print("{} has {} spaces".format(member, content.count(" ")))
print("{} has {} characters".format(member, len(content)))
#sys.exit()
tar.close()
When I print out vars(tar)
in pdb
(Pdb) p vars(tar)
{'mode': 'r', '_mode': 'rb', '_extfileobj': False, 'name': '/Users/user/Downloads/arXiv_src_9107_001a.tar', 'fileobj': <_io.BufferedReader name='arXiv_src_9107_001a.tar'>, 'errors': 'surrogateescape', 'pax_headers': {}, 'copybufsize': None, 'closed': False, 'members': [<TarInfo '9107' at 0x11004b048>, <TarInfo '9107/hep-lat9107001.gz' at 0x11004b110>, <TarInfo '9107/hep-lat9107002.gz' at 0x11004b1d8>, <TarInfo '9107/qc_01.gz' at 0x11004b2a0>, <TarInfo '9107/qc_02.gz' at 0x11004b368>, <TarInfo '9107/qi_01.gz' at 0x11004b430>, <TarInfo '9107/qs_01.gz' at 0x11004b4f8>, <TarInfo '9107/quant_only_01.gz' at 0x11004b5c0>], '_loaded': True, 'offset': 69120, 'inodes': {}, 'firstmember': None}
If I print out the content
variable, I get a bytes object. E.g. :
b'\x1f\x8b\x08\x08\xe5C\x12M\x00\x03hep-lat9107001\x00\xed}{w\xdbF\x92\xef\xfc\x1b|\x8a\xbe\xf72\x13i#R\x00\x08\xf0\x91\x8c\xf7\x1c?c\xcf\xc6\x8f\xb5\x9d\xc9\xeeZN\x06"!\tc\x92\xe0\x10\xa0d\x85W\xf9\xec\xf...
Question
In the case where a tar archive is composed of individually compressed files, how do I read/decompress those gz files into usable human language strings?
Upvotes: 0
Views: 1512
Reputation:
You can use gzip.decompress:
import tarfile, os, gzip
import sys
tar = tarfile.open("arXiv_src_9107_001a.tar")
n = 0
for member in tar.getmembers():
#Skip directory labeled at the top
if(n==0):
n=1
continue
f=tar.extractfile(member)
print(member)
content=f.read()
expanded = gzip.decompress(content)
# do whatever with expanded here
tar.close()
Upvotes: 1