Icon

m_​030_​python_​zip_​unzip

use Python zipfile to zip an unzip files

use Python zipfile to zip an unzip files

use Python zipfile to zip an unzip fileshttps://hub.knime.com/-/spaces/-/~WP7FMlxYUrZ0jF6T/current-state/download the whole workflow group: https://hub.knime.com/mlauber71/spaces/Public/kn_example_r_zip_archiv~M9guutnmWfSzP8Pb/ import knime.scripting.io as knioimport zipfileimport osimport time # Import the time moduledef unzip_with_timestamps(zip_path, extract_to): # Ensure the extraction path exists if not os.path.exists(extract_to): os.makedirs(extract_to) # Open the zip file with zipfile.ZipFile(zip_path, 'r') as zip_ref: # Extract all the contents into the directory zip_ref.extractall(extract_to) # Iterate over each file in the archive to set the timestamp for info in zip_ref.infolist(): extracted_path = os.path.join(extract_to, info.filename) # Preserve the original date and time date_time = time.mktime(info.date_time + (0, 0, -1)) # Set the modification time os.utime(extracted_path, (date_time, date_time))zip_path = knio.flow_variables['var_path_data'] + 'file_with_times.zip' # Path to your zip fileextract_to = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path where you want to extract the filesunzip_with_timestamps(zip_path, extract_to) import knime.scripting.io as knioimport zipfileimport osdef zip_folder(folder_path, zip_path): # Create a ZIP file with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: # Walk through the directory for root, dirs, files in os.walk(folder_path): for file in files: # Create the full path to the file file_path = os.path.join(root, file) # Write the file to the ZIP archive # arcname is the name of the file in the archive, which is the relative path from the folder being zipped arcname = os.path.relpath(file_path, folder_path) zipf.write(file_path, arcname)folder_path = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path to the folder you want to zipzip_path = knio.flow_variables['var_path_data'] + 'file_packed_with_python.zip' # Path to the ZIP file you want to createzip_folder(folder_path, zip_path) KNIME and Python — Setting up and managing Conda environmentshttps://medium.com/p/2ac217792539How to Set Up Your Python Extensionshttps://www.knime.com/blog/how-to-setup-the-python-extension locate and create/data/ folderwith absolute pathsvar_path_dataunzip the files from a .zip filezip_path = knio.flow_variables['var_path_data'] + 'file_with_times.zip' # Path to your zip fileextract_to = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path where you want to extract the filescompress files from a folder into a ZIP filefolder_path = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path to the folder you want to zipzip_path = knio.flow_variables['var_path_data'] + 'file_packed_with_python.zip' # Path to the ZIP file you want to create Collect LocalMetadata Java EditVariable (simple) Python Script Python Script use Python zipfile to zip an unzip fileshttps://hub.knime.com/-/spaces/-/~WP7FMlxYUrZ0jF6T/current-state/download the whole workflow group: https://hub.knime.com/mlauber71/spaces/Public/kn_example_r_zip_archiv~M9guutnmWfSzP8Pb/ import knime.scripting.io as knioimport zipfileimport osimport time # Import the time moduledef unzip_with_timestamps(zip_path, extract_to): # Ensure the extraction path exists if not os.path.exists(extract_to): os.makedirs(extract_to) # Open the zip file with zipfile.ZipFile(zip_path, 'r') as zip_ref: # Extract all the contents into the directory zip_ref.extractall(extract_to) # Iterate over each file in the archive to set the timestamp for info in zip_ref.infolist(): extracted_path = os.path.join(extract_to, info.filename) # Preserve the original date and time date_time = time.mktime(info.date_time + (0, 0, -1)) # Set the modification time os.utime(extracted_path, (date_time, date_time))zip_path = knio.flow_variables['var_path_data'] + 'file_with_times.zip' # Path to your zip fileextract_to = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path where you want to extract the filesunzip_with_timestamps(zip_path, extract_to) import knime.scripting.io as knioimport zipfileimport osdef zip_folder(folder_path, zip_path): # Create a ZIP file with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: # Walk through the directory for root, dirs, files in os.walk(folder_path): for file in files: # Create the full path to the file file_path = os.path.join(root, file) # Write the file to the ZIP archive # arcname is the name of the file in the archive, which is the relative path from the folder being zipped arcname = os.path.relpath(file_path, folder_path) zipf.write(file_path, arcname)folder_path = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path to the folder you want to zipzip_path = knio.flow_variables['var_path_data'] + 'file_packed_with_python.zip' # Path to the ZIP file you want to createzip_folder(folder_path, zip_path) KNIME and Python — Setting up and managing Conda environmentshttps://medium.com/p/2ac217792539How to Set Up Your Python Extensionshttps://www.knime.com/blog/how-to-setup-the-python-extension locate and create/data/ folderwith absolute pathsvar_path_dataunzip the files from a .zip filezip_path = knio.flow_variables['var_path_data'] + 'file_with_times.zip' # Path to your zip fileextract_to = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path where you want to extract the filescompress files from a folder into a ZIP filefolder_path = knio.flow_variables['var_path_data'] + 'extract from python - keep date-time' # Path to the folder you want to zipzip_path = knio.flow_variables['var_path_data'] + 'file_packed_with_python.zip' # Path to the ZIP file you want to createCollect LocalMetadata Java EditVariable (simple) Python Script Python Script

Nodes

Extensions

Links