Created
June 28, 2023 15:31
-
-
Save MadMcCrow/0d7e2990b6d076d3c7f7525d2c84e730 to your computer and use it in GitHub Desktop.
Compress Everything !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# zip a file | |
def zip (pathstr) : | |
import zipfile, pathlib, shutil | |
dirpath = pathlib.Path(pathstr) | |
zip_file = f"{dirpath.stem}.zip" | |
original_size = 0 | |
compressed_size = 0 | |
# write to zip | |
with zipfile.ZipFile(zip_file, mode="w", allowZip64 = True, compression=zipfile.ZIP_LZMA) as archive: | |
# compress | |
for file_path in dirpath.iterdir(): | |
archive.write(file_path, arcname=file_path.name) | |
# calculate final size | |
for info in archive.infolist(): | |
original_size += info.file_size | |
compressed_size += info.compress_size | |
return original_size, compressed_size | |
# print info about a zip file | |
def zip_info(zip_file) : | |
try : | |
import zipfile | |
original_size = 0 | |
compressed_size = 0 | |
with zipfile.ZipFile(zip_file, mode="r") as archive: | |
for info in archive.infolist(): | |
original_size += info.file_size | |
compressed_size += info.compress_size | |
print(f"Filename: {zip_file}") | |
print(f"Normal size: {original_size} bytes") | |
print(f"Compressed size: {compressed_size} bytes") | |
print("-" * 20) | |
except : | |
pass | |
# find all folders to compress | |
def glob ( path ): | |
from os import listdir | |
from os.path import isdir, join | |
return [f for f in listdir(path) if isdir(join(path, f))] | |
# TODO : Support 7Zip or other better compression algorithm | |
def compress(folder : str) : | |
a, b = zip(folder) | |
if b < a : | |
import shutil | |
shutil.rmtree(folder, ignore_errors=True) | |
# compress with python zip= | |
def compress_all(folder : str): | |
import time | |
import concurrent.futures | |
import multiprocessing | |
folders = glob(folder) | |
print(f"compressing folders :{', '.join(folders)}") | |
start = time.time() | |
with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor: | |
executor.map(compress, folders) | |
duration = time.time() - start | |
print(f"compressed in {duration}") | |
print("-" * 20) | |
list(map(lambda s : zip_info(f"{s}.zip"), folders)) | |
# run script if called directly | |
if __name__ == "__main__": | |
import os | |
cwd = os.getcwd() | |
compress_all(cwd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment