Python3, 9 lines of code for compression and decompression, bid farewell to compression software.

1 Introduction

Xiao Diaosi : Brother Yu, do you have 7-Zip on your computer?
Xiaoyu : No!
Xiao Diaosi : What about winRAR?
Xiaoyu : No!
Little Diaosi : What about 360?
Xiaoyu : Neither.
Xiao Diaosi : Then how do you decompress the software?
Small fish : zipfile, tarfile and rarfile.
insert image description here
Xiao Diaosi : Brother Yu, then you can share it .
Xiaoyu : Take a tan? ?
Little Diaosi : Well, let's get some sun.

2. Code example

In our daily work, we often need to compress files/installation software.
However, because there are too many kinds of compression and decompression software,
and some decompression software are not regular (you know… ), if you are not careful, you will step on the thunder.
Therefore, for the sake of social harmony, if necessary, you can create a few lines of code yourself to compress and decompress files. Today, we will compress and decompress these three kinds of files:

  • zip file
  • tar.gz file
  • rar file
  • 7z file

2.1 zip file

Speaking of zip file compression and decompression, Xiaoyu introduced the decompression method in detail in the blog post " Python3: 9 Lines of Code Helping Miss Sister to retrieve the password of the compressed package, and Miss Sister's return made me shy~~ ".
The method used here is the ZipFile method.
code example

# -*- coding:utf-8 -*-
# @Time   : 2022-04-05
# @Author : carl_DJ

import  os
import zipfile

'''
压缩文件
'''

def make_zip(source_path,output_filename):
    zip_file = zipfile.ZipFile(output_filename,'w')
    pre_len = len(os.path.dirname(source_path))
    for parent,dirnames,filenames in os.walk(source_path):
        for filename in filenames:
            print(f'{
      
      filename}')
            path_file = os.path.join(parent,filename)
            arcname = path_file[pre_len:].strip(os.path.sep)
            zip_file.write(path_file,arcname)

    zip_file.close()

'''
解压缩文件
'''
def decompression_zip(file_name):
    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"D:\phps_pro",'demo_test.zip')
    decompression_zip("demo_test.zip")
    

2.2 tar.gz file

2.2.1 Basic information

The tarfile module can be used to read and write tar archives, including those compressed with gzip, bz2 and lzma.
When using tarfile it is necessary to understand the mode:

**mode must be a string in the form of 'filemode[:compression]', its default value is 'r',
**, refer to the following table for details:

mode action
‘r’ or ‘r:*’ Open and read use transparent compression (recommended)
‘r:’ Open and read without compression
‘r:gz’ Open and read using gzip compression.
‘r:bz2’ Open and read using bzip2 compression.
‘r:xz’ Open and read use lzma compression.
'x' or 'x:' Create a tarfile without compression. If the file already exists, a FileExistsError exception is thrown.
‘x:gz’ Create a tarfile with gzip compression. If the file already exists, a FileExistsError exception is thrown.
‘x:bz2’ Create a tarfile with bzip2 compression. If the file already exists, a FileExistsError exception is thrown.
‘x:xz’ Create a tarfile with lzma compression. If the file already exists, a FileExistsError exception is thrown.
‘a’ or ‘a:’ Open for appending without compression. Create the file if it does not exist.
‘w’ or ‘w:’ Open for uncompressed writing.
‘w:gz’ Open for writing with gzip compression.
‘w:bz2’ Open for writing with bzip2 compression.
‘w:xz’ Turn on writing for lzma compression

For special purposes, there is also a second mode format: ' filemode|[compression] '.
tarfile.open() will return a TarFile object that handles its data as a stream of data blocks:

mode action
'rI*' Opens a stream of tar blocks for transparently compressed reading.
'rI' Opens a stream of uncompressed tar blocks for reading.
'rIgz' Opens a gzip compressed stream for reading.
‘rIbz2’ Opens a bzip2 compressed stream for reading.
‘rIxz’ Opens an lzma compressed stream for reading.
'wI' Opens an uncompressed stream for writing.
'wIgz' Opens a gzip-compressed stream for writing.
‘wIbz2’ Opens a bzip2 compressed stream for writing.
'wIxz' Opens an lzma-compressed stream for writing.

2.2.2 Code Examples

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-04-05
# @Author : carl_DJ


import os
import tarfile
import gzip

'''
tar.gz文件  压缩和解压缩
'''

# 一次性打包整个根目录。空子目录会被打包。
# 如果只打包不压缩,将"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()

#解压缩gz文件
def decompression_gz(file_name):
    
    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 decompression_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', "D:")
    make_targz_one_by_one('test01.tgz', "D:\phps_pro")
    decompression_tar("demo_test.tar")
    decompression_gz("demo_test.tar.gz")
    

2.3 rar file

2.3.1 Installation

We can use rarfile to decompress .rar files,
however , using rarfile to compress rar files is not supported.
Knock on the blackboard,
because unrar under Python also depends on the official RAR library, so we can't do it directly pip install unrar, otherwise an error will be reported.

We need to install it separately, for example, Windows:

  • Go to RARLab official download library file, https://www.rarlab.com/rar/UnRARDLL.exe, and then install it.
  • It is best to choose the default path for installation, generally in the C:Program Files (x86)UnrarDLL directory.
  • Add environment variables and create a new variable name UNRAR_LIB_PATH in the system variables. If it is a 64-bit system, enter C:Program Files (x86)UnrarDLLx64UnRAR64.dll, and if it is a 32-bit system, it is C:Program Files (x86)UnrarDLLUnRAR.dll.
  • After confirming that the environment variables are saved, after pip install unrar installation, the code will not report an error when running again.

2.3.2 Code Examples

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-04-05
# @Author : carl_DJ

import rarfile
#解压缩rar
def  decompression_rar(rar_file, dir_name):
	#rarfile需要unrar支持,windows下在winrar文件夹找到unrar,加到path里
	rarobj = rarfile.RarFile(rar_file.decode('utf-8'))
    rarobj.extractall(dir_name.decode('utf-8'))
    

2.4 7z files

The py7zr component is required to compress and decompress .7z files.

code example

# -*- coding:utf-8 -*-
# @Time   : 2022-04-05
# @Author : carl_DJ

import py7zr

'''压缩7z'''
with py7zr.SevenZipFile("Archive.7z", 'r') as archive:
    archive.extractall(path="/tmp")

'''解压缩7z'''
with py7zr.SevenZipFile("Archive.7z", 'w') as archive:
    archive.writeall("target/")
    

3. Summary

Today's sharing is here.
Strange knowledge has been added again.
In the future, there is no need to download third-party compression software, and
it is OK to execute a code directly.

Follow Xiaoyu to learn more about python third-party libraries.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/123956825