Overview

Aspose.ZIP facilitates a range of encryption and decryption operations for archives. Using Aspose.Zip API for Python.NET, you can efficiently encrypt and decrypt ZIP, RAR, and 7-Zip archives.

Encrypt ZIP files

Two encryption techniques can be utilized for ZIP archives: the older, now considered less secure traditional method, and the more robust modern AES encryption. The ZIP standard permits the encryption of individual entries within the same archive using either method. AES encryption, known for its stronger security, supports various key lengths, including 128-bit, 192-bit, and 256-bit, offering enhanced protection for sensitive data within ZIP archives. Each entry in the same archive may be encrypted with own encryption, so you can mix different encryption types in the same archive.

Unlock ZIP with Deflate method

    with open("archive.zip", 'x') as zip_file:
        with open("alice29.txt") as source:
            method =  zp.saving.CompressionSettings.deflate
            encryption = zp.saving.TraditionalEncryptionSettings("p@s$S")
            archive = zp.Archive(zp.saving.ArchiveEntrySettings(method, encryption))
            archive.create_entry("alice29.txt", source)
            archive.save(zip_file)

The code creates a new ZIP file named archive.zip, compresses the content of alice29.txt using the Deflate compression method, encrypts it with the traditional ZIP encryption method using the password "p@s$S", and saves the compressed and encrypted file to the newly created ZIP archive.

Encrypt ZIP with AES

    with open("archive.zip", 'x') as zip_file:
        with open("alice29.txt") as source:
            method =  zp.saving.CompressionSettings.deflate
            encryption = zp.saving.AesEcryptionSettings("p@s$S", zp.saving.EncryptionMethod.AES256)
            archive = zp.Archive(zp.saving.ArchiveEntrySettings(method, encryption))
            archive.create_entry("alice29.txt", source)
            archive.save(zip_file)

The provided code demonstrates how to create a ZIP archive named archive.zip, compress a file alice29.txt using the Deflate compression method, and encrypt it with AES-256 encryption using the password "p@s$S". For these samples we provided instances of EncryptionSetting within corresponding Archive constructor . It is possible to apply encryption for particular entry leaving other unprotected.
This showcases the flexibility of applying encryption to specific entries within a ZIP archive, allowing different files to have distinct levels of protection. This approach allows for a more granular and customized security strategy, where individual files or groups of files can have distinct levels of protection. However, it’s important to note that this practice may complicate the management and decryption process, and compatibility with various ZIP archive tools could vary.

Encrypt ZIP the second entry of three

    with open("archive.zip", 'x') as zip_file:
        encrypted_entry = zp.saving.ArchiveEntrySettings(None, zp.saving.AesEcryptionSettings("p@s$", zp.saving.EncryptionMethod.AES256))
        with zp.Archive as archive:
            archive.create_entry("alice29.txt", "alice29.txt")
            archive.create_entry("asyoulik.txt", "asyoulik.txt", false, encrypted_entry)
            archive.create_entry("fields.c", "fields.c")
            archive.save(zip_file)

This code demonstrates creating a password-protected ZIP archive in Python. It adds three files: alice29.txt, asyoulik.txt, and fields.c.
Here’s how it works:
Opens or creates a file for the ZIP archive based on the name stored in this.resultFile. Creates FileInfo objects to represent the files to be added. Creates a new archive object.
Adds files: alice29.txt and fields.c are added to the archive without any special settings. asyoulik.txt is added with AES-256 encryption, requiring the password p@s$ for decryption. Saves the completed archive to the specified file.

Encrypt ZIP with different methods and passwords

    with open("archive.zip", 'x') as zip_file:
        encrypted_1 = zp.saving.ArchiveEntrySettings(None, zp.saving.AesEcryptionSettings("p@s$1", zp.saving.EncryptionMethod.AES256))
        encrypted_2 = zp.saving.ArchiveEntrySettings(None, zp.saving.TraditionalEncryptionSettings("p@s$2"))
        encrypted_3 = zp.saving.ArchiveEntrySettings(None, zp.saving.AesEcryptionSettings("p@s$3", zp.saving.EncryptionMethod.AES128))
        with zp.Archive as archive:
            archive.create_entry("alice29.txt", "alice29.txt", false, encrypted_1)
            archive.create_entry ("asyoulik.txt", "asyoulik.txt", false, encrypted_2)
            archive.create_entry("fields.c", "fields.c", false, encrypted_3)
            archive.save(zip_file)

This code snippet demonstrates creating a ZIP archive in Python where individual files are encrypted with different methods and passwords. Here’s how its work:
File Setup: Opens or creates a file for the ZIP archive based on the name stored in this.resultFile. Creates FileInfo objects representing the files to be added: alice29.txt, asyoulik.txt, and fields.c.
Creating the Archive: Creates a new archive object (archive).
Adding Encrypted Files: The code uses a loop to iterate through the files and add them to the archive with specific encryption settings:
alice29.txt - This file is added with AES-256 encryption and requires the password "p@s" for decryption.
asyoulik.txt - This file is added with a different encryption method called Traditional Encryption. It also uses a separate password, "p@s", for decryption.
fields.c - This file is added with AES-128 encryption again, but with a different password, "p@s$3", for decryption.
Saving the Archive: Finally, the code saves the completed archive containing the encrypted files to the specified file.

Encrypt ZIP Archive with different methods and passwords

    with zp.Archive("plain.zip") as archive:
        save_options = zp.saving.ArchiveSaveOptions()
        save_options.encryption_options = zp.saving.AesEcryptionSettings("p@s$", zp.saving.EncryptionMethod.AES256)
        archive.save("encrypted.zip",  save_options)

This code takes an existing ZIP archive plain.zip, encrypts its contents using AES-256 with the password p@s$, and saves the encrypted version as a new archive named encrypted.zip.

Decrypt ZIP files

Decrypt ZIP File With Password

    with zp.Archive("source.zip") as archive:
        archive.entries[0].extract("first_entry.bin", "p@s$")

The user has the option to decrypt either a specific entry within the encrypted ZIP archive. This code snippet demonstrates a basic approach to decrypting the first entry from a password-protected ZIP archive. If the password is correct the archive opens successfully.

Decrypt Whole ZIP File

    load_options = zp.ArchiveLoadOptions()
    load_options.decryption_password = "p@s$S"
    with zp.Archive("source.zip", load_options) as archive:
        archive.extract_to_directory("destination")

This simple code takes a password-protected ZIP archive "source.zip", uses the provided password "p@s$S" for decryption, and extracts all its contents to a designated folder "destination".

Decrypt RAR file

Aspose.ZIP does not support RAR archive composition, it only supports extraction. RAR archive can be encrypted with or without protecting file names.
In the former case user need to provide decryption password within RarArchiveLoadOptions in the very beginning of RAR archive instantiation. In the latter case, it is possible to pass password later on entry extraction.

Decrypt whole RAR archive with encrypted file names

    load_options = zp.rar.RarArchiveLoadOptions()
    load_options.decryption_password = "p@s$S"
    with zp.rar.RarArchive("source.rar", load_options) as archive:
        archive.extract_to_directory("destination")

Essentially, this code functions similarly to the ZIP example but works specifically for RAR archives. It takes a password-protected RAR archive "source.rar", uses the provided password "p@s$S" for decryption, and extracts all its contents to a designated folder "destination".

Decrypt particular entry from RAR archive

    with zp.rar.RarArchive("source.rar") as archive:
        archive.entries[0].extract("first_entry.bin", "p@s$S")

This code opens the source.rar RAR archive, decrypts and extracts the first entry of the archive using the password "p@s$S" and saves it to the first_entry.bin file. This sample would work if only content of entries is encrypted but file names are not.

Decrypt particular entry from RAR archive as a stream

    with io.FileIO("file.bin", "xb") as destination:
        with zp.rar.RarArchive("source.rar") as archive:
            with archive.entries[0].open("p@s$S") as source:
                destination.write(source.readall())

This code opens the RAR archive source.rar, decrypts the second record of the archive using the password "p@s$S", and reads its contents into a ByteArrayOutputStream. By using a read buffer, the code ensures gradual data transfer, which helps handle large files efficiently. This sample would work if only content of entries is encrypted but file names are not. In this sample entry bytes are being copied to the memory stream.

7zip encryption with aes 256

The only option of 7z archive encryption is AES . The archive utilizes LZMA2 compression for efficient file size reduction and AES encryption with the password for added security. The code iterates through the files and adds them to the archive using the CreateEntry method.

Encrypt and Compress whole 7zip archive

    with open("archive.7z", 'xb') as sz_file:
        compression = zp.saving.SevenZipLZMA2CompressionSettings()
        encryption = zp.saving.SevenZipAESEncryptionSettings("p@s$S")
        entry_settings = zp.saving.SevenZipEntrySettings(compression, encryption) 
        with zp.sevenzip.SevenZipArchive(entry_settings) as archive:
            archive.create_entry("alice29.txt", "alice29.txt")
            archive.create_entry("asyoulik.txt", "asyoulik.txt")
            archive.create_entry("fields.c", "fields.c")
            archive.save(sz_file)

This code creates an encrypted 7z archive containing three files: alice29.txt, asyoulik.txt, and fields.c. The archive uses LZMA2 compression and AES encryption with the password "p@s$S". Files are added to the archive using the CreateEntry method, and the archive itself is saved to the created archive.7z file.

7z Encrypt with Password

    with open("archive.7z", 'xb') as sz_file:
        compression = zp.saving.SevenZipLZMA2CompressionSettings()
        entry_pass1 = zp.saving.SevenZipEntrySettings(compression, zp.saving.SevenZipAESEncryptionSettings("p@s$S"))
        entry_pass2 = zp.saving.SevenZipEntrySettings(compression, zp.saving.SevenZipAESEncryptionSettings("$ecret"))
        with zp.sevenzip.SevenZipArchive(entry_settings) as archive:
            archive.create_entry("alice29.txt", "alice29.txt")
            archive.create_entry("asyoulik.txt", "asyoulik.txt", false, entry_pass1)
            archive.create_entry("fields.c", "fields.c", false, entry_pass2)
            archive.save(sz_file)

This code creates an encrypted 7z archive containing three files: alice29.txt, asyoulik.txt, and fields.c. The first file is added to the archive without special settings. The second file is attached using LZMA2 compression and AES encryption with the password "p@s$S". The third file is attached using LZMA2 compression and AES encryption with the password "$ecret". Finally, the archive is saved to the archive.7z file.
Similar as ZIP, user can decrypt either whole archive or particular entry from it. 7Z archive can be encrypted with or without protecting file names. If file names are encrypted, you need to provide password on archive instantiation .

Decrypt 7ZIP files

Decrypt 7z archive with encrypted file names

    with zp.sevenzip.SevenZipArchive("archive.7z", "p@s$S") as archive: 
        archive.extract_to_directory("c:\extracted")

Whether file names are encrypted or not, it is safe to provide password within extracting constructor. The below sample will only work when only content is protected.

Decrypt 7zip particular entry

with zp.sevenzip.SevenZipArchive("archive.7z") as archive:
    archive.entries[0].extract("data.bin", "p@s$S")

Whether file names are encrypted or not, it is safe to provide password within extracting constructor. The below sample will only work when only content is protected.

Other Supported Aspose.ZIP for Python.Net API Features

With the Aspose.ZIP library in Python.Net, you can seamlessly handle various tasks related to ZIP file documents. Whether you need to convert formats, merge files, edit contents, or extract data, Aspose.ZIP provides comprehensive functionality to streamline your workflow and enhance productivity.

  

Support and Learning Resources

  
  

Aspose.Zip offers individual archive processing APIs for other popular development environments, listed below: