Unzip — Extract To
This one is short and sweet and has its root in legacy and contemporary computing. It is never out of fashion!
Use Case: If you have ever done batch processing such as payroll, tax computations, very large scale ETL operations, real-time shares transactions then you would have sent a zip file at a secure location at some point for a big powerful computer to crunch and munch.
If you want to extract all the files from a zip file (security or PGP or hashes I leave to you), without relying on the third-party libraries, then this little piece of code is useful.
I used this fragment for a neural network implementation to batch process thousands of files.
Applies to
· Any zip file in the current folder location and extract it to a specific folder
Benefits
· No third-party library needed
· Implemented as a function so just include
When to use
· When you want to extract files from a zipped archive
Code fragment
import zipfile
def extract_from_zip(zip_filename_param: str, target_folder_param: str):
print("Extracting...")
with zipfile.ZipFile(zip_filename_param, 'r') as zf:
print(f"\n\nCompressed file ({zip_filename_param}) contains following:\n{zf.namelist()}\n")
extract_count = 0
print("-" * 40)
for the_file in zf.namelist():
zf.extractall(target_folder_param)
print(f"Extracting: ({the_file})")
extract_count = extract_count + 1
#
print("-" * 40, f"\nTotal {extract_count} files processed")
return
# run the function, assumes a zip file called t.zip that will be extracted in the root folder (sorry)
extract_from_zip("t.zip", "C:\\")