Extract python tar.gz, tar, zip, rar files

For these general methods of decompression, there are a lot of introductions on the Internet. Here, I will not repeat them, but go to the main topic directly.

tar.gz decompress

import gzip
def un_gz(file_name):
    """decompression gz package"""
    f_name = file_name.replace(".gz", "")
    #Get the name of the file, remove
    g_file = gzip.GzipFile(file_name)
    #Create gzip object
    f =open(f_name, "w+").write(g_file.read())
    #After the gzip object is opened with read(), it is written into the file created by open().
    g_file.close()

tar decompression

import tarfile
def un_tar(file_name):
    """decompression tar"""
    print(file_name)
    tar = tarfile.open(file_name)
    names = tar.getnames()
    temp_file_path = ''
    if os.path.isdir(file_name + "_files"):
        print('file already exist')
        temp_file_path = os.path.isdir(file_name + "_files")
    else:
        temp_file_path = os.mkdir(file_name + "_files")
        print('Create a new filename')
    #Because there are many files after decompression, a directory with the same name should be established in advance
    for name in names:
        tar.extract(name, file_name + "_files/")
    tar.close()
    return temp_file_path

zip unzip

import zipfile
def un_zip(file_name):
    """decompression zip"""
    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()

rar decompression

import rarfile
import os
def un_rar(file_name):
    """unrar zip file"""
    rar = rarfile.RarFile(file_name)
    if os.path.isdir(file_name + "_files"):
        pass
    else:
        os.mkdir(file_name + "_files")
    if os.chdir(file_name + "_files"):
        rar.extractall()
        rar.close()

The above is the method of decompression. The following will enter the instance

# Compressed package file path
rar_file_path = "/Users/mac/Desktop/Login log/finally/"
for dirpath, dirnames, filenames in os.walk(rar_file_path):
    for filepath in filenames:
        gz_file_path = os.path.join(dirpath, filepath)
        # Tar GZ helper is the encapsulation class name of decompression method, which is convenient to call
        tar_gz_helper.un_tar(gz_file_path)
        

After decompressing the compressed package according to the above method, there is no problem, but sometimes our demand is not just for decompressing, but for decompressing the contents of the files in the subdirectory after the files are decompressed. At this time, we need to traverse again, and the Mac will generate a. DS_Store file by default after decompressing the files. If you still use my method to traverse again, you will If there is a problem, you need to judge that the suffix of the path is DS_Store, and pass it directly to avoid the error of handling the DS_Store file.

        if gz_file_path.endswith('DS_Store'):
            pass
        if gz_file_path.endswith('.tar.gz'):
            # Read the file path and start decompression
            tar_gz_helper.un_tar(gz_file_path)

It's a long way. No one drives you, but you have to go.

 


 

Keywords: Mac

Added by mherr170 on Mon, 23 Dec 2019 20:22:19 +0200