Compress Two Files into a Zip Archive

  • Share this:

Code introduction


This function compresses two files into a single zip archive. It first creates a zip file and then adds both files to the zip file.


Technology Stack : os, zipfile

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2):
    """
    Compress two files into a zip archive.
    """
    import zipfile
    import os

    # Create a zip file with both files
    with zipfile.ZipFile('combined.zip', 'w') as zipf:
        zipf.write(file1, arcname=os.path.basename(file1))
        zipf.write(file2, arcname=os.path.basename(file2))

    return "combined.zip"                
              
Tags: