python开发_tarfile_文档归档压缩|解压缩

'''
    python中的tarfile模块实现文档的归档压缩和解压缩
    
    功能:
        把工作空间下面的所有文件,打包生成一个tar文件
        同时提供一个方法把该tar文件中的一些文件解压缩到
        指定的目录中
'''

运行效果:

如果在你计算机的C盘里面没有test目录,系统会自动创建该目录:c:\\test

如果在你计算机的c:\\test目录下面没有temp目录,系统会自动创建该目录:c:\\test\\temp

控制台输出:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
创建目录:[c:\test]
创建目录:[c:\test\temp]
获取工作空间下的所有文件...
打开文件:[c:\test\hongten.tar]
压缩文件:[fibo.py]
压缩文件:[main.py]
压缩文件:[pythonindex.html]
压缩文件:[test_arrange.py]
压缩文件:[test_array.py]
压缩文件:[test_blogs.py]
压缩文件:[test_calendar.py]
压缩文件:[test_calendar_html.py]
压缩文件:[test_class.py]
压缩文件:[test_collections]
压缩文件:[test_collections.py]
压缩文件:[test_copy.py]
压缩文件:[test_count.py]
压缩文件:[test_email.py]
压缩文件:[test_email_send.py]
压缩文件:[test_filecmp.py]
压缩文件:[test_for.py]
压缩文件:[test_function.py]
压缩文件:[test_glob.py]
压缩文件:[test_gzip.py]
压缩文件:[test_linecache.py]
压缩文件:[test_list.py]
压缩文件:[test_modules.py]
压缩文件:[test_more_mudels.py]
压缩文件:[test_mysql.py]
压缩文件:[test_n.py]
压缩文件:[test_os.py]
压缩文件:[test_pprint.py]
压缩文件:[test_qq.py]
压缩文件:[test_shutil.py]
压缩文件:[test_stat.py]
压缩文件:[test_tar.py]
压缩文件:[test_tempfile.py]
压缩文件:[test_tkinter.py]
压缩文件:[test_truth_value.py]
压缩文件:[test_wifi.py]
压缩文件:[test_windows.py]
压缩文件:[__pycache__]
关闭文件[c:\test\hongten.tar]
##################################################
打开文件:[c:\test\hongten.tar]
获取到所有文件名称:['fibo.py', 'main.py', 'pythonindex.html', 'test_arrange.py', 'test_array.py', 'test_blogs.py', 'test_calendar.py', 'test_calendar_html.py', 'test_class.py', 'test_collections', 'test_collections.py', 'test_copy.py', 'test_count.py', 'test_email.py', 'test_email_send.py', 'test_filecmp.py', 'test_for.py', 'test_function.py', 'test_glob.py', 'test_gzip.py', 'test_linecache.py', 'test_list.py', 'test_modules.py', 'test_more_mudels.py', 'test_mysql.py', 'test_n.py', 'test_os.py', 'test_pprint.py', 'test_qq.py', 'test_shutil.py', 'test_stat.py', 'test_tar.py', 'test_tempfile.py', 'test_tkinter.py', 'test_truth_value.py', 'test_wifi.py', 'test_windows.py', '__pycache__', '__pycache__/fibo.cpython-33.pyc']
提取文件:[pythonindex.html]
>>> 

=================================================

代码部分:

=================================================

 1 #python tar
 2 
 3 #Author : Hongten
 4 #MailTo : hongtenzone@foxmail.com
 5 #QQ     : 648719819
 6 #Blog   : http://www.cnblogs.com/hongten
 7 #Create : 2013-08-19
 8 #Version: 1.0
 9     
10 import os
11 import tarfile
12 '''
13     python中的tarfile模块实现文档的归档压缩和解压缩
14     
15     功能:
16         把工作空间下面的所有文件,打包生成一个tar文件
17         同时提供一个方法把该tar文件中的一些文件解压缩到
18         指定的目录中
19 '''
20 #global var
21 SHOW_LOG = True
22 #tar文件存放位置
23 TAR_PATH = ''
24 #取出文件存放目录
25 EXT_PATH = ''
26 
27 def write_tar_file(path, content):
28     '''打开指定path的tar格式的文件,如果该文件不存在
29     系统会自动创建该文件,如果该文件以及存在,则打开文件
30     打开文件后,向文件中添加文件(这个功能类似于把几个文件
31     打包成tar包文件)'''
32     with tarfile.open(path, 'w') as tar:
33         if SHOW_LOG:
34             print('打开文件:[{}]'.format(path))
35         for n in content:
36             if SHOW_LOG:
37                 print('压缩文件:[{}]'.format(n))
38             tar.add(n)
39         if SHOW_LOG:
40             print('关闭文件[{}]'.format(path))
41         tar.close()
42         
43 def get_workspace_files():
44     '''获取工作空间下面的所有文件,然后以列表的形式返回'''
45     if  SHOW_LOG:
46         print('获取工作空间下的所有文件...')
47     return os.listdir('./')
48 
49 def extract_files(tar_path, ext_path, ext_name):
50     '''解压tar文件中的部分文件到指定目录中'''
51     with tarfile.open(tar_path) as tar:
52         if SHOW_LOG:
53             print('打开文件:[{}]'.format(tar_path))
54         names = tar.getnames()
55         if SHOW_LOG:
56             print('获取到所有文件名称:{}'.format(names))
57         for name in names:
58             if name.split('.')[-1] == ext_name:
59                 if SHOW_LOG:
60                     print('提取文件:[{}]'.format(name))
61                 tar.extract(name, path = ext_path)
62 
63 def mkdir(path):
64     '''创建不存在的目录'''
65     if os.path.exists(path):
66         if SHOW_LOG:
67             print('存在目录:[{}]'.format(path))
68     else:
69         if SHOW_LOG:
70             print('创建目录:[{}]'.format(path))
71         os.mkdir(path)
72 
73 def init():
74     global SHOW_LOG
75     SHOW_LOG = True
76     #tar文件存放位置
77     global TAR_PATH
78     TAR_PATH = 'c:\\test\\hongten.tar'
79     #取出文件存放目录
80     global EXT_PATH
81     EXT_PATH = 'c:\\test\\temp'
82     #创建目录,如果目录不存在
83     path = os.path.split(TAR_PATH)[0]
84     mkdir(path)
85     mkdir(EXT_PATH)
86     
87 def main():
88     init()
89     content = get_workspace_files()
90     #打包文件
91     write_tar_file(TAR_PATH, content)
92     print('#' * 50)
93     #提取文件
94     extract_files(TAR_PATH, EXT_PATH, 'html')
95         
96 
97 if __name__ == '__main__':
98     main()

 

posted @ 2013-08-19 15:55  Hongten  阅读(4956)  评论(0编辑  收藏  举报
Fork me on GitHub