|
| 1 | +import tarfile |
| 2 | +from tqdm import tqdm # pip3 install tqdm |
| 3 | + |
| 4 | + |
| 5 | +def decompress(tar_file, path, members=None): |
| 6 | + """ |
| 7 | + Extracts `tar_file` and puts the `members` to `path`. |
| 8 | + If members is None, all members on `tar_file` will be extracted. |
| 9 | + """ |
| 10 | + tar = tarfile.open(tar_file, mode="r:gz") |
| 11 | + if members is None: |
| 12 | + members = tar.getmembers() |
| 13 | + # with progress bar |
| 14 | + # set the progress bar |
| 15 | + progress = tqdm(members) |
| 16 | + for member in progress: |
| 17 | + tar.extract(member, path=path) |
| 18 | + # set the progress description of the progress bar |
| 19 | + progress.set_description(f"Extracting {member.name}") |
| 20 | + # or use this |
| 21 | + # tar.extractall(members=members, path=path) |
| 22 | + # close the file |
| 23 | + tar.close() |
| 24 | + |
| 25 | + |
| 26 | +def compress(tar_file, members): |
| 27 | + """ |
| 28 | + Adds files (`members`) to a tar_file and compress it |
| 29 | + """ |
| 30 | + # open file for gzip compressed writing |
| 31 | + tar = tarfile.open(tar_file, mode="w:gz") |
| 32 | + # with progress bar |
| 33 | + # set the progress bar |
| 34 | + progress = tqdm(members) |
| 35 | + for member in progress: |
| 36 | + # add file/folder/link to the tar file (compress) |
| 37 | + tar.add(member) |
| 38 | + # set the progress description of the progress bar |
| 39 | + progress.set_description(f"Compressing {member}") |
| 40 | + # close the file |
| 41 | + tar.close() |
| 42 | + |
| 43 | + |
| 44 | +# compress("compressed.tar.gz", ["test.txt", "test_folder"]) |
| 45 | +# decompress("compressed.tar.gz", "extracted") |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + import argparse |
| 49 | + parser = argparse.ArgumentParser(description="TAR file compression/decompression using GZIP.") |
| 50 | + parser.add_argument("method", help="What to do, either 'compress' or 'decompress'") |
| 51 | + parser.add_argument("-t", "--tarfile", help="TAR file to compress/decompress, if it isn't specified for compression, the new TAR file will be named after the first file to compress.") |
| 52 | + parser.add_argument("-p", "--path", help="The folder to compress into, this is only for decompression. Default is '.' (the current directory)", default="") |
| 53 | + parser.add_argument("-f", "--files", help="File(s),Folder(s),Link(s) to compress/decompress separated by ','.") |
| 54 | + |
| 55 | + args = parser.parse_args() |
| 56 | + method = args.method |
| 57 | + tar_file = args.tarfile |
| 58 | + path = args.path |
| 59 | + files = args.files |
| 60 | + |
| 61 | + # split by ',' to convert into a list |
| 62 | + files = files.split(",") if isinstance(files, str) else None |
| 63 | + |
| 64 | + if method.lower() == "compress": |
| 65 | + if not files: |
| 66 | + print("Files to compress not provided, exiting...") |
| 67 | + exit(1) |
| 68 | + elif not tar_file: |
| 69 | + # take the name of the first file |
| 70 | + tar_file = f"{files[0]}.tar.gz" |
| 71 | + compress(tar_file, files) |
| 72 | + elif method.lower() == "decompress": |
| 73 | + if not tar_file: |
| 74 | + print("TAR file to decompress is not provided, nothing to do, exiting...") |
| 75 | + exit(2) |
| 76 | + decompress(tar_file, path, files) |
| 77 | + else: |
| 78 | + print("Method not known, please use 'compress/decompress'.") |
| 79 | + |
0 commit comments