problem to uncompreess .tar file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
tomasbaldi
Posts: 1
Joined: Sat Apr 10, 2021 4:18 pm

problem to uncompreess .tar file

Post by tomasbaldi » Sat Apr 10, 2021 8:21 pm

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.

davidsmith
Posts: 2
Joined: Thu Apr 23, 2020 11:35 am

Re: problem to uncompreess .tar file

Post by davidsmith » Fri Aug 05, 2022 1:05 am

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

Post Reply