mirror of https://github.com/n1nj4sec/pupy.git
Improve zip command
This commit is contained in:
parent
1f9eb6b819
commit
6c9191bb81
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from pupylib.PupyModule import *
|
from pupylib.PupyModule import *
|
||||||
from pupylib.utils.rpyc_utils import redirected_stdio
|
from pupylib.utils.rpyc_utils import obtain
|
||||||
|
|
||||||
__class_name__="Zip"
|
__class_name__="Zip"
|
||||||
|
|
||||||
|
@ -14,14 +14,37 @@ class Zip(PupyModule):
|
||||||
self.arg_parser = PupyArgumentParser(prog="zip", description=self.__doc__)
|
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('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('-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)')
|
self.arg_parser.add_argument('-d', dest='destination', help='path of the destination file (default: current directory)')
|
||||||
|
|
||||||
def run(self, args):
|
def nice_size(self, value):
|
||||||
with redirected_stdio(self):
|
if value > 1024*1024*1024:
|
||||||
# zip
|
return '{}G'.format(value/(1024*1024*1024))
|
||||||
if not args.u:
|
elif value > 1024*1024:
|
||||||
self.client.conn.modules["pupyutils.zip"].zip(args.source, args.destination)
|
return '{}M'.format(value/(1024*1024))
|
||||||
# unzip
|
elif value > 1024:
|
||||||
|
return '{}K'.format(value/1024)
|
||||||
else:
|
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)
|
||||||
|
|
|
@ -17,8 +17,7 @@ def zip(src, dst):
|
||||||
dst = try_unicode(dst)
|
dst = try_unicode(dst)
|
||||||
|
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
print "[-] The file \"%s\" does not exists" % src
|
return False, "The file \"%s\" does not exists" % src
|
||||||
return
|
|
||||||
|
|
||||||
isDir = False
|
isDir = False
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
|
@ -33,8 +32,7 @@ def zip(src, dst):
|
||||||
|
|
||||||
# To not overwrite an existing file
|
# To not overwrite an existing file
|
||||||
if os.path.exists(dst):
|
if os.path.exists(dst):
|
||||||
print "[-] The destination file \"%s\" already exists" % dst
|
return False, "The destination file \"%s\" already exists" % dst
|
||||||
return
|
|
||||||
|
|
||||||
# Zip process
|
# Zip process
|
||||||
zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
|
zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
|
||||||
|
@ -48,8 +46,8 @@ def zip(src, dst):
|
||||||
else:
|
else:
|
||||||
zf.write(src)
|
zf.write(src)
|
||||||
|
|
||||||
print "[+] File zipped correctly: \"%s\"" % dst
|
|
||||||
zf.close()
|
zf.close()
|
||||||
|
return True, "File zipped correctly: \"%s\"" % dst
|
||||||
|
|
||||||
|
|
||||||
def unzip(src, dst):
|
def unzip(src, dst):
|
||||||
|
@ -57,8 +55,7 @@ def unzip(src, dst):
|
||||||
dst = try_unicode(dst)
|
dst = try_unicode(dst)
|
||||||
|
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
print "[-] The file \"%s\" does not exists" % src
|
return False, "The file \"%s\" does not exists" % src
|
||||||
return
|
|
||||||
|
|
||||||
if not dst:
|
if not dst:
|
||||||
d = src.split(os.sep)
|
d = src.split(os.sep)
|
||||||
|
@ -66,12 +63,27 @@ def unzip(src, dst):
|
||||||
|
|
||||||
# To not overwrite an existing file
|
# To not overwrite an existing file
|
||||||
if os.path.exists(dst):
|
if os.path.exists(dst):
|
||||||
print "[-] The destination file \"%s\" already exists" % dst
|
return False, "The destination file \"%s\" already exists" % dst
|
||||||
return
|
|
||||||
|
|
||||||
if zipfile.is_zipfile(src):
|
if zipfile.is_zipfile(src):
|
||||||
with zipfile.ZipFile(src, "r") as z:
|
with zipfile.ZipFile(src, "r") as z:
|
||||||
z.extractall(dst)
|
z.extractall(dst)
|
||||||
print "[+] File unzipped correctly: \"%s\"" % dst
|
return True, "File unzipped correctly: \"%s\"" % dst
|
||||||
else:
|
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
|
||||||
|
|
Loading…
Reference in New Issue