Compressing File Contents into Zip Files

  • Share this:

Code introduction


This function compresses the contents of two files into two new zip files.


Technology Stack : Built-in file operations and zip library

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        z = zip(f1, f2)
        with open(file1 + '.zip', 'wb') as f1_zip, open(file2 + '.zip', 'wb') as f2_zip:
            for item1, item2 in z:
                f1_zip.write(item1)
                f2_zip.write(item2)