Skip to content

Functions

src.utils.compression.compress_7z(path_in: Path, path_out: Optional[Path] = None, word_size: WordSize = WordSize.WS16, dict_size: DictionarySize = DictionarySize.DS1536) -> Path

Compress a target file and save it as a 7z archive to the output directory.

Parameters:

Name Type Description Default
path_in Path

File to compress.

required
path_out Path | None

Path to the archive to be saved. Use 'compressed' subdirectory if not provided.

None
word_size WordSize

Word size value to use for the compression.

WS16
dict_size DictionarySize

Dictionary size value to use for the compression.

DS1536

Returns:

Type Description
Path

Path to the resulting 7z archive.

Source code in src\utils\compression.py
 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
def compress_7z(
    path_in: Path,
    path_out: Optional[Path] = None,
    word_size: WordSize = WordSize.WS16,
    dict_size: DictionarySize = DictionarySize.DS1536
) -> Path:
    """Compress a target file and save it as a 7z archive to the output directory.

    Args:
        path_in: File to compress.
        path_out: Path to the archive to be saved. Use 'compressed' subdirectory if not provided.
        word_size: Word size value to use for the compression.
        dict_size: Dictionary size value to use for the compression.

    Returns:
        Path to the resulting 7z archive.
    """
    # Define the output file path
    path_out = path_out or Path(path_in.parent, '.compressed', path_in.name)
    path_out = path_out.with_suffix('.7z')
    null_device = open(os.devnull, 'w')

    # Compress the file
    with suppress(Exception):
        subprocess.run([
            "7z", "a", "-t7z", "-m0=LZMA", "-mx=9",
            f"-md={dict_size}M",
            f"-mfb={word_size}",
            str(path_out),
            str(path_in)
        ], stdout=null_device, stderr=null_device)
    return path_out

src.utils.compression.compress_7z_all(path_in: Path, path_out: Path = None, word_size: WordSize = WordSize.WS16, dict_size: DictionarySize = DictionarySize.DS1536) -> None

Compress every file inside path_in directory as 7z archives, then output those archives in the path_out.

Parameters:

Name Type Description Default
path_in Path

Directory containing files to compress.

required
path_out Path

Directory to place the archives. Use a subdirectory 'compressed' if not provided.

None
word_size WordSize

Word size value to use for the compression.

WS16
dict_size DictionarySize

Dictionary size value to use for the compression.

DS1536
Source code in src\utils\compression.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def compress_7z_all(
    path_in: Path,
    path_out: Path = None,
    word_size: WordSize = WordSize.WS16,
    dict_size: DictionarySize = DictionarySize.DS1536
) -> None:
    """Compress every file inside `path_in` directory as 7z archives, then output
    those archives in the `path_out`.

    Args:
        path_in: Directory containing files to compress.
        path_out: Directory to place the archives. Use a subdirectory 'compressed' if not provided.
        word_size: Word size value to use for the compression.
        dict_size: Dictionary size value to use for the compression.
    """
    # Use "compressed" subdirectory if not provided, ensure output directory exists
    path_out = path_out or Path(path_in, '.compressed')
    path_out.mkdir(mode=777, parents=True, exist_ok=True)

    # Get a list of all .psd files in the directory
    files = [
        Path(path_in, n) for n in os.listdir(path_in)
        if Path(path_in, n).is_file()]

    # Compress each file
    with tqdm(total=len(files), desc="Compressing files", unit="file") as pbar:
        for f in files:
            p = (path_out / f.name).with_suffix('.7z')
            pbar.set_description(f.name)
            compress_7z(
                path_in=f,
                path_out=p,
                word_size=word_size,
                dict_size=dict_size)
            pbar.update()

src.utils.compression.unpack_zip(path: Path) -> None

Unpack target 'zip' archive.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
152
153
154
155
156
157
158
159
160
161
162
163
164
def unpack_zip(path: Path) -> None:
    """Unpack target 'zip' archive.

    Args:
        path: Path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    with zipfile.ZipFile(path, 'r') as zip_ref:
        zip_ref.extractall(path=path.parent)

src.utils.compression.unpack_gz(path: Path) -> None

Unpack target 'gz' archive.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def unpack_gz(path: Path) -> None:
    """Unpack target 'gz' archive.

    Args:
        path: Path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    output = path.parent / path.name.replace('.gz', '')
    with gzip.open(path, 'rb') as arch:
        with open(output, 'wb') as f:
            shutil.copyfileobj(arch, f)

src.utils.compression.unpack_xz(path: Path) -> None

Unpack target 'xz' archive.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def unpack_xz(path: Path) -> None:
    """Unpack target 'xz' archive.

    Args:
        path: Path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    output = path.parent / path.name.replace('.xz', '')
    with lzma.open(path, mode='r') as arch:
        with open(output, 'wb') as f:
            shutil.copyfileobj(arch, f)

src.utils.compression.unpack_7z(path: Path) -> None

Unpack target '7z' archive.

Parameters:

Name Type Description Default
path Path

path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
201
202
203
204
205
206
207
208
209
210
211
212
213
def unpack_7z(path: Path) -> None:
    """Unpack target '7z' archive.

    Args:
        path: path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    with py7zr.SevenZipFile(path, 'r') as arch:
        arch.extractall(path=path.parent)

src.utils.compression.unpack_tar_gz(path: Path) -> None

Unpack target tar.gz archive.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
216
217
218
219
220
221
222
223
224
225
226
227
228
def unpack_tar_gz(path: Path) -> None:
    """Unpack target `tar.gz` archive.

    Args:
        path: Path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    with tarfile.open(path, 'r:gz') as tar:
        tar.extractall(path=path.parent)

src.utils.compression.unpack_tar_xz(path: Path) -> None

Unpack target tar.xz archive.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
231
232
233
234
235
236
237
238
239
240
241
242
243
def unpack_tar_xz(path: Path) -> None:
    """Unpack target `tar.xz` archive.

    Args:
        path: Path to the archive.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    if not path.is_file():
        raise FileNotFoundError(f'Archive not found: {str(path)}')
    with tarfile.open(path, 'r:xz') as tar:
        tar.extractall(path=path.parent)

src.utils.compression.unpack_archive(path: Path, remove: bool = True) -> None

Unpack an archive using the correct methodology based on its extension.

Parameters:

Name Type Description Default
path Path

Path to the archive.

required
remove bool

Whether to remove the archive after unpacking.

True

Raises:

Type Description
FileNotFoundError

If archive couldn't be located.

Source code in src\utils\compression.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def unpack_archive(path: Path, remove: bool = True) -> None:
    """Unpack an archive using the correct methodology based on its extension.

    Args:
        path: Path to the archive.
        remove: Whether to remove the archive after unpacking.

    Raises:
        FileNotFoundError: If archive couldn't be located.
    """
    action_map: dict[str, Callable] = {
        ArchiveExt.Zip: unpack_zip,
        ArchiveExt.GZip: unpack_gz,
        ArchiveExt.XZip: unpack_xz,
        ArchiveExt.TarGZip: unpack_tar_gz,
        ArchiveExt.TarXZip: unpack_tar_xz,
        ArchiveExt.SevenZip: unpack_7z,
        ArchiveExt.TarSevenZip: unpack_7z
    }
    if path.suffix not in action_map:
        return
    action = action_map[path.suffix]
    with ARCHIVE_LOCK:
        _ = action(path)
    if remove:
        os.remove(path)
    gc.collect()