9. 压缩文件

from zipfile import ZipFile

def zip_in():
    with ZipFile('./办公自动化/files/合并PDF.zip', 'w') as zip:
        zip.write('合并PDF.pdf')
        zip.write('拆分PDF_0.pdf')
        zip.write('拆分PDF_1.pdf')

def zip_dir_or_notdir(target_dir, to_file):
    import os
    import zipfile
    import re

    target_dir = target_dir
    to_file = to_file
    root_dir = os.path.dirname(target_dir)
    dir_name = os.path.basename(target_dir)
    cur_dir = os.getcwd()
    # print(root_dir, dir_name, cur_dir)   # D:\git-python\办公自动化 files D:\git-python
    os.chdir(root_dir)
    to_file_name = os.path.basename(to_file)
    # print("to_file_name", to_file_name)   # to_file_name PDF.zip
    zip_file_dir = os.path.join(target_dir, to_file_name)
    # print(zip_file_dir)   # D:\git-python\办公自动化\files\PDF.zip
    with zipfile.ZipFile(zip_file_dir, "w", zipfile.ZIP_DEFLATED) as f:
        for rootdir, dirlist, filelist in os.walk(dir_name, topdown=True):
            # print(rootdir, dirlist, filelist)
            for name in filelist:
                if re.search('pdf$', name):
                    # print(name)

                    # target_file = os.path.join(dir_name, name)  # 打包文件所在目录名
                    # f.write(target_file)

                    os.chdir(target_dir)
                    f.write(name)    # 不打包文件当前目录名

    os.chdir(cur_dir)
    return zip_file_dir

    # zipfile.ZipFile的压缩模式和压缩级别
    # compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
    #              ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma).
    # compresslevel: None (default for the given compression type) or an integer
    #                specifying the level to pass to the compressor.
    #                When using ZIP_STORED or ZIP_LZMA this keyword has no effect.
    #                When using ZIP_DEFLATED integers 0 through 9 are accepted.
    #                When using ZIP_BZIP2 integers 1 through 9 are accepted.

def zip_list(zip_path):
    with ZipFile(zip_path, 'r') as zip:
        print(zip.namelist())

def zip_out(zip_path, out_dir):
    with ZipFile(zip_path, 'r') as zip:
        # print(zip.namelist())   # ['加密PDF.pdf', '合并PDF.pdf', '拆分PDF_0.pdf', '拆分PDF_1.pdf', '违章通知书.pdf']
        # zip.extract('违章通知书.pdf', out_dir)  # 解压指定的一个文件出来
        zip.extractall(out_dir)   # 解压出所有文件至指定目录。指定目录可以不创建,目录存在则内容全部解压出来放进目录,目录不存在,则解压目录为指定目录

def tar_in():
    import tarfile
    import os

    with tarfile.open('./办公自动化/files/合并PDF.tar', 'w') as tar:
        os.chdir('./办公自动化/files/')
        tar.add('合并PDF.pdf', arcname='haha.pdf')   # 可改名
        tar.add('拆分PDF_0.pdf')
        tar.add('拆分PDF_1.pdf')

def tar_out():
    import tarfile

    with tarfile.open('./办公自动化/files/合并PDF.tar', 'r') as tar:
        print(tar.getmembers())   # [<TarInfo 'haha.pdf' at 0x25b8a9d1480>, <TarInfo '拆分PDF_0.pdf' at 0x25b8a9d1300>, <TarInfo '拆分PDF_1.pdf' at 0x25b8a9d10c0>]
        tar.extractall('./办公自动化/files/tar')

if __name__ == '__main__':
    # zip_in()
    # print(zip_dir_or_notdir("D:\\git-python\\办公自动化\\files","PDF.zip"))  # D:\git-python\办公自动化\files\PDF.zip
    # zip_list('D:\\git-python\\办公自动化\\files\\PDF.zip')  # ['加密PDF.pdf', '合并PDF.pdf', '拆分PDF_0.pdf', '拆分PDF_1.pdf', '违章通知书.pdf']
    # zip_out('D:\\git-python\\办公自动化\\files\\PDF.zip', 'D:\\git-python\\办公自动化\\files\\test')
    # tar_in()
    tar_out()