add gzip
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import os
|
||||
import sys
|
||||
import zlib
|
||||
|
||||
import brotli
|
||||
|
||||
DECOMPRESSED_SIZE = 50 * 1024 * 1024 * 1024
|
||||
RANDOM_BYTES = 1024 * 1024
|
||||
BROTLI_QUALITY = 5
|
||||
GZIP_LEVEL = 6
|
||||
|
||||
_HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
PNG_PATH = os.path.join(_HERE, "image.png")
|
||||
BR_PATH = os.path.join(_HERE, "bomb.png.br")
|
||||
GZIP_PATH = os.path.join(_HERE, "bomb.png.gz")
|
||||
|
||||
|
||||
def _load_real_png() -> bytes:
|
||||
@@ -36,16 +39,39 @@ def _build_brotli(png_bytes: bytes) -> bytes:
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if os.path.exists(BR_PATH) and "--rebuild" not in sys.argv:
|
||||
print(f"{os.path.basename(BR_PATH)} already exists; pass --rebuild to overwrite.")
|
||||
return
|
||||
def _build_gzip(png_bytes: bytes) -> bytes:
|
||||
co = zlib.compressobj(GZIP_LEVEL, zlib.DEFLATED, 16 + zlib.MAX_WBITS)
|
||||
out = bytearray()
|
||||
out += co.compress(png_bytes)
|
||||
out += co.compress(os.urandom(RANDOM_BYTES))
|
||||
|
||||
png_bytes = _load_real_png()
|
||||
data = _build_brotli(png_bytes)
|
||||
with open(BR_PATH, "wb") as f:
|
||||
remaining = DECOMPRESSED_SIZE - len(png_bytes) - RANDOM_BYTES
|
||||
block = b"\x00" * (16 * 1024 * 1024)
|
||||
while remaining > 0:
|
||||
n = min(len(block), remaining)
|
||||
out += co.compress(block[:n])
|
||||
remaining -= n
|
||||
|
||||
out += co.flush()
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def _write_bomb(path: str, builder, png_bytes: bytes, rebuild: bool) -> None:
|
||||
name = os.path.basename(path)
|
||||
if os.path.exists(path) and not rebuild:
|
||||
print(f"{name} already exists; pass --rebuild to overwrite.")
|
||||
return
|
||||
data = builder(png_bytes)
|
||||
with open(path, "wb") as f:
|
||||
f.write(data)
|
||||
print(f"wrote {os.path.basename(BR_PATH)} ({len(data):,} bytes)")
|
||||
print(f"wrote {name} ({len(data):,} bytes)")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
rebuild = "--rebuild" in sys.argv
|
||||
png_bytes = _load_real_png()
|
||||
_write_bomb(BR_PATH, _build_brotli, png_bytes, rebuild)
|
||||
_write_bomb(GZIP_PATH, _build_gzip, png_bytes, rebuild)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user