Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- assets/streamlit_ui.png +0 -0
- main.py +56 -0
- requirements.txt +1 -0
- zip.py +31 -0
assets/streamlit_ui.png
ADDED
main.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
5 |
+
|
6 |
+
from zip import zip_files, generate_password
|
7 |
+
|
8 |
+
DARA_PATH = "data"
|
9 |
+
os.makedirs(DARA_PATH, exist_ok=True)
|
10 |
+
|
11 |
+
def upload_files(files: List[UploadedFile]):
|
12 |
+
for file in files:
|
13 |
+
with open(file.name, "wb") as f:
|
14 |
+
f.write(file.read())
|
15 |
+
|
16 |
+
import base64
|
17 |
+
def get_binary_file_downloader_html(file_path, text="Download Link"):
|
18 |
+
# ダウンロードリンクを作成する関数
|
19 |
+
with open(file_path, 'rb') as f:
|
20 |
+
data = f.read()
|
21 |
+
b64 = base64.b64encode(data).decode()
|
22 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_path}">{text}</a>'
|
23 |
+
return href
|
24 |
+
|
25 |
+
def main():
|
26 |
+
st.title("File Zipper")
|
27 |
+
|
28 |
+
# ファイルのアップロード
|
29 |
+
files = st.file_uploader("Upload Files", accept_multiple_files=True)
|
30 |
+
|
31 |
+
if files:
|
32 |
+
os.chdir(DARA_PATH)
|
33 |
+
upload_files(files)
|
34 |
+
|
35 |
+
# zipファイルを作成するためのフォームを表示する
|
36 |
+
form = st.form(key='zip_form')
|
37 |
+
zip_file_name = form.text_input("Zip file name",
|
38 |
+
value=f"{os.path.splitext(files[0].name)[0]}.zip")
|
39 |
+
submit_button = form.form_submit_button("Create Zip File")
|
40 |
+
|
41 |
+
if submit_button:
|
42 |
+
# zipファイルを作成する
|
43 |
+
password = generate_password()
|
44 |
+
zip_files(src=[file.name for file in files],
|
45 |
+
dst=zip_file_name,
|
46 |
+
password=password)
|
47 |
+
# ダウンロードリンクとパスワードの表示
|
48 |
+
st.markdown(get_binary_file_downloader_html(zip_file_name,
|
49 |
+
text=f"Download {zip_file_name}"),
|
50 |
+
unsafe_allow_html=True)
|
51 |
+
st.markdown(f"password: :orange[{password}]")
|
52 |
+
|
53 |
+
os.chdir("../")
|
54 |
+
|
55 |
+
if __name__ == '__main__':
|
56 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pyminizip==0.2.6
|
zip.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Union
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
import zipfile
|
5 |
+
import pyminizip
|
6 |
+
|
7 |
+
import secrets
|
8 |
+
import string
|
9 |
+
|
10 |
+
def zip_files(src: Union[str, List[str]], dst: str, password: str) -> bool:
|
11 |
+
try:
|
12 |
+
# 型のチェック
|
13 |
+
if isinstance(src, str):
|
14 |
+
src_list = [src]
|
15 |
+
elif isinstance(src, list) and all(isinstance(src_i, str) for src_i in src):
|
16 |
+
src_list = src
|
17 |
+
else:
|
18 |
+
assert False, "Invalid src: [str, List[str]]"
|
19 |
+
pyminizip.compress_multiple([os.path.basename(src_i) for src_i in src_list],
|
20 |
+
[os.path.dirname(src_i) for src_i in src_list],
|
21 |
+
dst, password, 4)
|
22 |
+
return True
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error: {e}")
|
26 |
+
return False
|
27 |
+
|
28 |
+
def generate_password(length: int = 8) -> str:
|
29 |
+
alphabet = string.ascii_letters + string.digits
|
30 |
+
password = ''.join(secrets.choice(alphabet) for _ in range(length))
|
31 |
+
return password
|