Spaces:
Sleeping
Sleeping
File size: 2,030 Bytes
4942148 bf835f3 4942148 bf835f3 4942148 bf835f3 4942148 26297ba 4942148 bf835f3 4942148 bf835f3 4942148 bf835f3 4942148 bf835f3 4942148 bf835f3 |
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 |
import gradio as gr
import rarfile
import pyzipper
from io import BytesIO
def convert_rar_to_zip(rar_file, password=None, progress=gr.Progress()):
"""Convert password-protected RAR to encrypted ZIP with progress tracking"""
try:
progress(0, desc="π Initializing conversion...")
# Corrected password parameter
with rarfile.RarFile(rar_file.name,
password=password.encode() if password else None) as rar:
total_files = len(rar.infolist())
zip_buffer = BytesIO()
with pyzipper.AESZipFile(zip_buffer, 'w', compression=pyzipper.ZIP_LZMA) as zipf:
if password:
zipf.setpassword(password.encode())
for i, file_info in enumerate(progress.tqdm(rar.infolist(), desc="Processing")):
if file_info.is_dir():
zipf.writestr(file_info.filename + '/', '')
else:
with rar.open(file_info) as file:
content = file.read()
zipf.writestr(file_info.filename, content,
compress_type=pyzipper.ZIP_LZMA,
encrypt=bool(password))
progress((i+1)/total_files, desc=f"π Processed {i+1}/{total_files} files")
zip_buffer.seek(0)
return ("converted.zip", zip_buffer.read())
except Exception as e:
return f"β Error: {str(e)}"
app = gr.Interface(
fn=convert_rar_to_zip,
inputs=[
gr.File(label="Upload RAR", file_types=[".rar"]),
gr.Textbox(label="Password (optional)", type="password")
],
outputs=gr.File(label="Download Encrypted ZIP"),
title="Secure RAR to ZIP Converter",
description="Convert password-protected RAR archives to AES-256 encrypted ZIP files",
allow_flagging="never"
)
app.launch() |