Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip a folder into parts?

Tags:

python

zip

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?

like image 881
Philipp Avatar asked Mar 22 '23 02:03

Philipp


1 Answers

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)
like image 155
Chen Du Avatar answered Apr 02 '23 02:04

Chen Du