Page 1 of 1

problem to uncompreess .tar file

Posted: Sat Apr 10, 2021 8:21 pm
by tomasbaldi
Hi! i have a problem when i try uncompress a .tar file with utarfile library. When execute this program, all files are descompress, but are empty.

Code: Select all

import os
from lib.shutil import shutil
from lib.utarfile import utarfile

t = utarfile.TarFile('file.tar')

for i in t:
    print(i)
    if i.type == utarfile.DIRTYPE:
        os.makedirs(i.name)
    else:
        f = t.extractfile(i)
        shutil.copyfileobj(f, open(i.name, "wb"))
This is based on example-extract.py attached in utarfile library.

Re: problem to uncompreess .tar file

Posted: Fri Aug 05, 2022 1:05 am
by davidsmith
the example py script don't close file

Code: Select all

def tar_extract(file_name):
    """This function extracts an uncompressed tar file into the directory name
    it was archived from"""
    tar = utarfile.TarFile(file_name)
    for i in tar:
        print(i)
        if i.type == utarfile.DIRTYPE:
            if i.name != './':
                os.mkdir(i.name.strip('/'))
        else:
            sub_file = tar.extractfile(i)
            with open(i.name, "wb") as dest:
                copyfileobj(sub_file, dest)
                dest.close()
use this function