brienna
brienna

Reputation: 1604

Extract file inside a .gz that's inside a .tar without unzipping the two

I need to extract .tex files from multiple .gz files that are inside a single .tar file. I wrote some code that does this successfully, but I am unzipping the .tar and every .gz file. Is there a way to avoid doing so much unzipping? I would like to navigate straight to the .tex files and only extract these.

def extractFile(filename):
    tar = tarfile.open(filename)
    for item in tar:
        # Extract from .tar into 'temp' subfolder only if .gz
        if item.name.endswith('.gz'):
            item.name = os.path.basename(item.name) # reset path to remove parent directories like '0001'
            if not os.path.isdir('temp'):
                os.makedirs('temp')
            tar.extract(item, path='temp')
            # Extract from .gz into 'temp' subfolder only if .tex
            try: 
                gz = tarfile.open('temp/' + item.name, mode='r:gz')
                for file in gz:
                    if file.name.endswith('.tex'):
                        gz.extract(file, path='latex')
            except tarfile.ReadError:
                # Move to 'error' folder, ensuring it exists
                if not os.path.isdir('error'):
                    os.makedirs('error')
                os.rename('temp/' + item.name, 'error/' + item.name)

Upvotes: 0

Views: 723

Answers (1)

brienna
brienna

Reputation: 1604

I was able to answer my question with the help of the comments. (Thanks!) My code now extracts .tex files from multiple .gz files that are inside a single .tar file, without unzipping/saving each .gz file to the computer.

def extractFile(filename):
    tar = tarfile.open(filename)
    for subfile in tar.getmembers():
        # Open subfile only if .gz
        if subfile.name.endswith('.gz'):
            try: 
                gz = tar.extractfile(subfile)
                gz = tarfile.open(fileobj=gz)
                # Extract file from .gz into 'latex' subfolder only if .tex
                for subsubfile in gz.getmembers():
                    if subsubfile.name.endswith('.tex'):
                        gz.extract(subsubfile, path='latex')
            except tarfile.ReadError:
                # Add subfile name to error log
                with open('error_log.txt', 'a') as log:
                    log.write(subfile.name + '\n')

Upvotes: 1

Related Questions