-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.py
36 lines (27 loc) · 1012 Bytes
/
util.py
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
import zipfile
import py7zr
from logger import getLogger
l = getLogger("main")
max_uncompressed_size = 1000 * 1000 * 1000
def get_archive_filenames(path: str) -> list[str]:
if path.endswith(".7z"):
l.debug(f"reading archive '{path}'...")
archive = py7zr.SevenZipFile(path, mode='r')
uncompressed_size = archive.archiveinfo().uncompressed
if uncompressed_size > max_uncompressed_size:
raise ArchiveTooLargeException
filenames = archive.getnames()
elif path.endswith(".zip"):
l.debug(f"reading archive '{path}'...")
archive = zipfile.ZipFile(path, mode='r')
uncompressed_size = sum([zinfo.file_size for zinfo in archive.filelist])
if uncompressed_size > max_uncompressed_size:
raise ArchiveTooLargeException
filenames = archive.namelist()
else:
raise NotArchiveType
return filenames
class ArchiveTooLargeException(Exception):
pass
class NotArchiveType(Exception):
pass