smihica / pyminizip

To create a password encrypted zip file in python.
Other
108 stars 38 forks source link

cannot unzip in oop #48

Open USER4247 opened 1 year ago

USER4247 commented 1 year ago

While unzipping in loop , it throws error , here is code import os import pyminizip from zipfile import ZipFile

def split_file(input_file, output_folder, chunk_size, password): password_bytes = password.encode('utf-8') # Encode the password as bytes

# Ensure the output folder exists
os.makedirs(output_folder, exist_ok=True)

part_number = 1

with open(input_file, 'rb') as infile:
    while True:
        chunk = infile.read(chunk_size)
        if not chunk:
            break

        # Get the original file extension
        file_extension = os.path.splitext(input_file)[-1]

        # Create the part filename with the original extension
        part_filename = os.path.join(output_folder, f'part{part_number:04}.zip')

        # Write the chunk to a temporary file
        temp_filename = 'temp_file.dat'
        with open(temp_filename, 'wb') as temp_file:
            temp_file.write(chunk)

        # Create a password-encrypted ZIP file using pyminizip
        pyminizip.compress(temp_filename, None, part_filename, password, 0)

        # Remove the temporary file
        os.remove(temp_filename)

        part_number += 1

def join_parts(part_files, output_path, password): password_bytes = password.encode('utf-8') # Encode the password as bytes

# Create a temporary directory for extraction
extraction_dir = 'temp_extraction'
os.makedirs(extraction_dir, exist_ok=True)

try:
    for index, part_file in enumerate(part_files):
        print(f'Extracting {part_file}...')

        # Extract the ZIP file using pyminizip into the temporary directory
        print(os.path.exists(part_file) , 'FILE EXIST')
        pyminizip.uncompress(part_file, password, extraction_dir, 0)

        extracted_file_path = os.path.join(extraction_dir, 'data')

        # Check if the extracted file exists
        if os.path.exists(extracted_file_path):
            # Read the content of the extracted file
            with open(extracted_file_path, 'rb') as extracted_file:
                part_data = extracted_file.read()

            # Write the extracted data to the output file
            with open(output_path, 'ab') as output_file:
                output_file.write(part_data)
        else:
            print(f'Error: Extracted file not found for {part_file}')

finally:
    pass

print(f'File joined successfully.')

def test_run(): input_file = 'sample.mp4' # Replace with the path to your input file output_folder = 'output_parts' # Folder to store split parts chunk_size = 1 1024 1024 # 5 MB password = 'your_password' # Replace with your desired password

# Ensure the output folder exists
os.makedirs(output_folder, exist_ok=True)

# Specify the index to resume splitting from (e.g., None or 20,000,000 bytes)
start_index = None  # Change to the desired start index, or leave as None to start from the beginning

# Specify the byte progress for resuming (e.g., None or 2,000,000 bytes)
byte_progress = None  # Change to the desired byte progress, or leave as None to start from the beginning

# Split the file into parts, optionally resuming from a specific index and byte progress
split_file(input_file, output_folder, chunk_size, password)

# List of part files (you can modify this list as needed)
part_files = sorted([
    os.path.join(output_folder, filename)
    for filename in os.listdir(output_folder)
    if filename.startswith('part') and filename.endswith('.zip')
])

# Specify the output file for joining the parts
output_file = 'output_combined.mp4'  # Replace with your desired output file name

# Specify the index to resume joining from (e.g., None or 2)
start_index = None  # Change to the desired start index, or leave as None to start from the beginning

# Specify the byte progress for resuming (e.g., None or 2,000,000 bytes)
byte_progress = None  # Change to the desired byte progress, or leave as None to start from the beginning

# Join the parts to recreate the original file, optionally resuming from a specific index and byte progress
join_parts(part_files, output_file, password)

print(f'File split into {len(part_files)} parts and then joined successfully.')

Call the test_run function to execute the operations

if name == 'main': test_run()

test_run()