脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 基于Python实现文件的压缩与解压缩

基于Python实现文件的压缩与解压缩

2022-11-17 11:40Python编程学习圈 Python

在日常工作中,除了会涉及到使用Python处理文本文件,有时候还会涉及对压缩文件的处理。本文为大家总结了利用Python可以实现的几种文件压缩与解压缩实现代码,需要的可以参考一下

在日常工作中,除了会涉及到使用Python处理文本文件,有时候还会涉及对压缩文件的处理。

基于Python实现文件的压缩与解压缩

通常会涉及到的压缩文件格式有:

  • rar:Windows 环境下用的比较多的压缩,比较著名的GUI工具是winrar
  • tar: Linux系统下的打包工具,只打包,不压缩
  • gz:即gzip,通常只能压缩一个文件。与tar结合起来就可以实现先打包,再压缩。
  • tgz:即gz。先用tar打包,然后再用gz压缩得到的文件
  • zip:不同于gzip,虽然使用相似的算法,可以打包压缩多个文件,不过分别压缩文件,压缩率低于tar
  • 7z:7zip压缩软件支持的格式,压缩效率较高。

当然除了使用Python外,你还可以选择使用压缩解压缩软件或命令手动进行处理。

zip文件

zipfile是Python里用来做zip格式编码的压缩和解压缩的module,zipfile里有两个非常重要的class:ZipFile和ZipInfo。ZipFile是主要的类,用来创建和读取zip文件,而ZipInfo是存储的zip文件的每个文件的信息。

示例代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import zipfile
 
 
# 压缩
def make_zip(source_dir, output_filename):
    zipf = zipfile.ZipFile(output_filename, 'w')
    pre_len = len(os.path.dirname(source_dir))
    for parent, dirnames, filenames in os.walk(source_dir):
        for filename in filenames:
            print(filename)
            pathfile = os.path.join(parent, filename)
            arcname = pathfile[pre_len:].strip(os.path.sep)  # 相对路径
            zipf.write(pathfile, arcname)
        print()
    zipf.close()
 
 
# 解压缩
def un_zip(file_name):
    """unzip zip file"""
    zip_file = zipfile.ZipFile(file_name)
    if os.path.isdir(file_name + "_files"):
        pass
    else:
        os.mkdir(file_name + "_files")
    for names in zip_file.namelist():
        zip_file.extract(names, file_name + "_files/")
    zip_file.close()
 
 
if __name__ == '__main__':
    make_zip(r"E:python_samplelibstest_tar_fileslibs", "test.zip")
    un_zip("test.zip")

tar.gz文件

tarfile 模块可以用来读写 tar 归档,包括使用 gzip, bz2 和 lzma 压缩的归档。在使用tarfile是必须了解模式:

mode 必须是 ‘filemode[:compression]’ 形式的字符串,其默认值为 ‘r’。以下是模式组合的完整列表:

模式 动作
‘r’ or ‘r:*’ 打开和读取使用透明压缩(推荐)。
‘r:’ 打开和读取不使用压缩。
‘r:gz’ 打开和读取使用gzip 压缩。
‘r:bz2’ 打开和读取使用bzip2 压缩。
‘r:xz’ 打开和读取使用lzma 压缩。
‘x’ 或 ‘x:’ 创建tarfile不进行压缩。如果文件已经存在,则抛出 FileExistsError 异常。
‘x:gz’ 使用gzip压缩创建tarfile。如果文件已经存在,则抛出 FileExistsError 异常。
‘x:bz2’ 使用bzip2 压缩创建tarfile。如果文件已经存在,则抛出 FileExistsError 异常。
‘x:xz’ 使用lzma 压缩创建tarfile。如果文件已经存在,则抛出 FileExistsError 异常。
‘a’ or ‘a:’ 打开以便在没有压缩的情况下追加。如果文件不存在,则创建该文件。
‘w’ or ‘w:’ 打开用于未压缩的写入。
‘w:gz’ 打开用于 gzip 压缩的写入。
‘w:bz2’ 打开用于 bzip2 压缩的写入。
‘w:xz’ 打开用于 lzma 压缩的写入。

针对特殊的目的,还存在第二种 mode 格式: ‘filemode|[compression]’。tarfile.open() 将返回一个将其数据作为数据块流来处理的 TarFile 对象:

模式 动作
‘r|*’ 打开 tar 块的 流 以进行透明压缩读取。
‘r|’ 打开一个未压缩的 tar 块的 stream 用于读取。
‘r|gz’ 打开一个 gzip 压缩的 stream 用于读取。
‘r|bz2’ 打开一个 bzip2 压缩的 stream 用于读取。
‘r|xz’ 打开一个 lzma 压缩 stream 用于读取。
‘w|’ 打开一个未压缩的 stream 用于写入。
‘w|gz’ 打开一个 gzip 压缩的 stream 用于写入。
‘w|bz2’ 打开一个 bzip2 压缩的 stream 用于写入。
‘w|xz’ 打开一个 lzma 压缩的 stream 用于写入。

代码示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import tarfile
import gzip
 
 
# 一次性打包整个根目录。空子目录会被打包。
# 如果只打包不压缩,将"w:gz"参数改为"w:"或"w"即可。
def make_targz(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))
 
 
# 逐个添加文件打包,未打包空子目录。可过滤文件。
# 如果只打包不压缩,将"w:gz"参数改为"w:"或"w"即可。
def make_targz_one_by_one(output_filename, source_dir):
    tar = tarfile.open(output_filename, "w:gz")
    for root, dir, files in os.walk(source_dir):
        for file in files:
            pathfile = os.path.join(root, file)
            tar.add(pathfile)
    tar.close()
 
 
def un_gz(file_name):
    """ungz zip file"""
    f_name = file_name.replace(".gz", "")
    # 获取文件的名称,去掉
    g_file = gzip.GzipFile(file_name)
    # 创建gzip对象
    open(f_name, "wb+").write(g_file.read())
    # gzip对象用read()打开后,写入open()建立的文件里。
    g_file.close()  # 关闭gzip对象
 
 
def un_tar(file_name):
    # untar zip file
    tar = tarfile.open(file_name)
    names = tar.getnames()
    if os.path.isdir(file_name + "_files"):
        pass
    else:
        os.mkdir(file_name + "_files")
    # 由于解压后是许多文件,预先建立同名文件夹
    for name in names:
        tar.extract(name, file_name + "_files/")
    tar.close()
 
 
if __name__ == '__main__':
    make_targz('test.tar.gz', "E:python_samplelibs")
    make_targz_one_by_one('test01.tgz', "E:python_samplelibs")
    un_gz("test.tar.gz")
    un_tar("test.tar")

rar文件

我们可以使用rarfile来解压.rar的文件,但是不支持用rarfile来压缩rar文件。rarfile以下unrar组件,但是使用pip install unrar安装后发现会报如下错误:

Couldn’t find path to unrar library…

这是因为 Python下的 unrar 还依赖了RAR官方的库。

Windows的安装

  • 到RARLab官方下载库文件,然后安装。
  • 安装是最好选择默认路径,一般在 C:Program Files (x86)UnrarDLL 目录下。
  • 添加环境变量,在系统变量中新建变量名UNRAR_LIB_PATH,如果是64位系统,就输入 C:Program Files (x86)UnrarDLLx64UnRAR64.dll,如果是32位系统就是 C:Program Files (x86)UnrarDLLUnRAR.dll。
  • 确定保存环境变量后,在进行 pip install unrar 安装,这时代码再运行就不会报错了。

Linux的安装

  • 下载rar源文件
  • 解压安装包,进入安装包目录,编译安装,生成so 文件
  • 配置环境变量,完成后,在进行 pip install unrar 安装
?
1
2
3
4
5
6
7
8
9
# cd /usr/local/src/
# wget https://www.rarlab.com/rar/unrarsrc-6.0.3.tar.gz
# tar zxvf unrarsrc-6.0.3.tar.gz
# cd unrar
# make lib
# make install-lib  //生成libunrar.so 文件
# vim /etc/profile
export UNRAR_LIB_PATH=/usr/lib/libunrar.so
# source /etc/profile

代码示例:

?
1
2
3
4
5
6
7
import rarfile
 
 
def unrar(rar_file, dir_name):
    # rarfile需要unrar支持, linux下pip install unrar, windows下在winrar文件夹找到unrar,加到path里
    rarobj = rarfile.RarFile(rar_file.decode('utf-8'))
    rarobj.extractall(dir_name.decode('utf-8'))

7z文件

要压缩和解压缩.7z文件需要用到py7zr组件。代码示例:

?
1
2
3
4
5
6
7
8
9
import py7zr
 
# 压缩
with py7zr.SevenZipFile("Archive.7z", 'r') as archive:
    archive.extractall(path="/tmp")
 
# 解压缩
with py7zr.SevenZipFile("Archive.7z", 'w') as archive:
    archive.writeall("target/")

以上就是基于Python实现文件的压缩与解压缩的详细内容,更多关于Python文件压缩的资料请关注服务器之家其它相关文章!

原文链接:https://mp.weixin.qq.com/s/Q3T3S627lWJa_cOSrBocyw

延伸 · 阅读

精彩推荐
  • Python用 python 进行微信好友信息分析

    用 python 进行微信好友信息分析

    这篇文章主要介绍了用 python 进行微信好友信息分析的示例,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下...

    步平凡5272021-11-22
  • Python详解如何为eclipse安装合适版本的python插件pydev

    详解如何为eclipse安装合适版本的python插件pydev

    这篇文章主要介绍了详解如何为eclipse安装合适版本的python插件pydev,pydev是一款优秀的Eclipse插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    赵小生z9462021-04-15
  • Pythonpython3+requests接口自动化session操作方法

    python3+requests接口自动化session操作方法

    今天小编就为大家分享一篇python3+requests接口自动化session操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    jaysonzxk7352021-04-07
  • PythonPython format函数详谈

    Python format函数详谈

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,希望能够给你带来帮助...

    我是NeroZhang10312022-02-19
  • Python一文带你掌握Pyecharts地理数据可视化的方法

    一文带你掌握Pyecharts地理数据可视化的方法

    这篇文章主要介绍了一文带你掌握Pyecharts地理数据可视化的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    叶庭云5752021-09-03
  • Pythonpython 通过 pybind11 使用Eigen加速代码的步骤

    python 通过 pybind11 使用Eigen加速代码的步骤

    这篇文章主要介绍了python 通过 pybind11 使用Eigen加速代码的步骤,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下...

    好问雷7072021-08-10
  • PythonPython合并重叠矩形框

    Python合并重叠矩形框

    这篇文章主要介绍了Python合并重叠矩形框,循环+递归,依次判断两个框是否有重叠,下面具体实现过程需要的小伙伴可以参考一下...

    songxf6042022-11-06
  • Pythonpython聊天室(虽然很简洁,但是可以用)

    python聊天室(虽然很简洁,但是可以用)

    这篇文章主要为大家详细介绍了python实现多人聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    wangziwenhk4732021-12-22