Spaces:
Sleeping
Sleeping
| 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) |