From 7e2a7046a57f43bebd9145c58acc72269bc1c77c Mon Sep 17 00:00:00 2001 From: toto <> Date: Mon, 20 Sep 2021 16:47:10 +0200 Subject: [PATCH] Add compress.py --- README.md | 8 ++++---- compress.py | 42 ++++++++++++++++++++++++++++++++++++++++++ crypt.py | 12 +++--------- main.py | 12 ++++++++++-- 4 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 compress.py diff --git a/README.md b/README.md index 76a8f29..8d21255 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Script to recursively encrypt files and folders with AES ## -Source : -https://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto -https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html -https://nitratine.net/blog/post/python-encryption-and-decryption-with-pycryptodome/ +Source : +https://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto +https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html +https://nitratine.net/blog/post/python-encryption-and-decryption-with-pycryptodome/ diff --git a/compress.py b/compress.py new file mode 100644 index 0000000..ad12bc5 --- /dev/null +++ b/compress.py @@ -0,0 +1,42 @@ +import os +import re +import gzip + + +def recurse(path, maxsize="50M"): + max = parse_size(maxsize) + for f in os.listdir(path): + uri = os.path.join(path, f) + if os.path.isfile(uri): + size = os.path.getsize(uri) + Hsize = str(human_readable_size(size, 0)) + print(f + " : " + Hsize) + if size > max: + compress(uri) + elif os.path.isdir(uri): + recurse(uri) + + +def compress(file): + with open(file, 'rb') as infile: + with gzip.open(file + ".gz", 'wb') as zipfile: + while chunk := infile.read(64 * 1024): + zipfile.write(chunk) + print(file + " compressed") + pass + + +def human_readable_size(size, decimal_places=2): + for unit in ['B', 'K', 'M', 'G', 'T']: + if size < 1024.0: + break + size /= 1024.0 + return f"{size:.{decimal_places}f}{unit}" + + +def parse_size(size): + units = {"B": 1, "K": 2**10, "M": 2**20, "G": 2**30, "T": 2**40} + if size[-1].isdigit(): + size = size + 'K' + number, unit = re.match(r"([0-9]+)([BKMGT])", size, re.I).groups() + return int(float(number)*units[unit]) \ No newline at end of file diff --git a/crypt.py b/crypt.py index 6556d4a..f4d2184 100644 --- a/crypt.py +++ b/crypt.py @@ -33,11 +33,8 @@ def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024): outfile.write(struct.pack('