I have several tar archives that I need to extract/read in memory. The problem is each tar contains many ZIP archives and each contain unique XML documents.
So the structure of each tar is as follows: tar -> directories-> ZIPs->XML.
Obviously I can manually extract a single TAR but I have about 1000 TAR archives that are about 3 GB each and contains about 6000 ZIP archives each. I'm looking for a way to handle the .tar archives in memory and extract the XML data of each ZIP. Is there a way to do this?
This should be doable, since all of the relevant methods have non-disk-related options.
Lots of loops here, so let's dig in.
For each tar archive:
tarfile.openwould open the tar archive. (Docs).getmemberson the resultingTarFileinstance to get a list of the zips (or other files) contained in the archive. (Docs)For each zip within the tar archive:
.extractfileon yourTarFileinstance to get a file object for that zip. (Docs)zipfile.ZipFilewith your file object in order to open the zip so you can work with it. (Docs).infoliston yourZipFileinstance to get a list of the files it contains (including your XML files). (Docs)For each XML file within the zip:
.openon yourZipFileinstance in order to get a file object of one of your XML files. (Docs).readit, copy it to disk somewhere, stick it in anElementTree(docs), etc.