done
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) Shuge Lee <
shug...@gmail.com> 2009
# 2009-05-07
import os
from os import path as osp
import zipfile, tarfile
#=======================================================================
# guess platform
import platform
p = platform.platform(terse=1).lower()
if p.find('linux') != -1:
lee_platform = 'linux'
lee_writelines = '\n'
lee_output_encode = 'utf8'
elif p.find('window') != -1:
lee_platform = 'windows'
lee_writelines = '\r\n'
havecolor = 0
lee_output_encode = 'gbk'
elif p.find('cygwin') != -1:
lee_platform = 'cygwin'
lee_writelines = '\r\n'
lee_output_encode = 'gbk'
else:
problem = _("We are sorry for that Lee System doesn't supports
your platform - %s now" % p)
solution = _('Report to Shuge developer')
std(problem, solution, 'quit')
#=======================================================================
def unpack(packed_file, save_path):
if not packed_file:
print '%s is None' % packed_file
return -1
if not save_path:
print '%s is None' % save_path
return -1
else:
save_path = osp.realpath(save_path)
if not osp.exists(save_path):
print '%s created' % save_path
os.makedirs(save_path)
# guess file type
file_type = osp.splitext(packed_file)[-1]
if file_type not in ['.7z', '.bz2', '.gz', '.zip', '.rar']:
print 'Unpack file format - %s not recognized. Ignoring.' %
packed_file
return -1
e = 0
# extract file
"""
0 No error
1 Warning (Non fatal error(s)). For example, one or more files
were
locked by some other application, so they were not compressed.
2 Fatal error
7 Command line error
8 Not enough memory for operation
255 User stopped the process
"""
if file_type in ['.7z', '.rar']:
if lee_platform == 'windows':
extract_app = '7z.exe'
elif lee_platform in ['linux', 'cygwin']:
extract_app = '7z'
else:
print "It doesn't support your platform"
sys.exit(-1)
extract_cmd = ' %s x "%s" -o"%s" ' % (extract_app,
packed_file, save_path)
e = os.system(extract_cmd)
#print "We're sorry that Lee System doesn't support RAR/7z
archive"
return e
elif file_type == '.zip':
try:
# for python 2.5
target_file = zipfile.ZipFile(packed_file)
for vary_file in target_file.namelist():
vary_file_name = osp.join(save_path, vary_file)
if vary_file_name.endswith('/'):
if not osp.exists(vary_file):
os.mkdir(vary_file)
else:
parent_folder = osp.dirname(vary_file_name)
if not osp.exists(parent_folder):
os.makedirs(parent_folder)
outfile = open(vary_file_name, 'wb')
outfile.write(target_file.read(vary_file))
outfile.close()
# # for python 2.6
# target_file = zipfile.open(packed_file)
# target_file.extractall()
# target_file.close()
except zipfile.BadZipfile, e:
return e
elif file_type in ['.bz2', '.gz']:
try:
target_file = tarfile.open(packed_file)
target_file.extractall(path=save_path)
target_file.close()
except tarfile.ReadError, e:
return e
return e
if __name__ == '__main__':
import sys
if len(sys.argv) >= 2:
packed_file = sys.argv[1]
if len(sys.argv) > 2:
save_path = sys.argv[2]
else:
save_path = osp.dirname(osp.realpath(__file__))
unpack(packed_file, save_path)
On May 7, 1:49 pm, Jiahua Huang <
jhuangjia...@gmail.com> wrote:
> 2009/5/7 Shuge Lee <
shuge....@gmail.com>: