75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
import zipfile
|
|
import os
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# 標準モジュールがない場合の安全策
|
|
try:
|
|
import folder_paths
|
|
except ImportError:
|
|
folder_paths = None
|
|
|
|
class ZipOutputFolder:
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
|
|
"clear_previous_zips": ("BOOLEAN", {"default": True}),
|
|
}
|
|
}
|
|
|
|
# 出力を2つに増やしました
|
|
RETURN_TYPES = ("STRING", "STRING")
|
|
RETURN_NAMES = ("full_path", "filename_only")
|
|
FUNCTION = "zip_folder"
|
|
CATEGORY = "Custom"
|
|
|
|
def zip_folder(self, seed, clear_previous_zips):
|
|
if folder_paths:
|
|
output_dir = Path(folder_paths.get_output_directory())
|
|
else:
|
|
output_dir = Path("output")
|
|
|
|
if not output_dir.exists():
|
|
output_dir = Path("output")
|
|
|
|
# --- オプション: 古いzip削除 ---
|
|
if clear_previous_zips:
|
|
for existing_zip in output_dir.glob("*.zip"):
|
|
try:
|
|
existing_zip.unlink()
|
|
print(f"Deleted old zip: {existing_zip}")
|
|
except Exception as e:
|
|
print(f"Failed to delete {existing_zip}: {e}")
|
|
|
|
# --- Zipファイル名作成 ---
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
zip_filename = f"output_{timestamp}.zip"
|
|
zip_path = output_dir / zip_filename
|
|
|
|
# --- Zip作成 ---
|
|
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
|
for file_path in output_dir.rglob("*"):
|
|
if not file_path.is_file():
|
|
continue
|
|
if file_path.absolute() == zip_path.absolute():
|
|
continue
|
|
if file_path.suffix.lower() == ".zip":
|
|
continue
|
|
|
|
arcname = file_path.relative_to(output_dir)
|
|
zipf.write(file_path, arcname)
|
|
|
|
print(f"Zip created: {zip_path}")
|
|
|
|
# フルパスと、ファイル名単体を返す
|
|
return (str(zip_path), zip_filename)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"Zip Output Folder": ZipOutputFolder
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"Zip Output Folder": "Zip Output Folder (Batch)"
|
|
} |