Alternating File Compression into ZIP

  • Share this:

Code introduction


Compress the contents of two files alternately into a ZIP file.


Technology Stack : Built-in libraries

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        with open('zipped_files.zip', 'wb') as f_zip:
            while True:
                block1 = f1.read(1024)
                block2 = f2.read(1024)
                if not block1 and not block2:
                    break
                f_zip.write(block1)
                f_zip.write(block2)