crytic_compile.utils.zip

Handle ZIP operations

 1"""
 2Handle ZIP operations
 3"""
 4import json
 5import zipfile
 6
 7# Cycle dependency
 8from typing import TYPE_CHECKING, List, Union
 9from zipfile import ZipFile
10
11from crytic_compile.platform.archive import generate_archive_export
12
13if TYPE_CHECKING:
14    from crytic_compile import CryticCompile
15
16
17def _to_str(txt: Union[bytes, str]) -> str:
18    """Convert bytes to an utf8 str. Do nothing if its already a str
19
20    Args:
21        txt (Union[bytes, str]): target name
22
23    Returns:
24        str: str
25    """
26    if isinstance(txt, bytes):
27        return txt.decode("utf8")
28    return txt
29
30
31def load_from_zip(target: str) -> List["CryticCompile"]:
32    """Load a file from a zip
33
34    Args:
35        target (str): path to the file
36
37    Returns:
38        List[CryticCompile]: List of loaded projects
39    """
40    # pylint: disable=import-outside-toplevel
41    from crytic_compile import CryticCompile
42
43    compilations = []
44    with ZipFile(target, "r") as file_desc:
45        for project in file_desc.namelist():
46            compilations.append(
47                CryticCompile(_to_str(file_desc.read(project)), compile_force_framework="Archive")
48            )
49
50    return compilations
51
52
53# https://docs.python.org/3/library/zipfile.html#zipfile-objects
54ZIP_TYPES_ACCEPTED = {
55    "lzma": zipfile.ZIP_LZMA,
56    "stored": zipfile.ZIP_STORED,
57    "deflated": zipfile.ZIP_DEFLATED,
58    "bzip2": zipfile.ZIP_BZIP2,
59}
60
61
62def save_to_zip(
63    crytic_compiles: List["CryticCompile"], zip_filename: str, zip_type: str = "lzma"
64) -> None:
65    """Save projects to a zip
66
67    Args:
68        crytic_compiles (List[CryticCompile]): List of project to save
69        zip_filename (str): zip filename
70        zip_type (str): Zip types. Supported lzma, stored, deflated, bzip2. Defaults to "lzma".
71    """
72    with ZipFile(
73        zip_filename, "w", compression=ZIP_TYPES_ACCEPTED.get(zip_type, zipfile.ZIP_LZMA)
74    ) as file_desc:
75        for crytic_compile in crytic_compiles:
76            output, target_name = generate_archive_export(crytic_compile)
77            file_desc.writestr(target_name, json.dumps(output))
def load_from_zip(target: str) -> list[crytic_compile.crytic_compile.CryticCompile]:
32def load_from_zip(target: str) -> List["CryticCompile"]:
33    """Load a file from a zip
34
35    Args:
36        target (str): path to the file
37
38    Returns:
39        List[CryticCompile]: List of loaded projects
40    """
41    # pylint: disable=import-outside-toplevel
42    from crytic_compile import CryticCompile
43
44    compilations = []
45    with ZipFile(target, "r") as file_desc:
46        for project in file_desc.namelist():
47            compilations.append(
48                CryticCompile(_to_str(file_desc.read(project)), compile_force_framework="Archive")
49            )
50
51    return compilations

Load a file from a zip

Args: target (str): path to the file

Returns: List[CryticCompile]: List of loaded projects

ZIP_TYPES_ACCEPTED = {'lzma': 14, 'stored': 0, 'deflated': 8, 'bzip2': 12}
def save_to_zip( crytic_compiles: list[crytic_compile.crytic_compile.CryticCompile], zip_filename: str, zip_type: str = 'lzma') -> None:
63def save_to_zip(
64    crytic_compiles: List["CryticCompile"], zip_filename: str, zip_type: str = "lzma"
65) -> None:
66    """Save projects to a zip
67
68    Args:
69        crytic_compiles (List[CryticCompile]): List of project to save
70        zip_filename (str): zip filename
71        zip_type (str): Zip types. Supported lzma, stored, deflated, bzip2. Defaults to "lzma".
72    """
73    with ZipFile(
74        zip_filename, "w", compression=ZIP_TYPES_ACCEPTED.get(zip_type, zipfile.ZIP_LZMA)
75    ) as file_desc:
76        for crytic_compile in crytic_compiles:
77            output, target_name = generate_archive_export(crytic_compile)
78            file_desc.writestr(target_name, json.dumps(output))

Save projects to a zip

Args: crytic_compiles (List[CryticCompile]): List of project to save zip_filename (str): zip filename zip_type (str): Zip types. Supported lzma, stored, deflated, bzip2. Defaults to "lzma".