-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathzip.py
More file actions
111 lines (97 loc) · 3.97 KB
/
zip.py
File metadata and controls
111 lines (97 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from .. import loader, utils
import os
import urllib.parse
from uuid import uuid4
ztd = 'zip-temp-dir'
@loader.tds
class ZipMod(loader.Module):
'''Запаковывает/распаковывает файлы'''
strings = {'name': 'ZIP'}
@loader.unrestricted
async def zipaddcmd(self, message):
""".zipadd <file/reply to file> - сохраняет файл во временную папку"""
reply = await message.get_reply_message()
event = reply or message
if not event.file:
await message.edit('<b>[ZIP]Добавить что?<b>')
return
if not os.path.exists(ztd):
os.mkdir(ztd)
fn = _fn = event.file.name
if not fn:
date = event.date
kind = event.file.mime_type.split('/')[0]
ext = event.file.ext
fn = _fn = '{}_{}-{:02}-{:02}_{:02}-{:02}-{:02}{}'.format(kind, date.year, date.month, date.day, date.hour, date.minute, date.second, ext)
files = os.listdir(ztd)
copy = 1
while fn in files:
fn = f"({copy}).{_fn}"
copy += 1
await message.edit(f'<b>[ZIP]Загружаю файл \'</b><code>{fn}</code>\'...')
await event.download_media(f'{ztd}/{fn}')
await message.edit(f"<b>[ZIP]Файл \"</b><code>{fn}</code><b>\" загружен!</b>")
@loader.unrestricted
async def ziplistcmd(self, message):
"""список сохраненных файлов"""
if not os.path.exists(ztd):
await message.edit('<b>[ZIP]В папке пусто!</b>')
return
files = os.listdir(ztd)
files = '\n'.join([f'<a href="tg://msg?text=.zipshow+{urllib.parse.quote(fn)}">{num+1})</a> <code>{fn}</code>' for num, fn in enumerate(files)])
await message.edit('<b>[ZIP]Список файлов:</b>\n'+files)
@loader.unrestricted
async def zipshowcmd(self, message):
""".zipshow <name> - показывает сохранённый файл"""
if not os.path.exists(ztd):
await message.edit('<b>[ZIP]В папке пусто!</b>')
return
files = os.listdir(ztd)
file = utils.get_args_raw(message)
if not file:
await message.edit('<b>[ZIP]Пустой запрос!</b>')
return
if file not in files:
await message.edit('<b>[ZIP]Такого файла нет!</b>')
return
await message.edit(f"<b>[ZIP]Отправляю \"</b><code>{file}</code><b>\"...")
await message.respond(file=ztd+"/"+file)
await message.delete()
@loader.unrestricted
async def zipdelcmd(self, message):
""".zipdel <name> - удаляет сохранённый файл"""
file = utils.get_args_raw(message)
try:
os.remove(ztd+"/"+file)
except FileNotFoundError:
await message.edit("<b>[ZIP]Такого файла нет!</b>")
return
await message.edit(f"<b>[ZIP]Файл \"</b><code>{file}</code><b>\" удалён!</b>")
@loader.unrestricted
async def zipcmd(self, message):
""".zip <name> (-s) - пакует в архив name. если есть флаг -s то сохраняет папку с фацлами"""
if not os.path.exists(ztd):
await message.edit("<b>[ZIP]Файлов для запаковки не найдено!</b>")
return
name = utils.get_args_raw(message)
save = False
if "-s" in name:
save = True
name = name.replace("-s","").strip()
if not name:
name = str(uuid4()).split("-")[-1]+".zip"
name = name + (".zip" if ".zip" not in name else "")
await message.edit(f'<b>[ZIP]Запаковываю {len(os.listdir(ztd))} файл(ов) в </b>"<code>{name}</code>"')
os.system(f"zip {name} {ztd}/*")
await message.edit(f'<b>[ZIP]Отправляю </b>"<code>{name}</code>"')
await message.respond(file=open(name, "rb"))
await message.delete()
os.system("rm -rf {name}")
if not save:
os.system("rm -rf zip-temp-dir")
@loader.unrestricted
async def zipcleancmd(self, message):
""".zipclear - очищает папку с файлами"""
os.system("rm -rf zip-temp-dir")
await message.edit('<b>[ZIP]Очищено!</b>')
os.mkdir(ztd)