Skip to content

Commit

Permalink
New post by iPhone: files2zip & zip2clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ibbsbbr committed Jul 20, 2024
1 parent 8d13f94 commit 06b94e6
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions _posts/2024-07-20-iphone-files2zip-zip2clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
tags:
- iphone
info: aberto.
date: 2024-07-20
type: post
layout: post
published: true
slug: iphone-files2zip-zip2clipboard
title: 'iPhone: files2zip & zip2clipboard'
---

iPhone:
1) Make a zip file with the desired contents;
2) Run the shortcut and select that zip file;
3) The desired contents now are in the clipboard.

script:
```
import os
import shutil
import zipfile
import tarfile
import gzip
import bz2
import sys
import logging
from pathlib import Path
# Set up logging
log_file_path = '/private/var/mobile/Containers/Data/Application/5A3A90C0-A077-4A2C-80A6-0DF006769AC6/Documents/script.log'
logging.basicConfig(filename=log_file_path, level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
def extract_archive(archive_path, extract_dir):
"""Extracts files from various archive formats."""
logging.info(f'Starting extraction for: {archive_path}')
try:
if zipfile.is_zipfile(archive_path):
with zipfile.ZipFile(archive_path, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
elif tarfile.is_tarfile(archive_path):
with tarfile.open(archive_path, 'r') as tar_ref:
tar_ref.extractall(extract_dir)
elif archive_path.endswith('.gz') and not tarfile.is_tarfile(archive_path):
with gzip.open(archive_path, 'rb') as gz_file:
with open(archive_path[:-3], 'wb') as output_file: # Remove .gz extension
shutil.copyfileobj(gz_file, output_file)
elif archive_path.endswith('.bz2') and not tarfile.is_tarfile(archive_path):
with bz2.open(archive_path, 'rb') as bz2_file:
with open(archive_path[:-4], 'wb') as output_file: # Remove .bz2 extension
shutil.copyfileobj(bz2_file, output_file)
else:
logging.warning(f'Unsupported archive format: {archive_path}')
print(f"Unsupported archive format: {archive_path}")
except Exception as e:
logging.error(f'Failed to extract {archive_path}: {e}', exc_info=True)
print(f"Failed to extract {archive_path}: {e}")
def process_files(root_path, rar_folder):
"""Recursively processes files and folders, extracting archives and moving RAR files."""
logging.info(f'Starting to process files in: {root_path}')
for dirpath, _, filenames in os.walk(root_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if os.path.isfile(file_path):
if file_path.endswith('.rar'):
destination_path = os.path.join(rar_folder, filename)
try:
shutil.move(file_path, destination_path)
logging.info(f'Moved RAR file {file_path} to {destination_path}')
except Exception as e:
logging.error(f'Failed to move {file_path} to {destination_path}: {e}', exc_info=True)
print(f"Failed to move {file_path}: {e}")
else:
extract_archive(file_path, dirpath)
def move_to_cat_folder(root_path):
"""Moves all files and folders to a new 'CAT' folder at the same level as the root path."""
logging.info(f'Starting to move files to CAT folder from: {root_path}')
root_path = Path(root_path).resolve()
cat_folder_path = root_path / 'CAT' # Create 'CAT' folder at the same level
cat_folder_path.mkdir(exist_ok=True)
for item in root_path.iterdir():
if item.name in ['CAT', 'CAT-RAR']:
continue
destination_path = cat_folder_path / item.name
try:
shutil.move(str(item), destination_path)
logging.info(f'Moved {item} to {destination_path}')
except Exception as e:
logging.error(f'Failed to move {item} to {destination_path}: {e}', exc_info=True)
print(f"Failed to move {item}: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <path>")
logging.error('Incorrect usage. Path not provided.')
sys.exit(1)
target_path = sys.argv[1]
if os.path.exists(target_path):
logging.info(f'Processing started for path: {target_path}')
rar_folder_path = os.path.join(target_path, 'CAT-RAR')
os.makedirs(rar_folder_path, exist_ok=True)
process_files(target_path, rar_folder_path)
move_to_cat_folder(target_path)
print("Processing and moving completed!")
logging.info('Processing completed successfully.')
else:
print(f"The path {target_path} does not exist.")
logging.error(f'The path {target_path} does not exist.')
```

0 comments on commit 06b94e6

Please sign in to comment.