Home > Backend Development > Python Tutorial > How to make a file decompression tool based on Python

How to make a file decompression tool based on Python

PHPz
Release: 2023-05-13 13:43:21
forward
1775 people have browsed it

Often due to the different compression formats, when decompressing files, you need to download different decompression tools to process different files, so that there are three or four types of compression tools on the desktop, so I use python to do it. A small tool for decompressing files in various common formats.

How to make a file decompression tool based on Python

The common compression formats are mainly the following four formats:

Compressed files in zip format are generally decompressed using 360 compression software.

Compressed files in tar.gz format are generally decompressed using the tar command on Linux systems.

Compressed files in rar format are generally decompressed using rar compression software.

Compressed files in 7z format are generally decompressed using 7-zip compression software.

Import a non-standard library for decompression processing in zip format.

import os
import zipfile as zip
Copy after login

Write a file compression function in zip decompression format.

def do_zip(source_, target_file):
    '''
    zip文件压缩
    :param source_: 原始文件路径
    :param target_file: 目标文件路径
    :return:
    '''
    zip_file = zip.ZipFile(target_file, 'w')
    pre_len = len(os.path.dirname(source_))
    for parent, dirnames, filenames in os.walk(source_):
        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()
Copy after login

Write a file decompression function in zip decompression format.

def un_zip(source_file, target_):
    '''
    zip文件解压缩
    :param source_file: 原始文件路径
    :param target_: 目标文件路径
    :return:
    '''
    zip_file = zip.ZipFile(source_file)
    if os.path.isdir(target_):
        pass
    else:
        os.mkdir(target_)
    for names in zip_file.namelist():
        zip_file.extract(names, target_)
    zip_file.close()
Copy after login

Import the non-standard library for decompression processing in 7z format.

import py7zr
Copy after login

Write a file compression function in 7z decompression format.

def do_7z(source_, target_file):
    '''
    7z文件压缩
    :param source_:
    :param target_file:
    :return:
    '''
    with py7zr.SevenZipFile(target_file, 'r') as file:
        file.extractall(path=source_)
Copy after login

Write a file decompression function in 7z decompression format.

def un_7z(source_file, target_):
    '''
    7z文件解压缩
    :param source_file:
    :param target_:
    :return:
    '''
    with py7zr.SevenZipFile(source_file, 'w') as file:
        file.writeall(target_)
Copy after login

Import a non-standard library for decompression processing in rar format.

import rarfile as rar
Copy after login

Write a file decompression function in rar decompression format.

def un_rar(source_file, target_):
    '''
    rar文件解压缩
    :param source_file: 原始文件
    :param target_: 目标文件路径
    :return:
    '''
    obj_ = rar.RarFile(source_file.decode('utf-8'))
    obj_.extractall(target_.decode('utf-8'))
Copy after login

Let’s get to the point. First, use the print function to print the menu options, which allows the user to make menu selections after starting the software.

print('==========PYTHON工具:文件解压缩软件==========')
print('说明:目前支持zip、7z、rar格式')
print('1、文件解压缩格式:zip/rar/7z')
print('2、文件操作类型(压缩/解压):Y/N')
print('3、文件路径选择,需要输入相应的操作文件路径')
print('==========PYTHON工具:文件解压缩软件==========')
Copy after login

Use the input function to receive the file decompression format input by the user.

format_ = input('请输入文件解压缩的格式:\n')
Copy after login

Use the input function to receive the file operation type (compression/decompression) input by the user.

type_ = input('请输入文件操作的类型:\n')
Copy after login

Use the input function to receive the file path that needs to be operated input by the user.

source_ = input('请输入原始文件的存储路径(文件或目录):\n')
Copy after login

Use the input function to receive the target path of the generated new file input by the user.

target_ = input('请输入目标文件的存储路径(文件或目录):\n')
Copy after login

In order to maintain the flexibility of input, add business judgments in different formats and different operation types.

if format_ == 'zip' and type_ == 'Y':
    do_zip(source_, target_)
elif format_ == 'zip' and type_ == 'N':
    un_zip(source_, target_)
elif format_ == 'rar' and type_ == 'Y':
    un_zip(source_, target_)
elif format_ == 'rar' and type_ == 'N':
    un_zip(source_, target_)
elif format_ == '7z' and type_ == 'Y':
    un_zip(source_, target_)
elif format_ == '7z' and type_ == 'N':
    un_zip(source_, target_)
Copy after login

Currently, the function points are made in three formats. If necessary later, the current version may be expanded and upgraded.

The above is the detailed content of How to make a file decompression tool based on Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template