12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- def extract_archive(filepath):
- """
- Returns the path of the archive
- :param str filepath: Path to file to extract or read
- :return: path of the archive
- :rtype: str
- """
- # Checks if file path is a directory
- if os.path.isdir(filepath):
- path = os.path.abspath(filepath)
- print("Archive already extracted. Viewing from {}...".format(path))
- return path
- # Checks if the filepath is a zipfile and continues to extract if it is
- # if not it raises an error
- elif not zipfile.is_zipfile(filepath):
- # Misuse of TypeError? :P
- raise TypeError("{} is not a zipfile".format(filepath))
- archive_sha = SHA1_file(
- filepath=filepath,
- # Add version of slackviewer to hash as well so we can invalidate the cached copy
- # if there are new features added
- extra=to_bytes(slackviewer.__version__)
- )
- extracted_path = os.path.join(SLACKVIEWER_TEMP_PATH, archive_sha)
- if os.path.exists(extracted_path):
- print("{} already exists".format(extracted_path))
- else:
- # Extract zip
- with zipfile.ZipFile(filepath) as zip:
- print("{} extracting to {}...".format(filepath, extracted_path))
- zip.extractall(path=extracted_path)
- print("{} extracted to {}".format(filepath, extracted_path))
- # Add additional file with archive info
- create_archive_info(filepath, extracted_path, archive_sha)
- return extracted_path
|