-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcompress.py
27 lines (25 loc) · 1.02 KB
/
compress.py
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
27
import const
import os
if const.CHAT_COMPRESS == "brotli":
import brotli
import tempfile
elif const.CHAT_COMPRESS == "zstd":
import zstandard
import tempfile
if const.CHAT_COMPRESS == "brotli":
def compress_file(file):
with open(file, encoding="utf8") as f:
compressor = brotli.Compressor(mode=brotli.BrotliEncoderMode.TEXT)
with tempfile.NamedTemporaryFile(prefix=(os.path.basename(file)+"."), suffix=".br", delete=False) as l:
for line in f:
data = line.encode()
data = compressor.compress(data)
l.write(compressor.flush())
l.write(compressor.finish())
return l.name
elif const.CHAT_COMPRESS == "zstd":
def compress_file(file):
cctx = zstandard.ZstdCompressor()
with open(file, "rb") as ifh, tempfile.NamedTemporaryFile(prefix=(os.path.basename(file)+"."), suffix=".zst", delete=False) as ofh:
cctx.copy_stream(ifh, ofh)
return ofh.name