Skip to content

unzip_file

Unzips a .zip file to a specified destination.

Parameters:

Name Type Description Default
complete_file_path str

The full path to the .zip file.

required
destination str

The destination directory for the unzipped files. Default is the directory of the .zip file.

None

Returns:

Type Description
None

This method does not return any values.

Raises:

Type Description
ValueError

If the file path is invalid or does not end with .zip.

Source code in tinybig/util/utility.py
def unzip_file(complete_file_path: str, destination: str = None):
    """
    Unzips a `.zip` file to a specified destination.

    Parameters
    ----------
    complete_file_path : str
        The full path to the `.zip` file.
    destination : str, optional
        The destination directory for the unzipped files. Default is the directory of the `.zip` file.

    Returns
    -------
    None
        This method does not return any values.

    Raises
    ------
    ValueError
        If the file path is invalid or does not end with `.zip`.
    """
    if complete_file_path is None or not complete_file_path.endswith('.zip'):
        raise ValueError('file_name ending with .zip needs to be provided...')

    if destination is None:
        destination = os.path.dirname(complete_file_path)

    print(f"Unzipping: {complete_file_path}")
    with zipfile.ZipFile(complete_file_path, 'r') as zip_ref:
        zip_ref.extractall(destination)
    print(f"Unzipped: {complete_file_path}")