Archive Conversion Basics
SwissKnife can convert between different archive formats by extracting the contents and recompressing them in the target format.
ZIP to TAR.GZ
Convert ZIP archives to compressed TAR.GZ format:
python solution.py convert backup.zip extracted.tar.gz
Expected Output:
Converting: /path/to/backup.zip to /path/to/extracted.tar.gz
Success: Archive conversion successful: /path/to/extracted.tar.gz
Info: Conversion completed in 5.43 seconds.
This extracts all files from backup.zip and creates a new extracted.tar.gz archive containing the same contents.
RAR to 7Z
Convert RAR archives to 7-Zip format:
python solution.py convert files.rar compressed.7z
7-Zip typically provides better compression ratios than RAR or ZIP formats.
Password-Protected Archives
SwissKnife supports extracting and creating password-protected archives using the --password flag.
Convert a password-protected ZIP to TAR.GZ:
python solution.py convert protected.zip extracted.tar.gz --password mypassword
Expected Output:
Converting: /path/to/protected.zip to /path/to/extracted.tar.gz
Password: mypassword
Info: Extracting password-protected archive: /path/to/protected.zip
Success: Archive conversion successful: /path/to/extracted.tar.gz
Info: Conversion completed in 6.78 seconds.
Create Password-Protected Archive
Convert RAR to password-protected 7Z:
python solution.py convert secure.rar backup.7z --password archivepassword
Password Protection Limitations: TAR-based formats (.tar, .tar.gz, .tar.bz2, .tar.xz) and their variants (.gz, .bz2, .xz) do not support password protection. If you specify a password when converting to these formats, the password will be ignored and the archive will be created without encryption.
How Archive Conversion Works
Archive conversion is a two-step process (from solution.py:136-146):
# Step 1: Extract to temporary directory
temp_extract_dir = tempfile.mkdtemp()
patoolib.extract_archive(
work_path,
outdir=temp_extract_dir,
password=password if password else None,
interactive=False
)
# Step 2: Create new archive from extracted files
patoolib.create_archive(
output_abs,
[temp_extract_dir],
password=password if password and output_ext not in (".tar", ".tar.gz", ...) else None
)
The temporary directory is automatically cleaned up after conversion.
SwissKnife supports these archive formats:
- ZIP - Universal cross-platform format
- TAR - Unix tape archive (uncompressed)
- GZ - Gzip compression (often combined with TAR)
- BZ2 - Bzip2 compression (better than Gzip)
- 7Z - 7-Zip format (best compression)
- RAR - Proprietary format (requires external tools)
Common combinations include:
.tar.gz or .tgz - TAR archive with Gzip compression
.tar.bz2 or .tbz2 - TAR archive with Bzip2 compression
.tar.xz - TAR archive with XZ compression
Some archive formats require external command-line tools:
7-Zip (.7z)
Linux/Ubuntu:
sudo apt update
sudo apt install p7zip-full
macOS:
Windows:
- Download from 7-zip.org
- Add installation directory to PATH
RAR (.rar)
Linux/Ubuntu:
sudo apt update
sudo apt install unrar rar
macOS:
Windows:
- Download from rarlab.com
- Extract to folder and add to PATH
ZIP, TAR, GZ, and BZ2 formats work out of the box with Python’s standard library. Only 7Z and RAR require external tools.
While SwissKnife focuses on conversion, you can extract archives to directories using external tools:
# Using Python's zipfile module
python -m zipfile -e archive.zip ./extracted/
# Using tar command (Linux/macOS)
tar -xzf archive.tar.gz -C ./extracted/
# Using 7z command
7z x archive.7z -o./extracted/
Compression Workflows
Create archives from directories:
Create ZIP Archive
# Using Python's zipfile module
python -m zipfile -c archive.zip ./directory/
Create TAR.GZ Archive
# Using tar command (Linux/macOS)
tar -czf archive.tar.gz ./directory/
Create 7Z Archive
# Using 7z command
7z a archive.7z ./directory/
Common Use Cases
Convert platform-specific formats to universal ZIP:
# Convert RAR (Windows) to ZIP (universal)
python solution.py convert windows_backup.rar universal.zip
# Convert TAR.GZ (Linux) to ZIP (universal)
python solution.py convert linux_backup.tar.gz universal.zip
2. Maximum Compression
Convert to 7Z for smallest file size:
# Convert ZIP to 7Z for better compression
python solution.py convert large_backup.zip compressed.7z
Typical compression ratios:
- ZIP → 7Z: 20-40% size reduction
- TAR.GZ → 7Z: 10-25% size reduction
3. Secure Backups
Add password protection to existing archives:
# Convert unprotected ZIP to password-protected 7Z
python solution.py convert backup.zip secure_backup.7z --password mySecurePassword123
4. Remove Password Protection
Extract password-protected archive to unprotected format:
# Extract password-protected RAR to unprotected ZIP
python solution.py convert protected.rar public.zip --password oldPassword
Standardize backup archives to a single format:
# Convert all archives in a directory to TAR.GZ
python solution.py batch-convert ./old_backups ./standardized .zip .tar.gz
python solution.py batch-convert ./old_backups ./standardized .rar .tar.gz
python solution.py batch-convert ./old_backups ./standardized .7z .tar.gz
Best Practices
Choose the right format for your needs:
- ZIP: Best for cross-platform compatibility and quick access
- 7Z: Best for maximum compression and storage efficiency
- TAR.GZ: Best for Unix/Linux systems and version control
- RAR: Use only if required by legacy systems
Use strong passwords: When creating password-protected archives, use passwords with:
- At least 12 characters
- Mix of uppercase, lowercase, numbers, and symbols
- No dictionary words or personal information
Backup before converting: Archive conversion involves extraction and re-compression. Always keep the original archive until you verify the converted version.
Preserve originals with —preserve-original: Use this flag to ensure the original archive remains unchanged during conversion:python solution.py convert backup.zip backup.7z --preserve-original
Conversion Speed
- Small archives (<100MB): 2-10 seconds
- Medium archives (100MB-1GB): 10-60 seconds
- Large archives (>1GB): Several minutes
Speed depends on:
- Archive size and file count
- Source and target compression formats
- CPU speed and available RAM
- Disk I/O performance
Compression Ratios
Typical file size comparisons:
| Format | Relative Size | Speed |
|---|
| TAR (uncompressed) | 100% | Fastest |
| ZIP | 60-80% | Fast |
| TAR.GZ | 50-70% | Medium |
| TAR.BZ2 | 45-65% | Slower |
| 7Z | 40-60% | Slowest |
| RAR | 45-65% | Medium |
Temporary Disk Space
Archive conversion requires temporary disk space equal to the uncompressed size of the archive. Ensure you have sufficient free space before converting large archives.
For example:
- 1GB ZIP containing 5GB of data needs 5GB free space during conversion
- Temporary files are stored in system temp directory
- Automatically cleaned up after conversion completes
Troubleshooting
Error: “7z command not found”
Solution: Install p7zip:
# Linux
sudo apt install p7zip-full
# macOS
brew install p7zip
Error: “unrar command not found”
Solution: Install unrar:
# Linux
sudo apt install unrar
# macOS
brew install unrar
Error: “Incorrect password”
Solution: Verify the password is correct and try again:
python solution.py convert protected.zip output.tar.gz --password correctPassword
Error: “No space left on device”
Solution: Free up disk space or use a different temporary directory with more space. The conversion needs space equal to the uncompressed archive size.