Create Zip File from Directory

  • Share this:

Code introduction


This function creates a zip file containing all files and folders from a specified directory.


Technology Stack : The packages and technologies used in this code[English]

Code Type : Function

Code Difficulty :


                
                    
def zipfile_create(input_path, output_path):
    import zipfile
    import os

    # 创建一个zip文件
    with zipfile.ZipFile(output_path, 'w') as zipf:
        # 遍历输入路径中的所有文件
        for foldername, subfolders, filenames in os.walk(input_path):
            for filename in filenames:
                # 将文件添加到zip文件中
                zipf.write(os.path.join(foldername, filename), arcname=os.path.join(os.path.relpath(foldername, input_path), filename))

    return output_path