| """ |
| Zip a (possibly nested) directory into multiple zip archives, each with CHUNK_SIZE files. |
| Unzipping any archive will recreate the original relative structure under INPUT_DIR. |
| """ |
|
|
| import os |
| from pathlib import Path |
| import zipfile |
| from tqdm import tqdm |
| from itertools import islice |
|
|
|
|
| |
| |
|
|
| CHUNK_SIZE = 1000 |
| EXTS = [".png"] |
|
|
| INPUT_2_ZIP_DIRS = { |
| "images/images1024x1024": "images_zip/images1024x1024", |
| "images/custom1024x1024": "images_zip/custom1024x1024", |
| "images/custom256x256_matte_white_background": "images_zip/custom256x256_matte_white_background", |
| "images/custom512x512_matte_white_background": "images_zip/custom512x512_matte_white_background", |
| "images/custom1024x1024_skin_tones": "images_zip/custom1024x1024_skin_tones", |
| "images/images512x512_matte_white_background_categorised": "images_zip/images512x512_matte_white_background_categorised", |
| "images/images512x512_matte_white_background_similars": "images_zip/images512x512_matte_white_background_similars", |
| |
| "segmaps/custom1024x1024": "segmaps_zip/custom1024x1024", |
| "segmaps/segmaps512x512": "segmaps_zip/segmaps512x512", |
| } |
| |
| |
|
|
|
|
|
|
|
|
|
|
| |
| def batched(iterable, n): |
| it = iter(iterable) |
| while True: |
| batch = list(islice(it, n)) |
| if not batch: |
| break |
| yield batch |
|
|
| def want_file(p: Path) -> bool: |
| return "*" in EXTS or p.suffix.lower() in EXTS |
|
|
| |
|
|
| for INPUT_DIR, ZIP_DIR in tqdm(INPUT_2_ZIP_DIRS.items()): |
| INPUT_DIR = Path(INPUT_DIR) |
| ZIP_DIR = Path(ZIP_DIR) |
| |
| ZIP_DIR.mkdir(parents=True, exist_ok=True) |
|
|
| |
| all_files = sorted([p for p in INPUT_DIR.rglob("*") if p.is_file() and want_file(p)]) |
|
|
| for idx, chunk in enumerate(tqdm(list(batched(all_files, CHUNK_SIZE)), desc="Chunks")): |
| zip_path = ZIP_DIR / f"images_part_{idx:05}.zip" |
| with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf: |
| for f in tqdm(chunk, desc="Files in chunk", leave=False): |
| |
| arcname = f.relative_to(INPUT_DIR) |
| zf.write(f, arcname) |
| print(f"Created: {zip_path} with {len(chunk)} files") |
|
|
| print("DONE.") |
|
|