namelessai commited on
Commit
74c0b49
Β·
verified Β·
1 Parent(s): e3d710d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import zipfile
4
+ import tempfile
5
+ import shutil
6
+ from pathlib import Path
7
+
8
+ try:
9
+ import rarfile
10
+ except ImportError:
11
+ print("Installing rarfile...")
12
+ os.system("pip install rarfile")
13
+ import rarfile
14
+
15
+ def convert_rar_to_zip(rar_file):
16
+ """
17
+ Convert a RAR file to ZIP format.
18
+
19
+ Args:
20
+ rar_file: Path to the uploaded RAR file
21
+
22
+ Returns:
23
+ Path to the created ZIP file or error message
24
+ """
25
+ if rar_file is None:
26
+ return None, "Please upload a RAR file first."
27
+
28
+ try:
29
+ # Create a temporary directory for extraction
30
+ temp_dir = tempfile.mkdtemp()
31
+
32
+ # Get the base name without extension
33
+ base_name = Path(rar_file.name).stem
34
+ zip_path = os.path.join(temp_dir, f"{base_name}.zip")
35
+
36
+ # Extract RAR file
37
+ with rarfile.RarFile(rar_file.name, 'r') as rar_ref:
38
+ rar_ref.extractall(temp_dir)
39
+
40
+ # Create ZIP file
41
+ with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_ref:
42
+ # Walk through extracted files and add them to ZIP
43
+ for root, dirs, files in os.walk(temp_dir):
44
+ for file in files:
45
+ if file.endswith('.zip'):
46
+ continue # Skip the ZIP file itself
47
+ file_path = os.path.join(root, file)
48
+ # Add file with relative path
49
+ arcname = os.path.relpath(file_path, temp_dir)
50
+ zip_ref.write(file_path, arcname)
51
+
52
+ file_size = os.path.getsize(zip_path) / (1024 * 1024) # Size in MB
53
+ success_msg = f"βœ… Conversion successful! ZIP file size: {file_size:.2f} MB"
54
+
55
+ return zip_path, success_msg
56
+
57
+ except rarfile.BadRarFile:
58
+ return None, "❌ Error: Invalid or corrupted RAR file."
59
+ except Exception as e:
60
+ return None, f"❌ Error during conversion: {str(e)}"
61
+
62
+ # Create Gradio interface
63
+ with gr.Blocks(title="RAR to ZIP Converter", theme=gr.themes.Soft()) as demo:
64
+ gr.Markdown(
65
+ """
66
+ # πŸ“¦ RAR to ZIP Converter
67
+ Upload your RAR file and convert it to ZIP format instantly.
68
+ """
69
+ )
70
+
71
+ with gr.Row():
72
+ with gr.Column():
73
+ rar_input = gr.File(
74
+ label="Upload RAR File",
75
+ file_types=[".rar"],
76
+ type="filepath"
77
+ )
78
+ convert_btn = gr.Button("Convert to ZIP", variant="primary", size="lg")
79
+
80
+ with gr.Column():
81
+ status_output = gr.Textbox(
82
+ label="Status",
83
+ placeholder="Upload a RAR file and click Convert...",
84
+ lines=2
85
+ )
86
+ zip_output = gr.File(
87
+ label="Download ZIP File",
88
+ interactive=False
89
+ )
90
+
91
+ gr.Markdown(
92
+ """
93
+ ### πŸ“ Instructions:
94
+ 1. Click "Upload RAR File" and select your .rar file
95
+ 2. Click "Convert to ZIP" button
96
+ 3. Wait for the conversion to complete
97
+ 4. Download your converted ZIP file
98
+
99
+ **Note:** This app requires `rarfile` and `unrar` to be installed.
100
+ On Linux/Mac, you may need to install unrar: `sudo apt-get install unrar` or `brew install unrar`
101
+ """
102
+ )
103
+
104
+ # Connect the conversion function to the button
105
+ convert_btn.click(
106
+ fn=convert_rar_to_zip,
107
+ inputs=rar_input,
108
+ outputs=[zip_output, status_output]
109
+ )
110
+
111
+ # Launch the app
112
+ if __name__ == "__main__":
113
+ demo.launch(share=False)