Skip to content

Instantly share code, notes, and snippets.

@burak-kara
Created April 13, 2022 08:58
Show Gist options
  • Save burak-kara/e90ad15fb44403c139fae818488a3f6b to your computer and use it in GitHub Desktop.
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+
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