Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import tempfile
|
|
| 6 |
import shutil
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
-
def process_archives(uploaded_files, position, options, custom_text):
|
| 10 |
if not uploaded_files:
|
| 11 |
return []
|
| 12 |
|
|
@@ -15,6 +15,11 @@ def process_archives(uploaded_files, position, options, custom_text):
|
|
| 15 |
|
| 16 |
current_time_str = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
for file_path in uploaded_files:
|
| 19 |
filename = os.path.basename(file_path)
|
| 20 |
ext = os.path.splitext(filename)[1].lower()
|
|
@@ -24,7 +29,7 @@ def process_archives(uploaded_files, position, options, custom_text):
|
|
| 24 |
continue
|
| 25 |
|
| 26 |
archive_basename = os.path.splitext(filename)[0]
|
| 27 |
-
extract_dir = os.path.join(temp_dir, archive_basename)
|
| 28 |
os.makedirs(extract_dir, exist_ok=True)
|
| 29 |
|
| 30 |
# 根據副檔名選擇解壓縮方式
|
|
@@ -39,8 +44,12 @@ def process_archives(uploaded_files, position, options, custom_text):
|
|
| 39 |
print(f"無法讀取壓縮檔 {filename}: {e}")
|
| 40 |
continue
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# 組合要增加的文字
|
| 46 |
components = []
|
|
@@ -66,27 +75,47 @@ def process_archives(uploaded_files, position, options, custom_text):
|
|
| 66 |
else:
|
| 67 |
name, file_ext = os.path.splitext(f)
|
| 68 |
new_filename = f"{name}_{add_str}{file_ext}"
|
| 69 |
-
|
|
|
|
| 70 |
new_path = os.path.join(renamed_dir, new_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
shutil.copy2(old_path, new_path)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
for
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
for
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
processed_files.append(output_archive_path)
|
| 91 |
|
| 92 |
return processed_files
|
|
@@ -94,7 +123,7 @@ def process_archives(uploaded_files, position, options, custom_text):
|
|
| 94 |
# 建立 Gradio 網頁介面
|
| 95 |
with gr.Blocks(title="壓縮檔內容批次重新命名工具", theme=gr.themes.Soft()) as demo:
|
| 96 |
gr.Markdown("## 📦 壓縮檔內容批次重新命名工具 (支援 ZIP 與 7Z)")
|
| 97 |
-
gr.Markdown("上傳 `.zip` 或 `.7z` 檔案後,自訂你想增加的文字與位置
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column(scale=1):
|
|
@@ -119,16 +148,23 @@ with gr.Blocks(title="壓縮檔內容批次重新命名工具", theme=gr.themes.
|
|
| 119 |
info="需在上方勾選「自訂文字」才會生效"
|
| 120 |
)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
gr.Markdown("### 📂 檔案上傳與下載")
|
| 124 |
-
# 更新 file_types,讓介面允許選擇 .7z 檔案
|
| 125 |
file_input = gr.File(label="上傳壓縮檔 (可多選 ZIP 或 7Z)", file_count="multiple", type="filepath", file_types=[".zip", ".7z"])
|
| 126 |
submit_btn = gr.Button("🚀 開始處理", variant="primary")
|
| 127 |
file_output = gr.File(label="下載處理後的新壓縮檔")
|
| 128 |
|
|
|
|
| 129 |
submit_btn.click(
|
| 130 |
fn=process_archives,
|
| 131 |
-
inputs=[file_input, position_radio, options_checkbox, custom_text_input],
|
| 132 |
outputs=file_output
|
| 133 |
)
|
| 134 |
|
|
|
|
| 6 |
import shutil
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
+
def process_archives(uploaded_files, position, options, custom_text, pack_mode):
|
| 10 |
if not uploaded_files:
|
| 11 |
return []
|
| 12 |
|
|
|
|
| 15 |
|
| 16 |
current_time_str = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 17 |
|
| 18 |
+
# 如果是一鍵打包,建立一個共用的資料夾來存放所有重新命名的檔案
|
| 19 |
+
if pack_mode == "一鍵打包":
|
| 20 |
+
master_renamed_dir = os.path.join(temp_dir, "master_renamed")
|
| 21 |
+
os.makedirs(master_renamed_dir, exist_ok=True)
|
| 22 |
+
|
| 23 |
for file_path in uploaded_files:
|
| 24 |
filename = os.path.basename(file_path)
|
| 25 |
ext = os.path.splitext(filename)[1].lower()
|
|
|
|
| 29 |
continue
|
| 30 |
|
| 31 |
archive_basename = os.path.splitext(filename)[0]
|
| 32 |
+
extract_dir = os.path.join(temp_dir, f"extracted_{archive_basename}")
|
| 33 |
os.makedirs(extract_dir, exist_ok=True)
|
| 34 |
|
| 35 |
# 根據副檔名選擇解壓縮方式
|
|
|
|
| 44 |
print(f"無法讀取壓縮檔 {filename}: {e}")
|
| 45 |
continue
|
| 46 |
|
| 47 |
+
# 決定重新命名後的檔案要放哪裡
|
| 48 |
+
if pack_mode == "個別打包":
|
| 49 |
+
renamed_dir = os.path.join(temp_dir, f"{archive_basename}_renamed")
|
| 50 |
+
os.makedirs(renamed_dir, exist_ok=True)
|
| 51 |
+
else:
|
| 52 |
+
renamed_dir = master_renamed_dir
|
| 53 |
|
| 54 |
# 組合要增加的文字
|
| 55 |
components = []
|
|
|
|
| 75 |
else:
|
| 76 |
name, file_ext = os.path.splitext(f)
|
| 77 |
new_filename = f"{name}_{add_str}{file_ext}"
|
| 78 |
+
|
| 79 |
+
# 防呆機制:避免同名檔案覆蓋 (特別是在一鍵打包時)
|
| 80 |
new_path = os.path.join(renamed_dir, new_filename)
|
| 81 |
+
if os.path.exists(new_path):
|
| 82 |
+
name, file_ext = os.path.splitext(new_filename)
|
| 83 |
+
counter = 1
|
| 84 |
+
while os.path.exists(os.path.join(renamed_dir, f"{name}_{counter}{file_ext}")):
|
| 85 |
+
counter += 1
|
| 86 |
+
new_filename = f"{name}_{counter}{file_ext}"
|
| 87 |
+
new_path = os.path.join(renamed_dir, new_filename)
|
| 88 |
+
|
| 89 |
shutil.copy2(old_path, new_path)
|
| 90 |
|
| 91 |
+
# 如果是個別打包,處理完一個壓縮檔就立刻打包
|
| 92 |
+
if pack_mode == "個別打包":
|
| 93 |
+
output_archive_name = f"processed_{filename}"
|
| 94 |
+
output_archive_path = os.path.join(temp_dir, output_archive_name)
|
| 95 |
+
|
| 96 |
+
if ext == '.zip':
|
| 97 |
+
with zipfile.ZipFile(output_archive_path, 'w', zipfile.ZIP_DEFLATED) as out_zip:
|
| 98 |
+
for root, dirs, files in os.walk(renamed_dir):
|
| 99 |
+
for f in files:
|
| 100 |
+
file_to_zip = os.path.join(root, f)
|
| 101 |
+
out_zip.write(file_to_zip, arcname=f)
|
| 102 |
+
elif ext == '.7z':
|
| 103 |
+
with py7zr.SevenZipFile(output_archive_path, 'w') as out_7z:
|
| 104 |
+
for root, dirs, files in os.walk(renamed_dir):
|
| 105 |
+
for f in files:
|
| 106 |
+
file_to_7z = os.path.join(root, f)
|
| 107 |
+
out_7z.write(file_to_7z, arcname=f)
|
| 108 |
+
|
| 109 |
+
processed_files.append(output_archive_path)
|
| 110 |
|
| 111 |
+
# 如果是一鍵打包,等全部檔案處理完後,統一打包成一個 ZIP
|
| 112 |
+
if pack_mode == "一鍵打包":
|
| 113 |
+
output_archive_path = os.path.join(temp_dir, "all_processed_files.zip")
|
| 114 |
+
with zipfile.ZipFile(output_archive_path, 'w', zipfile.ZIP_DEFLATED) as out_zip:
|
| 115 |
+
for root, dirs, files in os.walk(master_renamed_dir):
|
| 116 |
+
for f in files:
|
| 117 |
+
file_to_zip = os.path.join(root, f)
|
| 118 |
+
out_zip.write(file_to_zip, arcname=f)
|
| 119 |
processed_files.append(output_archive_path)
|
| 120 |
|
| 121 |
return processed_files
|
|
|
|
| 123 |
# 建立 Gradio 網頁介面
|
| 124 |
with gr.Blocks(title="壓縮檔內容批次重新命名工具", theme=gr.themes.Soft()) as demo:
|
| 125 |
gr.Markdown("## 📦 壓縮檔內容批次重新命名工具 (支援 ZIP 與 7Z)")
|
| 126 |
+
gr.Markdown("上傳 `.zip` 或 `.7z` 檔案後,自訂你想增加的文字與位置,並選擇個別下載或是一鍵打包下載。")
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column(scale=1):
|
|
|
|
| 148 |
info="需在上方勾選「自訂文字」才會生效"
|
| 149 |
)
|
| 150 |
|
| 151 |
+
pack_mode_radio = gr.Radio(
|
| 152 |
+
choices=["個別打包", "一鍵打包"],
|
| 153 |
+
value="個別打包",
|
| 154 |
+
label="4. 打包方式",
|
| 155 |
+
info="個別打包:維持原本的壓縮檔數量 / 一鍵打包:將所有檔案統合成一個 ZIP 檔"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
with gr.Column(scale=1):
|
| 159 |
gr.Markdown("### 📂 檔案上傳與下載")
|
|
|
|
| 160 |
file_input = gr.File(label="上傳壓縮檔 (可多選 ZIP 或 7Z)", file_count="multiple", type="filepath", file_types=[".zip", ".7z"])
|
| 161 |
submit_btn = gr.Button("🚀 開始處理", variant="primary")
|
| 162 |
file_output = gr.File(label="下載處理後的新壓縮檔")
|
| 163 |
|
| 164 |
+
# 綁定按鈕點擊事件,傳入所有設定值
|
| 165 |
submit_btn.click(
|
| 166 |
fn=process_archives,
|
| 167 |
+
inputs=[file_input, position_radio, options_checkbox, custom_text_input, pack_mode_radio],
|
| 168 |
outputs=file_output
|
| 169 |
)
|
| 170 |
|