Skip to content

Instantly share code, notes, and snippets.

@SurendraTamang
Created March 23, 2024 05:40
Show Gist options
  • Save SurendraTamang/05d401707dea5afa599e0aab9c21e0fa to your computer and use it in GitHub Desktop.
Save SurendraTamang/05d401707dea5afa599e0aab9c21e0fa to your computer and use it in GitHub Desktop.
divide the large file in simple chunk zipped
import os
import zipfile
required_mb = 850
def zip_folder(folder_path, max_chunk_size_mb=required_mb):
# Convert MB to bytes for comparison
max_chunk_size = max_chunk_size_mb * 1024 * 1024
current_chunk_size = 0
chunk_num = 1
zip_file = None
zip_list = []
# List all files in the directory and sort them (optional)
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
files.sort() # This is optional but helps in maintaining a predictable order
for file in files:
file_path = os.path.join(folder_path, file)
file_size = os.path.getsize(file_path)
# Check if adding the current file would exceed the max chunk size
if current_chunk_size + file_size > max_chunk_size:
# Close the current zip file if it's open
if zip_file is not None:
zip_file.close()
zip_file = None
# Prepare for the next chunk
chunk_num += 1
current_chunk_size = 0
# If there's no zip file open, create a new one
if zip_file is None:
zip_filename = os.path.join(folder_path, f"chunk_{chunk_num}.zip")
zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
zip_list.append(zip_filename)
# Add the current file to the open zip file
zip_file.write(file_path, arcname=file)
current_chunk_size += file_size
# Make sure to close the last zip file if it's still open
if zip_file is not None:
zip_file.close()
return zip_list
# Example usage
folder_path = 'ig_images' # Update this path to your folder path
zip_files = zip_folder(folder_path)
print(f"Created zip files: {zip_files}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment