Created
April 13, 2022 08:58
-
-
Save burak-kara/e90ad15fb44403c139fae818488a3f6b to your computer and use it in GitHub Desktop.
compress, decompress and check MD5 hash of files using gzip in python 3.8+
This file contains hidden or 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
import gzip | |
import shutil | |
import hashlib | |
import os | |
original_file = "file/path" | |
zipped_file = "file/path" | |
unzipped_file = "file/path" | |
def zip(): | |
with open(original_file, 'rb') as src: | |
with gzip.open(zipped_file, 'wb') as dst: | |
shutil.copyfileobj(src, dst) | |
def unzip(): | |
with gzip.open(zipped_file, 'rb') as src: | |
with open(unzipped_file, 'wb') as dst: | |
shutil.copyfileobj(src, dst) | |
def get_md5(file): | |
return hashlib.md5(open(file, 'rb').read()).hexdigest() | |
def to_kb(size): | |
return round(size / 1024, 2) | |
def get_file_size(file): | |
return to_kb(os.path.getsize(file)) | |
zip() | |
unzip() | |
original = get_md5(original_file) | |
zipped = get_md5(zipped_file) | |
unzipped = get_md5(unzipped_file) | |
print('Same file:', original == unzipped) | |
print("Original size: {}".format(get_file_size(original_file))) | |
print("Zipped size: {}".format(get_file_size(zipped_file))) | |
print("Unzipped size: {}".format(get_file_size(unzipped_file))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment