def test_SHA1_file(): filepath = path.join("tests", "testarchive.zip") version = to_bytes(slackviewer.__version__) def SHA1_file(filepath, extra=b''): """The original unoptimized method (reads whole file instead of chunks)""" with io.open(filepath, 'rb') as f: return hashlib.sha1(f.read() + extra).hexdigest() expected = SHA1_file(filepath, version) actual = archive.SHA1_file(filepath, version) assert actual == expected
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__)) try: extracted_path = os.path.join(os.getenv('SLACKVIEWER_TEMP_PATH'), archive_sha) except: 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
def extract_archive(filepath): if os.path.isdir(filepath): print("Archive already extracted. Viewing from {}...".format(filepath)) return filepath if 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 archive_info = { "sha1": archive_sha, "filename": os.path.split(filepath)[1], "empty_dms": remove_empty_dirs(extracted_path) } with io.open(os.path.join( extracted_path, ".slackviewer_archive_info.json", ), 'w+', encoding="utf-8") as f: s = json.dumps(archive_info, ensure_ascii=False) s = to_unicode(s) f.write(s) return extracted_path