Create and Merge Zip Files for Two Inputs

  • Share this:

Code introduction


This function creates separate zip files for two input files, and then merges these zip files into a single zip file.


Technology Stack : os, zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    import os
    import zipfile

    def create_zip(source_dir, zip_name):
        with zipfile.ZipFile(zip_name, 'w') as zipf:
            zipf.write(source_dir, arcname=os.path.basename(source_dir))
        return zip_name

    def zip_two_files(file1, file2, output_zip):
        with zipfile.ZipFile(output_zip, 'w') as zipf:
            zipf.write(file1, arcname=os.path.basename(file1))
            zipf.write(file2, arcname=os.path.basename(file2))

    zip_name = create_zip(file1, 'output1.zip')
    zip_name = create_zip(file2, 'output2.zip')
    zip_two_files(file1, file2, 'combined.zip')

    return 'combined.zip'                
              
Tags: