Skip to content

Commit 2677cc9

Browse files
committed
add compress_zip.py
1 parent 278c210 commit 2677cc9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

compress_zip.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env -S uv run --script
2+
# /// script
3+
# dependencies = [
4+
# "zopflipy",
5+
# ]
6+
# ///
7+
8+
import io
9+
import sys
10+
import zipfile
11+
from collections.abc import Sequence
12+
from pathlib import Path
13+
from zopfli import ZipFile
14+
15+
def compress_zip_file(path: Path) -> str | None:
16+
data = io.BytesIO(path.read_bytes())
17+
18+
with ZipFile(data, "r", zipfile.ZIP_DEFLATED) as source:
19+
with ZipFile(path, "w", zipfile.ZIP_DEFLATED) as out:
20+
for zip_info in source.infolist():
21+
out.writestr(zip_info, source.read(zip_info.filename))
22+
23+
return None
24+
25+
def main(args: Sequence[str]) -> str | int:
26+
if not args:
27+
return f"USAGE: {sys.argv[0]} FILES.."
28+
for arg in args:
29+
path = Path(arg)
30+
if not path.is_file():
31+
return f"{path.as_posix()} is not a file"
32+
if err := compress_zip_file(path):
33+
return f"Could not compress {path.as_posix()}: {err}"
34+
35+
return 0
36+
37+
sys.exit(main(sys.argv[1:]))

0 commit comments

Comments
 (0)