Compress Two Files into a Zip File

  • Share this:

Code introduction


Compresses the content of two files and stores it in a new zip file.


Technology Stack : Built-in file operations

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        zipped = zip(f1, f2)
        with open('zipped_files.zip', 'wb') as f_out:
            for block in zipped:
                f_out.write(block)
        return 'zipped_files.zip'