compress.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import tarfile
  16. def compress_file_tar(file_path, output_filename):
  17. # 确保输出文件名以 .tar.gz 结尾
  18. if not output_filename.endswith('.tar.gz'):
  19. output_filename += '.tar.gz'
  20. # 创建一个 tarfile 对象,使用 'w:gz' 模式表示写入 gzip 压缩的 tar 包
  21. with tarfile.open(output_filename, "w:gz") as tar:
  22. # 将文件添加到 tar 包中,arcname 指定在 tar 包中的相对路径
  23. tar.add(file_path, arcname=os.path.relpath(file_path))
  24. def compress_tar(folder_path, output_filename):
  25. # 确保输出文件名以 .tar.gz 结尾
  26. if not output_filename.endswith('.tar.gz'):
  27. output_filename += '.tar.gz'
  28. # 创建一个 tarfile 对象,使用 'w:gz' 模式表示写入 gzip 压缩的 tar 包
  29. with tarfile.open(output_filename, "w:gz") as tar:
  30. # os.walk() 遍历目录
  31. for root, dirs, files in os.walk(folder_path):
  32. for file in files:
  33. # 构建完整的文件路径
  34. file_path = os.path.join(root, file)
  35. # 将文件添加到 tar 包中,arcname 指定在 tar 包中的相对路径
  36. tar.add(file_path, arcname=os.path.relpath(file_path, start=folder_path))
  37. def uncompress_tar(compressed_filename, output_folder):
  38. """
  39. 解压 .tar.gz 文件到指定的输出文件夹。
  40. Args:
  41. compressed_filename (str): 要解压的 .tar.gz 文件的路径。
  42. output_folder (str): 解压文件的目标文件夹路径。
  43. """
  44. # 确保输出文件夹存在
  45. if not os.path.exists(output_folder):
  46. os.makedirs(output_folder)
  47. # 打开 .tar.gz 文件进行解压缩
  48. with tarfile.open(compressed_filename, "r:gz") as tar:
  49. # 解压 tar 包到指定的输出文件夹
  50. tar.extractall(path=output_folder)
  51. if __name__ == "__main__":
  52. folder_path="/Users/dingyunpeng/tardemo"
  53. output_filename="/Users/dingyunpeng/tardemo.tar.gz"
  54. compressed_filename=output_filename
  55. output_folder=folder_path + "2"
  56. compress_tar(folder_path, output_filename)
  57. uncompress_tar(output_filename, output_folder)