Python3 Compression and Decompression Implementation Code

  • 2021-09-11 20:51:46
  • OfStack

1. Description

Compression and decompression is a common operation in daily life. Whether it is the operation of graphical interface on windows or the compression and decompression with commands on linux, it is generally convenient.

But with the code to achieve has not done, recently also have to achieve code compression and decompression operations, so take the time to study 1.

2. Implementation of zip file compression and decompression


import os
import zipfile

#  The function function is zip_file_list All files, and zip_dir_list All files in all directories are compressed to 1 A zip_file_name In the compressed file of 
def my_zip_function(zip_file_name, zip_file_list=[], zip_dir_list=[]):
  #  The final requirement for compressed files close For our convenience, we use it directly with
  with zipfile.ZipFile(zip_file_name, "w") as zip_obj:
    #  Zip file 
    for tmp_file in zip_file_list:
      zip_obj.write(tmp_file)
    #  Compressed directory 
    for tmp_dir in zip_dir_list:
      # zipfile There is no function of directly compressing directories. To compress directories, you can only traverse directories 1 A 1 File pressure. 
      for root, dirs, files in os.walk(tmp_dir):
        #  If you want the directory to be compressed when it is empty, the directory should also be compressed 1 Throughout; On the contrary, please comment out the following lines 
        zip_obj.write(root)
        for tmp_file in files:
          #  Splice the complete directory of the file, otherwise you can't find the file only by the file name code 
          tmp_file_path = os.path.join(root, tmp_file)
          zip_obj.write(tmp_file_path)


#  The function is used to traverse all files in a compressed file 
def my_traversal_zip_function(zip_file_name):
  with zipfile.ZipFile(zip_file_name, "r") as zip_obj:
    #  The result returned is 1 A ZipInfo List 
    #  If the compressed directory is displayed at compression time, the directory is also used as 1 A single ZipInfo Present in the list; On the contrary, there is no directory ZipInfo
    all_file_list = zip_obj.infolist()
    for tmp_file in all_file_list:
      print(tmp_file.filename)
      #  You can also read the contents of the file directly without extracting it 
      #  It can be passed through ZipInfo.is_dir() To distinguish between files and directories 
      # if not tmp_file.is_dir():
      #   with zip_obj.open(tmp_file) as zip_fd:
      #     print(zip_fd.read())

#  The compressed file () function extracts the compressed file directly 
def my_unzip_function(zip_file_name, path="."):
  with zipfile.ZipFile(zip_file_name, "r") as zip_obj:
    zip_obj.extractall(path=path)

if __name__ == "__main__":
  zip_file_name = "test_zip.zip"
  #  When testing, you should first create files and directories to be compressed by yourself 
  zip_file_list = ["test_tar_file1.txt", "test_tar_file2.txt"]
  zip_dir_list = ["test_tar_dir"]
  my_zip_function(zip_file_name, zip_file_list, zip_dir_list)
  my_traversal_zip_function(zip_file_name)
  # my_unzip_function(zip_file_name, path=".")

3. Realization of tar file compression and decompression

In addition to the straightforward.tar file, the .tar.gz/.tar.bz2/.tar.xz Realization of compression and decompression of equal format files.


import os
import tarfile

# Python Learning and communication group: 778463939
#  The function function is tar_file_list All files, and tar_dir_list All files in all directories are compressed to 1 A tar_file_name In the compressed file of 
def my_tar_function(tar_file_name, tar_file_list=[], tar_dir_list=[], model="w"):
  #  It should have been tarfile.TarFile(tar_file_name, model) To create, but TarFile Not supported "r:gz" Equal extension form 
  #  The final requirement for compressed files close For our convenience, we use it directly with
  with tarfile.open(tar_file_name, model) as tar_obj:
    #  Zip file 
    for tmp_file in tar_file_list:
      tar_obj.add(tmp_file)
    #  Compress the directory. And zipfile Compared to tarfile Allows direct compression of directories without traversing directories 1 Individual file pressure 
    for tmp_dir in tar_dir_list:
      tar_obj.add(tmp_dir)


#  The function is used to traverse all files in a compressed file 
def my_traversal_tar_function(tar_file_name, model="r"):
  with tarfile.open(tar_file_name, model) as tar_obj:
    #  The result returned is 1 A TarInfo List 
    all_file_list = tar_obj.getmembers()
    for tmp_file in all_file_list:
      print(tmp_file.name)
      #  You can also read the contents of the file directly without extracting it 
      #  It can be passed through TarInfo.isdir() To distinguish between files and directories 
      # if not tmp_file.isdir():
      #   #  Equivalent to zip Adj. open Does not unzip the file 
      #   tar_fd = tar_obj.extractfile(tmp_file)
      #   print(tar_fd.read())


#  The compressed file () function extracts the compressed file directly 
def my_untar_function(tar_file_name, path=".", model="r"):
  with tarfile.open(tar_file_name, model) as tar_obj:
    tar_obj.extractall(path=path)


if __name__ == "__main__":
  #  When testing, you should first create files and directories to be compressed by yourself 
  tar_file_list = ["test_tar_file1.txt", "test_tar_file2.txt"]
  tar_dir_list = ["test_tar_dir"]
  tar_file_name = "test_tar.tar"
  #  In .tar On that basi, tarfile Also supports gz/bz2/xz As long as the compression is used on the basis of the original open mode : Or | Connect the compression method, such as "r:gz"
  #  In particular, if you are reading files, you can use "r:*" To indicate an attempt to read in any format 
  open_model = "w"
  # open_model = "w:gz"
  my_tar_function(tar_file_name, tar_file_list, tar_dir_list, model=open_model)
  open_model = "r"
  # open_model = "r:*"
  my_traversal_tar_function(tar_file_name, model=open_model)
  # open_model = "r:*"
  # my_untar_function(tar_file_name, path=".", model=open_model)

Related articles: