From c1327adf6bdc41fc3030b7a149f60fad6c319a3a Mon Sep 17 00:00:00 2001 From: Wout Bouckaert Date: Sun, 11 Aug 2024 09:31:34 -0600 Subject: [PATCH] Add zlib compress and decompress functions to file_helpers.py --- app/classes/shared/file_helpers.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index 1abed6a6..8069e26e 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -4,6 +4,7 @@ import logging import pathlib import tempfile import zipfile +import zlib import hashlib from typing import BinaryIO import mimetypes @@ -166,7 +167,7 @@ class FileHelpers: with value_to_hash.open("rb") as f: while True: # Read 2^16 byte chunks, 64 KiB. - data = value_to_hash.read(65536) + data = f.read(65536) # Break if end of file if not data: @@ -444,3 +445,29 @@ class FileHelpers: zip_ref.extractall(temp_dir) if user_id: return temp_dir + + @staticmethod + def zlib_compress_bytes(bytes_to_compress: bytes) -> bytes: + """Compress provided bytes using zlib default compression settings. Returns + compressed bytes. + + Args: + bytes_to_compress (bytes): Bytes to compress. + + Returns: + bytes: Compressed bytes. + """ + return zlib.compress(bytes_to_compress) + + @staticmethod + def zlib_decompress_bytes(bytes_to_decompress: bytes) -> bytes: + """Decompress provided bytes using zlib default settings. Returns decompressed + bytes. + + Args: + bytes_to_decompress (bytes): Compresses bytes to decompress. + + Returns: + bytes: Decompressed bytes. + """ + return zlib.decompress(bytes_to_decompress)