import io
import tarfile
import zipfile

def string2tar(name, bString):
        tmpfile = io.BytesIO(bString)
        tarinfo = tarfile.TarInfo(name=name)
        tarinfo.size=len(bString)
        return (tarinfo, tmpfile)

def convert_zip_to_tar(zipname, tarname):
        with zipfile.ZipFile(zipname) as zip_file, tarfile.open(tarname, "w|xz") as tar_file:
                for zip_file_name in zip_file.namelist():
                        content = zip_file.read(zip_file_name)
                        tar_file.addfile(*string2tar(zip_file_name, content))

Published

Category

snippet

Tags