Huhujingjing commited on
Commit
3ca5af2
·
1 Parent(s): d4819df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -1,20 +1,23 @@
1
- import zipfile
2
 
3
- # 定义要上传的zip文件名
4
- filename = "test.zip"
5
 
6
- # 将zip文件上传到Colab中,并提取到当前路径下
7
- from gradio.inputs import FileUploader
8
- uploader = FileUploader(label="上传zip文件")
9
- input_zip = uploader()
10
- with open(filename, "wb") as f:
11
- f.write(input_zip.getvalue())
12
-
13
- # 解压缩zip文件,并依次遍历其中的每个txt文件
14
- with zipfile.ZipFile(filename, 'r') as zip_ref:
15
- for name in zip_ref.namelist():
16
- if name.endswith(".txt"): # 只处理txt文件
17
- with zip_ref.open(name) as file:
18
- print("当前处理的文件为:", name)
19
- for line in file:
20
- print(line.decode('utf-8').rstrip()) # 输出每一行的内容
 
 
 
 
 
1
+ from zipfile import ZipFile
2
 
3
+ import gradio as gr
 
4
 
5
+
6
+ def zip_to_json(file_obj):
7
+ files = []
8
+ with ZipFile(file_obj.name) as zfile:
9
+ for zinfo in zfile.infolist():
10
+ files.append(
11
+ {
12
+ "name": zinfo.filename,
13
+ "file_size": zinfo.file_size,
14
+ "compressed_size": zinfo.compress_size,
15
+ }
16
+ )
17
+ return files
18
+
19
+
20
+ demo = gr.Interface(zip_to_json, "file", "txt")
21
+
22
+ if __name__ == "__main__":
23
+ demo.launch()