Python 以相对/绝对路径的方式压缩文件


1. tarfile 简单介绍

Python 的 tarfile 模块提供了对 .tar 格式归档文件的全面支持,允许用户创建、读取、修改和写入 tar 归档文件。在实际应用中,tar 文件通常用于将多个文件或目录打包成一个单一的档案文件,便于存储、传输或者备份。此外,tar 文件还可以配合压缩工具(如 gzip, bzip2, 或 lzma)生成诸如 .tar.gz, .tar.bz2, 或 .tar.xz 等格式的压缩归档。

以下是一些 tarfile 模块的核心功能:

  1. 打开和创建归档

    • 使用 tarfile.open() 函数可以打开已存在的 tar 归档文件,也可以创建新的归档文件。模式参数(mode)决定了操作行为,比如 'r' 用于只读、 'w' 用于覆盖写入、 'a' 用于追加以及 'x' 用于独占创建等。
  2. 添加文件到归档

    • 创建 TarFile 对象后,可以使用 add() 方法将单个文件或整个目录结构添加到归档中。
  3. 从归档中提取文件

    • 使用 extractall() 方法可以将归档中的所有文件解压到指定目录。
    • 通过 extract() 方法可以单独提取归档内的某个成员文件。
  4. 读取归档信息

    • 可以遍历 TarFile 对象获取每个成员(即归档中的文件)的信息,包括名称、大小、权限、时间戳等。
    • 也可以通过 getmember() 方法查找并返回特定成员的信息。
  5. 支持压缩

    • 如上述模式参数所示,tarfile.open() 函数可以直接处理压缩过的 tar 归档文件,并且支持多种压缩格式,如 'r:gz' 表示读取gzip压缩的tar文件, 'w:xz' 表示创建lzma压缩的tar文件等。
  6. 其他高级功能

    • 支持自定义归档成员的属性(metadata)。
    • 支持存档加密(尽管标准tarfile模块不直接提供加密功能,但可以通过与其他库结合实现)。

举例说明如何使用 tarfile 模块创建一个简单的tar归档:

import tarfile

# 创建一个新的tar归档文件(不压缩)
with tarfile.open("example.tar", mode="w") as tar:
    # 将当前目录下的"file.txt"文件添加到归档
    tar.add("file.txt")

# 打开一个已存在的gzip压缩过的tar归档文件进行读取
with tarfile.open("example.tar.gz", mode="r:gz") as tar:
    # 提取归档中的所有文件到当前目录
    tar.extractall()

2. tarfile 支持的模式

modeaction
'r''r:*'Open for reading with transparent compression (recommended). 自动解压并打开文件–推荐模式
'r:'Open for reading exclusively without compression. 只打开文件不解压
'r:gz'Open for reading with gzip compression. 采用gzip格式解压并打开文件
'r:bz2'Open for reading with bzip2 compression. 采用bz2格式解压并打开文件
'r:xz'Open for reading with lzma compression. 采用lzma格式解压并打开文件
'x''x:'Create a tarfile exclusively without compression. Raise an [FileExistsError]exception if it already exists. 仅创建打包文件,不压缩
'x:gz'Create a tarfile with gzip compression. Raise an [FileExistsError]exception if it already exists. 采用gzip方式压缩并打包文件
'x:bz2'Create a tarfile with bzip2 compression. Raise an [FileExistsError]exception if it already exists. 采用bzip2方式压缩并打包文件
'x:xz'Create a tarfile with lzma compression. Raise an [FileExistsError]exception if it already exists. 采用lzma方式压缩并打包文件
'a''a:'Open for appending with no compression. The file is created if it does not exist. 打开文件,并以不压缩的方式追加内容。如果文件不存在,则新建
'w''w:'Open for uncompressed writing. 以不压缩的方式写入,即只归档不压缩
'w:gz'Open for gzip compressed writing. 以gzip的方式压缩并写入
'w:bz2'Open for bzip2 compressed writing. 以bzip2的方式压缩并写入
'w:xz'Open for lzma compressed writing. 以lzma的方式压缩并写入

3. 绝对路径压缩

下面对 /User/xx/data/file.txt 进行压缩,这个是以绝对路径压缩的。

    def compress(compress_file_path, ori_file_path):
        with tarfile.open(compress_file_path, "w:gz") as tar:
            tar.add(ori_file_path)
if __name__ == "__main__":
	compress("~/dataset/outfile.tgz", "~/dataset/file.txt")

绝对路径压缩后,其解压后如下:
在这里插入图片描述
也就是说会保留你当时解压时的绝对路径!

4. 相对路径压缩

增加参数 arcname,例如,下面对 /User/xx/data/file.txt 进行压缩,这个是以相对路径压缩的,即在任何地方解压后就直接可以得到这个文件file.txt

    def compress(compress_file_path, ori_file_path):
        with tarfile.open(compress_file_path, "w:gz") as tar:
            tar.add(ori_file_path, arcname=os.path.basename(ori_file_path))

if __name__ == "__main__":
	compress("/User/xx/data/outfile.tgz", "/User/xx/data/file.txt")

5. 参考

https://www.cnblogs.com/hls-code/p/16626565.html

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SmallerFL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值