80 lines
1.9 KiB
Python
Executable File
80 lines
1.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
"""zip2tar """
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
import os
|
|
from zipfile import ZipFile
|
|
import tarfile
|
|
import time
|
|
|
|
|
|
def get_opts():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"-q",
|
|
dest="quiet",
|
|
action="store_true",
|
|
default=False,
|
|
help="Quiet operation",
|
|
)
|
|
parser.add_argument(
|
|
"-f",
|
|
dest="force",
|
|
action="store_true",
|
|
default=False,
|
|
help="Overwrite target",
|
|
)
|
|
parser.add_argument(
|
|
"zip",
|
|
action="store",
|
|
help="Zip to convert",
|
|
)
|
|
parser.add_argument(
|
|
"tar",
|
|
action="store",
|
|
help="Output filename, defaults to [zip].tgz. Recognizes extensios: .tar, .tgz, .gz, .bz2",
|
|
default=None,
|
|
nargs="?",
|
|
)
|
|
|
|
parsed = parser.parse_args()
|
|
if parsed.tar == None:
|
|
parsed.tar = os.path.splitext(parsed.zip)[0] + ".tgz"
|
|
return parsed
|
|
|
|
|
|
def main():
|
|
opts = get_opts()
|
|
|
|
if not opts.force and os.path.exists(opts.tar):
|
|
raise FileExistsError(opts.tar)
|
|
|
|
mode = "w"
|
|
if opts.tar.endswith(".tgz") or opts.tar.endswith(".gz"):
|
|
mode = "w:gz"
|
|
if opts.tar.endswith(".bz2"):
|
|
mode = "w:bz2"
|
|
|
|
with ZipFile(opts.zip) as zipf:
|
|
with tarfile.open(opts.tar, mode) as tarf:
|
|
for zip_info in zipf.infolist():
|
|
if not opts.quiet:
|
|
print(zip_info.filename, zip_info.file_size)
|
|
|
|
tar_info = tarfile.TarInfo(name=zip_info.filename)
|
|
tar_info.size = zip_info.file_size
|
|
tar_info.mtime = time.mktime(zip_info.date_time + (-1, -1, -1))
|
|
if zip_info.is_dir():
|
|
tar_info.type = tarfile.DIRTYPE
|
|
tarf.addfile(tarinfo=tar_info)
|
|
else:
|
|
tarf.addfile(tarinfo=tar_info, fileobj=zipf.open(zip_info.filename))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|