File size: 1,364 Bytes
eb2aea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import aspose.zip as az
from io import BytesIO
import tempfile
import os
import gradio as gr

def rar_to_zip(rar_file_path):
    # Create a temporary ZIP file
    temp_zip = tempfile.NamedTemporaryFile(suffix='.zip', delete=False)
    temp_zip_path = temp_zip.name
    temp_zip.close()

    # Create ZIP archive
    with az.Archive() as zip_archive:
        # Load RAR file
        with az.rar.RarArchive(rar_file_path) as rar_archive:
            # Loop through entries and add to ZIP
            for i in range(rar_archive.entries.length):
                if not rar_archive.entries[i].is_directory:
                    ms = BytesIO()
                    rar_archive.entries[i].extract(ms)
                    zip_archive.create_entry(rar_archive.entries[i].name, ms)
        # Save ZIP archive
        zip_archive.save(temp_zip_path)

    return temp_zip_path

def convert_rar_to_zip(rar_file):
    if rar_file is None:
        return None
    zip_path = rar_to_zip(rar_file)
    return zip_path

with gr.Blocks() as demo:
    gr.Markdown("## RAR to ZIP Converter")
    file_input = gr.File(label="Upload RAR File", file_types=[".rar"])
    file_output = gr.File(label="Download ZIP File")
    convert_button = gr.Button("Convert")
    convert_button.click(
        convert_rar_to_zip,
        inputs=file_input,
        outputs=file_output
    )

demo.launch()