forked from kaganisildak/bitchat-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.py
More file actions
26 lines (20 loc) · 703 Bytes
/
Copy pathcompression.py
File metadata and controls
26 lines (20 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import Tuple
import lz4.frame
COMPRESSION_THRESHOLD = 100
def compress_if_beneficial(data: bytes) -> Tuple[bytes, bool]:
"""Compress data if it reduces size"""
if len(data) < COMPRESSION_THRESHOLD:
return (data, False)
compressed = lz4.frame.compress(data)
if len(compressed) < len(data):
return (compressed, True)
else:
return (data, False)
def decompress(data: bytes) -> bytes:
"""Decompress LZ4 data"""
try:
return lz4.frame.decompress(data)
except Exception as e:
raise ValueError(f"Decompression failed: {e}")
# Export functions
__all__ = ['compress_if_beneficial', 'decompress', 'COMPRESSION_THRESHOLD']