Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import zipfile
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def process_zips(uploaded_files):
|
| 8 |
+
if not uploaded_files:
|
| 9 |
+
return []
|
| 10 |
+
|
| 11 |
+
processed_files = []
|
| 12 |
+
# 建立一個暫存資料夾來處理檔案,避免搞亂伺服器空間
|
| 13 |
+
temp_dir = tempfile.mkdtemp()
|
| 14 |
+
|
| 15 |
+
for file_path in uploaded_files:
|
| 16 |
+
# 取得上傳檔案的原始檔名
|
| 17 |
+
filename = os.path.basename(file_path)
|
| 18 |
+
|
| 19 |
+
# 確保是 zip 檔
|
| 20 |
+
if not filename.lower().endswith('.zip'):
|
| 21 |
+
continue
|
| 22 |
+
|
| 23 |
+
# 取得不含副檔名的壓縮檔名稱 (例如 test.zip -> test)
|
| 24 |
+
zip_basename = os.path.splitext(filename)[0]
|
| 25 |
+
|
| 26 |
+
# 建立解壓縮用的暫存資料夾
|
| 27 |
+
extract_dir = os.path.join(temp_dir, zip_basename)
|
| 28 |
+
os.makedirs(extract_dir, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
# 嘗試解壓縮
|
| 31 |
+
try:
|
| 32 |
+
with zipfile.ZipFile(file_path, 'r') as zip_ref:
|
| 33 |
+
zip_ref.extractall(extract_dir)
|
| 34 |
+
except zipfile.BadZipFile:
|
| 35 |
+
print(f"無法讀取壓縮檔: {filename}")
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
# 建立存放重新命名後檔案的資料夾
|
| 39 |
+
renamed_dir = os.path.join(temp_dir, f"{zip_basename}_renamed")
|
| 40 |
+
os.makedirs(renamed_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
# 走訪解壓縮出來的所有檔案並重新命名
|
| 43 |
+
for root, dirs, files in os.walk(extract_dir):
|
| 44 |
+
for f in files:
|
| 45 |
+
old_path = os.path.join(root, f)
|
| 46 |
+
|
| 47 |
+
# 新檔名:壓縮檔名_原檔名 (例如 test_image.jpg)
|
| 48 |
+
new_filename = f"{zip_basename}_{f}"
|
| 49 |
+
new_path = os.path.join(renamed_dir, new_filename)
|
| 50 |
+
|
| 51 |
+
# 將檔案複製並重新命名到新資料夾
|
| 52 |
+
shutil.copy2(old_path, new_path)
|
| 53 |
+
|
| 54 |
+
# 將重新命名後的檔案打包成新的 ZIP 檔
|
| 55 |
+
output_zip_name = f"processed_{filename}"
|
| 56 |
+
output_zip_path = os.path.join(temp_dir, output_zip_name)
|
| 57 |
+
|
| 58 |
+
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as out_zip:
|
| 59 |
+
for root, dirs, files in os.walk(renamed_dir):
|
| 60 |
+
for f in files:
|
| 61 |
+
file_to_zip = os.path.join(root, f)
|
| 62 |
+
# 這裡只取檔案名稱,代表將所有檔案攤平放在新的壓縮檔最外層
|
| 63 |
+
out_zip.write(file_to_zip, arcname=f)
|
| 64 |
+
|
| 65 |
+
processed_files.append(output_zip_path)
|
| 66 |
+
|
| 67 |
+
return processed_files
|
| 68 |
+
|
| 69 |
+
# 建立 Gradio 網頁介面
|
| 70 |
+
with gr.Blocks(title="壓縮檔內容批次重新命名工具") as demo:
|
| 71 |
+
gr.Markdown("## 📦 壓縮檔內容批次重新命名工具")
|
| 72 |
+
gr.Markdown("請上傳一個或多個 ZIP 壓縮檔。程式會自動將裡面的檔案加上 **「該壓縮檔的名稱_」** 作為前綴,並打包成新的壓縮檔供您下載。")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
# file_count="multiple" 允許上傳多個檔案,type="filepath" 會回傳檔案路徑的字串列表
|
| 77 |
+
file_input = gr.File(label="1. 上傳壓縮檔 (可多選)", file_count="multiple", type="filepath", file_types=[".zip"])
|
| 78 |
+
submit_btn = gr.Button("2. 開始處理", variant="primary")
|
| 79 |
+
|
| 80 |
+
with gr.Column():
|
| 81 |
+
file_output = gr.File(label="3. 下載處理後的新壓縮檔")
|
| 82 |
+
|
| 83 |
+
# 綁定按鈕點擊事件
|
| 84 |
+
submit_btn.click(
|
| 85 |
+
fn=process_zips,
|
| 86 |
+
inputs=file_input,
|
| 87 |
+
outputs=file_output
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# 啟動應用程式
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|