cornsnake.zip_dir

Functions for creating a zip archive from a directory or a list of files.

Documentation

 1"""
 2Functions for creating a zip archive from a directory or a list of files.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/zip_dir.html)
 5"""
 6
 7import shutil
 8import zipfile
 9
10
11def create_zip(source_dir: str, output_zip_file: str) -> None:
12    """
13    Function to create a zip archive from a directory.
14
15    Args:
16    source_dir (str): The path to the source directory to be zipped.
17    output_zip_file (str): The name of the output zip file.
18
19    Returns:
20    None
21    """
22    if output_zip_file.endswith(".zip"):
23        output_zip_file = output_zip_file[:-4]
24    shutil.make_archive(output_zip_file, "zip", source_dir)
25
26
27def create_zip_of_files(
28    files_to_include: list[str], root_dir: str, path_to_output_zipfile: str
29) -> list[str]:
30    """
31    Function to create a zip archive from a list of files.
32
33    Args:
34    files_to_include (list[str]): List of filepaths to include.
35    root_dir (str): The root dir to use when deciding the relative path inside the ZIP.
36    path_to_output_zipfile (str): The path to the output zip file.
37
38    Returns:
39    list[str]: List of file paths inside the ZIP.
40    """
41    if not files_to_include:
42        return []
43    with zipfile.ZipFile(path_to_output_zipfile, "w") as zip:
44        file_names = []
45        for file_name in files_to_include:
46            short_file_name = file_name.replace(root_dir, "")
47            file_names.append(short_file_name)
48            zip.write(file_name, short_file_name)
49        return file_names
def create_zip(source_dir: str, output_zip_file: str) -> None:
12def create_zip(source_dir: str, output_zip_file: str) -> None:
13    """
14    Function to create a zip archive from a directory.
15
16    Args:
17    source_dir (str): The path to the source directory to be zipped.
18    output_zip_file (str): The name of the output zip file.
19
20    Returns:
21    None
22    """
23    if output_zip_file.endswith(".zip"):
24        output_zip_file = output_zip_file[:-4]
25    shutil.make_archive(output_zip_file, "zip", source_dir)

Function to create a zip archive from a directory.

Args: source_dir (str): The path to the source directory to be zipped. output_zip_file (str): The name of the output zip file.

Returns: None

def create_zip_of_files( files_to_include: list[str], root_dir: str, path_to_output_zipfile: str) -> list[str]:
28def create_zip_of_files(
29    files_to_include: list[str], root_dir: str, path_to_output_zipfile: str
30) -> list[str]:
31    """
32    Function to create a zip archive from a list of files.
33
34    Args:
35    files_to_include (list[str]): List of filepaths to include.
36    root_dir (str): The root dir to use when deciding the relative path inside the ZIP.
37    path_to_output_zipfile (str): The path to the output zip file.
38
39    Returns:
40    list[str]: List of file paths inside the ZIP.
41    """
42    if not files_to_include:
43        return []
44    with zipfile.ZipFile(path_to_output_zipfile, "w") as zip:
45        file_names = []
46        for file_name in files_to_include:
47            short_file_name = file_name.replace(root_dir, "")
48            file_names.append(short_file_name)
49            zip.write(file_name, short_file_name)
50        return file_names

Function to create a zip archive from a list of files.

Args: files_to_include (list[str]): List of filepaths to include. root_dir (str): The root dir to use when deciding the relative path inside the ZIP. path_to_output_zipfile (str): The path to the output zip file.

Returns: list[str]: List of file paths inside the ZIP.