Skip to content

Instantly share code, notes, and snippets.

@Lucs1590
Created December 1, 2022 14:08
Show Gist options
  • Save Lucs1590/7a26bbc617728277f738f2bf399ea2d0 to your computer and use it in GitHub Desktop.
Save Lucs1590/7a26bbc617728277f738f2bf399ea2d0 to your computer and use it in GitHub Desktop.
How to save zip file chunks according to a batch of files.
import os
import zlib
import zipfile
from glob import glob
def compress(file_names: list):
compression = zipfile.ZIP_STORED
store_chunk_limit = 1_000_000_000 # 1 GB
store_chunk_size = 0
files_chunk = []
count = 0
for file_name in file_names:
if store_chunk_size < store_chunk_limit:
files_chunk.append(file_name)
store_chunk_size += os.path.getsize(file_name)
else:
print(f"Compressing chunk {count} with {len(files_chunk)} files")
with zipfile.ZipFile(f"place_to_save/chunk_{count}.zip", "w", compression) as zf:
for file in files_chunk:
zf.write(file)
zf.close()
count += 1
files_chunk = [file_name]
store_chunk_size = os.path.getsize(file_name)
file_names = glob('path_of_files/*.jpg')
compress(file_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment