Improve zip command

This commit is contained in:
Oleksii Shevchuk 2017-07-06 16:47:54 +03:00
parent 1f9eb6b819
commit 6c9191bb81
2 changed files with 53 additions and 18 deletions

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from pupylib.PupyModule import *
from pupylib.utils.rpyc_utils import redirected_stdio
from pupylib.utils.rpyc_utils import obtain
__class_name__="Zip"
@ -14,14 +14,37 @@ class Zip(PupyModule):
self.arg_parser = PupyArgumentParser(prog="zip", description=self.__doc__)
self.arg_parser.add_argument('source', type=str, help='path of the source file or directory to zip')
self.arg_parser.add_argument('-l', action='store_true', help='list file (default: zip file)')
self.arg_parser.add_argument('-u', action='store_true', help='unzip file (default: zip file)')
self.arg_parser.add_argument('-d', dest='destination', help='path of the destination file (default: current directory)')
def run(self, args):
with redirected_stdio(self):
# zip
if not args.u:
self.client.conn.modules["pupyutils.zip"].zip(args.source, args.destination)
# unzip
def nice_size(self, value):
if value > 1024*1024*1024:
return '{}G'.format(value/(1024*1024*1024))
elif value > 1024*1024:
return '{}M'.format(value/(1024*1024))
elif value > 1024:
return '{}K'.format(value/1024)
else:
self.client.conn.modules["pupyutils.zip"].unzip(args.source, args.destination)
return '{}B'.format(value)
def run(self, args):
if args.l:
result, data = self.client.conn.modules["pupyutils.zip"].list(args.source)
if result:
data = obtain(data)
log = args.source + ':\n' + '\n'.join(
'{:>8} {}'.format(self.nice_size(file_size), filename) for filename, file_size in data
)
else:
log = data
elif not args.u:
result, log = self.log(self.client.conn.modules["pupyutils.zip"].zip(args.source, args.destination))
else:
result, log = self.log(self.client.conn.modules["pupyutils.zip"].unzip(args.source, args.destination))
if result:
self.success(log)
else:
self.error(log)

View File

@ -17,8 +17,7 @@ def zip(src, dst):
dst = try_unicode(dst)
if not os.path.exists(src):
print "[-] The file \"%s\" does not exists" % src
return
return False, "The file \"%s\" does not exists" % src
isDir = False
if os.path.isdir(src):
@ -33,8 +32,7 @@ def zip(src, dst):
# To not overwrite an existing file
if os.path.exists(dst):
print "[-] The destination file \"%s\" already exists" % dst
return
return False, "The destination file \"%s\" already exists" % dst
# Zip process
zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
@ -48,8 +46,8 @@ def zip(src, dst):
else:
zf.write(src)
print "[+] File zipped correctly: \"%s\"" % dst
zf.close()
return True, "File zipped correctly: \"%s\"" % dst
def unzip(src, dst):
@ -57,8 +55,7 @@ def unzip(src, dst):
dst = try_unicode(dst)
if not os.path.exists(src):
print "[-] The file \"%s\" does not exists" % src
return
return False, "The file \"%s\" does not exists" % src
if not dst:
d = src.split(os.sep)
@ -66,12 +63,27 @@ def unzip(src, dst):
# To not overwrite an existing file
if os.path.exists(dst):
print "[-] The destination file \"%s\" already exists" % dst
return
return False, "The destination file \"%s\" already exists" % dst
if zipfile.is_zipfile(src):
with zipfile.ZipFile(src, "r") as z:
z.extractall(dst)
print "[+] File unzipped correctly: \"%s\"" % dst
return True, "File unzipped correctly: \"%s\"" % dst
else:
print '[-] The zipped file does not have a valid zip format: \"%s\"'
return False, 'The zipped file does not have a valid zip format: \"%s\"' % src
def list(src):
src = try_unicode(src)
if not os.path.exists(src):
return False, "The file \"%s\" does not exists" % src
if zipfile.is_zipfile(src):
with zipfile.ZipFile(src, "r") as z:
return True, [
(
i.filename, i.file_size,
) for i in z.infolist()
]
else:
return False, 'The zipped file does not have a valid zip format: \"%s\"' % src