is it possible to zip a folder into multiple files at Python? I just found some examples how to zip a folder/file to a single zip container.
Short: How can I zip one folder to multiple zip parts in Python?
You can zip files into a giant file first, then split it into pieces. tested, it works.
# MAX = 500*1024*1024 # 500Mb - max chapter size
MAX = 15*1024*1024
BUF = 50*1024*1024*1024 # 50GB - memory buffer size
def file_split(FILE, MAX):
'''Split file into pieces, every size is MAX = 15*1024*1024 Byte'''
chapters = 1
uglybuf = ''
with open(FILE, 'rb') as src:
while True:
tgt = open(FILE + '.%03d' % chapters, 'wb')
written = 0
while written < MAX:
if len(uglybuf) > 0:
tgt.write(uglybuf)
tgt.write(src.read(min(BUF, MAX - written)))
written += min(BUF, MAX - written)
uglybuf = src.read(1)
if len(uglybuf) == 0:
break
tgt.close()
if len(uglybuf) == 0:
break
chapters += 1
def zipfiles(directory, outputZIP = 'attachment.zip'):
# path to folder which needs to be zipped
# directory = './outbox'
# calling function to get all file paths in the directory
file_paths = get_all_file_paths(directory)
# printing the list of all files to be zipped
print('Following files will be zipped:')
for file_name in file_paths:
print(file_name)
# writing files to a zipfile
with ZipFile(outputZIP,'w') as zip:
# writing each file one by one
for file in file_paths:
zip.write(file)
print('All files zipped successfully!')
if __name__ == '__main__':
outputZIP = 'attachment.zip'
zipfiles(directory, outputZIP)
file_split(outputZIP, MAX)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With