2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

01/27/2026: Create xz File Using Python


Goal

Create xz File Using Python

Code

#!/usr/bin/env python3
"""Create XZ-compressed files containing a text file with one sentence.

Note: XZ (.xz) is a compression format, not an archive format, and it does not
support native password protection. For encrypted archives, prefer 7z or RAR.
"""

import lzma
import glob


def create_xz_file(output_file="archive.xz", sentence="Hello, this is a test sentence.", password=None):
    """
    Create an XZ-compressed file containing a text file with the specified sentence.

    Args:
        output_file: Path to the output .xz file
        sentence: The sentence to write in the text file
        password: Optional password (not supported by XZ; will be ignored with a warning)
    """
    if password:
        print("⚠ Warning: XZ format does not support native password protection.")
        print("  Use 7z or RAR for encrypted archives.")

    # Create file content
    file_content = sentence.encode("utf-8")

    # Compress with XZ (LZMA) at a reasonable preset
    compressed_content = lzma.compress(file_content, preset=6)

    # Write compressed bytes to disk
    with open(output_file, "wb") as f:
        f.write(compressed_content)

    print(f"✓ Created XZ-compressed file: {output_file}")


def is_password_protected(filepath):
    """
    Check if an XZ file is password-protected.

    Args:
        filepath: Path to the .xz file

    Returns:
        False - XZ format does not support password protection
    """
    return False


def decompress_xz(filepath):
    """
    Decompress an XZ file to verify its contents.

    Args:
        filepath: Path to the .xz file

    Returns:
        Decompressed content as a string, or None on error
    """
    try:
        with lzma.open(filepath, "rt", encoding="utf-8") as f:
            return f.read()
    except Exception as e:
        print(f"Error decompressing {filepath}: {e}")
        return None


if __name__ == "__main__":
    # Create unencrypted file
    create_xz_file(output_file="archive.xz")

    print("Note: XZ format does not support password protection.")

    # Check all XZ files in current directory
    print("\nChecking XZ files:")
    for filepath in sorted(glob.glob("*.xz")):
        is_protected = is_password_protected(filepath)
        status = "🔒 Password-protected" if is_protected else "🔓 Unprotected"
        print(f"  {filepath}: {status}")

        content = decompress_xz(filepath)
        if content is not None:
            print(f"    Content: {content}")

01/27/2026: Create rar File Using Python


Goal

Create rar File Using Python

Code

#!/usr/bin/env python3
"""Create RAR archives containing a text file with one sentence.

Note: RAR archive creation requires the 'rar' command-line tool (from WinRAR).
This script uses the rarfile library for reading and analyzing RAR files,
and attempts to create them via the command-line tool if available.
"""

import rarfile
import os
import glob
import subprocess
import tempfile


def create_rar_archive(output_file="archive.rar", sentence="Hello, this is a test sentence.", password=None):
    """
    Create a RAR archive containing a text file with the specified sentence.
    
    Requires the 'rar' command-line tool to be installed.
    On Linux, you can install it with: sudo apt-get install rar
    On macOS: brew install rar
    On Windows: Install WinRAR
    
    Args:
        output_file: Path to the output RAR file
        sentence: The sentence to write in the text file
        password: Optional password to encrypt the archive
    """
    # Use a temporary file to create the RAR archive
    with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
        temp_file.write(sentence)
        temp_filename = temp_file.name
    
    try:
        # Build rar command
        cmd = ['rar', 'a', '-ep1']  # -ep1 excludes the base folder
        if password:
            cmd.extend(['-p' + password])
        cmd.extend([output_file, temp_filename])
        
        # Try to create RAR using command line
        try:
            result = subprocess.run(cmd, check=True, capture_output=True, text=True)
            if password:
                print(f"✓ Created password-protected RAR archive: {output_file}")
            else:
                print(f"✓ Created RAR archive: {output_file}")
        except FileNotFoundError:
            print(f"✗ Error: 'rar' command not found.")
            print(f"  Please install RAR to create RAR archives:")
            print(f"    - Linux: sudo apt-get install rar")
            print(f"    - macOS: brew install rar")
            print(f"    - Windows: Install WinRAR")
        except subprocess.CalledProcessError as e:
            print(f"✗ Error creating RAR archive: {e.stderr}")
    finally:
        # Clean up temporary file
        if os.path.exists(temp_filename):
            os.remove(temp_filename)


def is_password_protected(filepath):
    """
    Check if a RAR file is password-protected.
    
    Args:
        filepath: Path to the RAR file
        
    Returns:
        True if the archive is password-protected, False otherwise
    """
    try:
        with rarfile.RarFile(filepath, "r") as archive:
            # Use the built-in needs_password method
            return archive.needs_password()
    except Exception as e:
        print(f"Error checking {filepath}: {e}")
        return False


if __name__ == "__main__":
    # Create unencrypted archive
    create_rar_archive(output_file="archive.rar")
    
    # Create password-protected archive
    create_rar_archive(output_file="archive_protected.rar", password="secure123")
    
    # Check all RAR files in current directory
    print("\nChecking RAR files for password protection:")
    for filepath in sorted(glob.glob("*.rar")):
        is_protected = is_password_protected(filepath)
        status = "🔒 Password-protected" if is_protected else "🔓 Unprotected"
        print(f"  {filepath}: {status}")