Skip to content

ZipFile 压缩单个文件

python
import zipfile
import os

with zipfile.ZipFile('log.zip', 'w') as z:
    z.write('log.log')

解压文件

python
with zipfile.ZipFile('log.zip', 'r') as z:
    print(z.namelist())     # 查看压缩包中的文件列表
    print(z.read(z.namelist()[0]))  # 读出来压缩包中的第一个文件的内容打印到屏幕,也可保存到文件中
    # z.extractall('aaa')      # 解压,可设置解压路径
    # z.extract('log.log')      # 解压,可选择解压某个文件
    z.extractall()      # 解压全部

压缩某个目录下所有文件

python
def compress_file(zipfilename, dirname):      # zipfilename是压缩包名字,dirname是要打包的目录
    if os.path.isfile(dirname):
        with zipfile.ZipFile(zipfilename, 'w') as z:
            z.write(dirname)
    else:
        with zipfile.ZipFile(zipfilename, 'w') as z:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    if single_file != zipfilename:
                        filepath = os.path.join(root, single_file)
                        z.write(filepath)

compress_file('a.zip', '.')      # 执行函数

添加文件到已有的zip包中

python
def addfile(zipfilename, dirname):
    if os.path.isfile(dirname):
        with zipfile.ZipFile(zipfilename, 'a') as z:
            z.write(dirname)
    else:
        with zipfile.ZipFile(zipfilename, 'a') as z:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    if single_file != zipfilename:
                        filepath = os.path.join(root, single_file)
                        z.write(filepath)

addfile('a.zip', 'test.txt')

查看压缩包中的文件

python
def viewfile(zipfilename):
    with zipfile.ZipFile(zipfilename, 'r') as z:
        print(z.namelist())

viewfile('a.zip')

**TarFile** 压缩文件
python
import tarfile
import os

with tarfile.open('a.tar', 'w') as tar:
    tar.add('log.log', arcname='log.log')
    tar.add('test.txt', arcname='test.txt')

解压文件

python
with tarfile.open('a.tar', 'r') as tar:
    print(tar.getmembers())     # 查看压缩包内文件成员
    # tar.extract('test.txt')  # 可选择解压某个文件
    # tar.extractall('ccc')  # 可设置解压路径
    tar.extractall()  # 解压全部

压缩某个目录下所有文件

python
def compress_file(tarfilename, dirname):    # tarfilename是压缩包名字,dirname是要打包的目录
    if os.path.isfile(dirname):
        with tarfile.open(tarfilename, 'w') as tar:
            tar.add(dirname)
    else:
        with tarfile.open(tarfilename, 'w') as tar:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)

compress_file('test.tar', 'test.txt')
compress_file('t.tar', '.')

添加文件到已有的tar包中

python
def addfile(tarfilename, dirname):    # tarfilename是压缩包名字,dirname是要打包的目录
    if os.path.isfile(dirname):
        with tarfile.open(tarfilename, 'a') as tar:
            tar.add(dirname)
    else:
        with tarfile.open(tarfilename, 'a') as tar:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)

addfile('t.tar', 'ttt.txt')
addfile('t.tar', 'ttt')