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