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