# nsz - Nintendo Switch Compression Tool **NSZ/XCZ Format Support** - Compress Nintendo Switch games by 20-30% ## Overview `nsz` is a Python-based compression tool for Nintendo Switch game files. It compresses NSP (Nintendo Submission Package) and XCI (NX Card Image) files into NSZ and XCZ formats respectively, achieving 20-30% space savings while maintaining compatibility with homebrew tools. **Key Feature**: Lossless compression that works with Atmosphere custom firmware and other homebrew loaders. ## Installation Installed via pip3: `nsz` v4.6.1 Location: `~/.local/bin/nsz` Dependencies (auto-installed): - pycryptodome (encryption support) - zstandard (compression) - enlighten (progress bars) ## Important: Keys Required! NSZ requires Nintendo Switch cryptographic keys to compress/decompress files. ### Keys Setup **Keys Location (one of):** - `~/.local/bin/keys.txt` - `~/.switch/prod.keys` **How to Get Keys:** 1. Use Lockpick_RCM on your Switch: https://github.com/shchmue/Lockpick_RCM/releases 2. Dump keys from your own Switch (legal requirement!) 3. Copy `prod.keys` to one of the locations above **Example keys.txt format:** ``` header_key = [32-character hex] key_area_key_application_00 = [32-character hex] ... ``` ⚠️ **Legal Note**: You must dump keys from your own Nintendo Switch. Sharing or downloading keys from others is illegal. ## Supported Formats ### Input Formats - **NSP**: Nintendo Submission Package (eShop downloads, installed games) - **XCI**: NX Card Image (game card dumps) ### Output Formats - **NSZ**: Compressed NSP (20-30% smaller) - **XCZ**: Compressed XCI (20-30% smaller) ### Decompression - NSZ → NSP - XCZ → XCI ## Format Comparison | Format | Size | Compatibility | Notes | |--------|------|---------------|-------| | NSP | 100% | Universal | Standard format, works everywhere | | XCI | 100% | Universal | Game card dumps, works everywhere | | NSZ | 70-80% | Homebrew only | Compressed NSP, Atmosphere/Tinfoil | | XCZ | 70-80% | Homebrew only | Compressed XCI, SX OS/Tinfoil | **Space Savings Example** (100 Switch games @ 3GB average): - Original NSP: 300 GB - Compressed NSZ: 210-240 GB - **Saved: 60-90 GB (20-30%)** ## Basic Usage ### Compress NSP to NSZ ```bash # Single file nsz -C file.nsp # Output: file.nsz (20-30% smaller) # Multiple files nsz -C game1.nsp game2.nsp game3.nsp # Entire directory nsz -C /path/to/nsp/folder/ ``` ### Compress XCI to XCZ ```bash # Single file nsz -C file.xci # Output: file.xcz (20-30% smaller) ``` ### Decompress NSZ to NSP ```bash # Decompress NSZ nsz -D file.nsz # Output: file.nsp (original size) # Decompress XCZ nsz -D file.xcz # Output: file.xci ``` ## Advanced Options ### Compression Level ```bash # Maximum compression (slower, ~5% more savings) nsz -C -l 22 file.nsp # Fast compression (faster, slightly less savings) nsz -C -l 18 file.nsp # Default: level 18 (good balance) ``` ### Compression Settings ```bash # Compress with specific settings nsz -C \\ -l 22 \\ # Compression level (18-22) -bs 262144 \\ # Block size (default: 262144) -t 4 \\ # Threads (default: CPU count) -o output/ \\ # Output directory file.nsp # Verify after compression nsz -C -V file.nsp # Verify compressed file # Keep original file nsz -C -k file.nsp # Don't delete source ``` ### Batch Processing ```bash # Compress all NSP files in directory nsz -C /path/to/games/*.nsp # Compress with progress nsz -C -p /path/to/games/ # Multi-threaded batch nsz -C -t 8 /path/to/games/*.nsp ``` ### Filtering ```bash # Only compress base games (no updates/DLC) nsz -C --level base /path/to/games/ # Only compress updates nsz -C --level update /path/to/games/ # Only compress DLC nsz -C --level dlc /path/to/games/ ``` ## Command Reference ### Main Options - `-C, --compress`: Compress NSP/XCI to NSZ/XCZ - `-D, --decompress`: Decompress NSZ/XCZ to NSP/XCI - `-l LEVEL, --level`: Compression level (1-22, default: 18) - `-t THREADS, --threads`: Number of threads (default: CPU count) - `-o DIR, --output`: Output directory - `-V, --verify`: Verify after compression/decompression - `-k, --keep`: Keep original files (don't delete) - `-p, --progress`: Show detailed progress ### Compression Options - `-bs SIZE, --block-size`: Block size in bytes (default: 262144) - `--solid`: Use solid compression (better ratio, slower) - `--parsable`: Output parsable for scripts ### Filtering Options - `--level base`: Only process base games - `--level update`: Only process updates - `--level dlc`: Only process DLC ### Information Options - `-i, --info`: Show file information without processing - `--titlekeys`: Use title keys database ## Performance Notes ### Compression Speed (typical 3GB NSP) - **Level 18**: 2-4 minutes (fast, 20-25% savings) - **Level 22**: 4-8 minutes (slower, 25-30% savings) ### Memory Usage - **Compression**: ~500 MB - 1 GB RAM - **Decompression**: ~200 MB RAM ### CPU Usage - **Multi-threaded**: Uses all specified cores - **Default**: Uses all available CPU cores - **Recommendation**: Leave 1-2 cores free for system ## File Structure ### NSP Format NSP files contain: - NCA files (Nintendo Content Archive) - game data - CNMT (Content Meta) - metadata - XML files - certificates/tickets ### NSZ Compression NSZ compresses each NCA individually: - Smaller NCAs: Full compression - Large NCAs: Block-level compression - Headers: Uncompressed (for fast access) ## Compatibility ### Homebrew Loaders (NSZ/XCZ Support) - ✅ **Atmosphere**: Full support with Tinfoil - ✅ **Tinfoil**: Native NSZ installer - ✅ **Awoo Installer**: NSZ support - ✅ **Goldleaf**: With NSZ plugin - ❌ **Official firmware**: Not supported (decompress first) ### Emulators - **Yuzu**: NSP only (decompress NSZ → NSP) - **Ryujinx**: NSP/XCI only (decompress first) ### Game Types - ✅ **Base games**: Fully supported - ✅ **Updates**: Fully supported - ✅ **DLC**: Fully supported - ✅ **Retro games**: Fully supported - ✅ **Multi-game collections**: Supported ## Integration with ROM Farmer ### Transformation Tracking NSZ compression can be tracked in the transformation database: ```python # Track NSP → NSZ transformation db.record_transformation( source_file="game.nsp", destination_file="game.nsz", transformation_type="compress", tool="nsz", tool_version="4.6.1", source_format="nsp", dest_format="nsz", metadata={ "compression_level": 22, "block_size": 262144, "solid_compression": False } ) # Query transformation chain chain = db.get_transformation_chain("game.nsz") # Reverse transformation (NSZ → NSP) if emulator_needs_nsp: nsz_decompress(nsz_file, nsp_file) ``` ### ScreenScraper Hash Lookup NSZ files need to be temporarily decompressed for hash matching: ```python def get_nsp_hash_from_nsz(nsz_file): # Decompress NSZ → temporary NSP temp_nsp = tempfile.mktemp(suffix=".nsp") nsz_decompress(nsz_file, temp_nsp) # Calculate hash hash_value = calculate_hash(temp_nsp, algorithm="sha256") # Cleanup os.remove(temp_nsp) return hash_value ``` ## Batch Processing Scripts ### Compress All NSP Files ```bash #!/bin/bash # Compress all NSP files in directory to NSZ for nsp in *.nsp; do output="${nsp%.nsp}.nsz" echo "Compressing $nsp → $output..." nsz -C -l 22 -k "$nsp" # Verify compression if [ -f "$output" ]; then echo "✓ Created: $output" original_size=$(stat -f%z "$nsp" 2>/dev/null || stat -c%s "$nsp") compressed_size=$(stat -f%z "$output" 2>/dev/null || stat -c%s "$output") savings=$((100 - (compressed_size * 100 / original_size))) echo " Space saved: ${savings}%" # Optional: Remove original NSP to save space # rm "$nsp" else echo "✗ Compression failed: $nsp" fi done ``` ### Parallel Compression ```bash #!/bin/bash # Compress NSP files in parallel (requires GNU parallel) find . -name "*.nsp" | parallel -j 4 \\ 'nsz -C -l 22 -k {} && \\ echo "✓ Compressed: {}"' ``` ### Calculate Space Savings ```bash #!/bin/bash # Calculate total space saved after NSZ compression nsp_size=$(du -shc *.nsp 2>/dev/null | tail -1 | cut -f1) nsz_size=$(du -shc *.nsz 2>/dev/null | tail -1 | cut -f1) echo "Original NSP: $nsp_size" echo "Compressed NSZ: $nsz_size" ``` ### Decompress for Emulator ```bash #!/bin/bash # Decompress NSZ files for emulator use for nsz in *.nsz; do nsp="${nsz%.nsz}.nsp" echo "Decompressing $nsz → $nsp..." nsz -D "$nsz" if [ -f "$nsp" ]; then echo "✓ Decompressed: $nsp" else echo "✗ Decompression failed: $nsz" fi done ``` ## Troubleshooting ### "keys.txt or prod.keys not found" **Solution**: Dump keys from your Switch using Lockpick_RCM: 1. Download Lockpick_RCM: https://github.com/shchmue/Lockpick_RCM/releases 2. Boot into RCM mode on your Switch 3. Run Lockpick_RCM 4. Copy `prod.keys` to `~/.switch/prod.keys` or `~/.local/bin/keys.txt` ### "Invalid NCM magic" **Problem**: Corrupted or invalid NSP/XCI file **Solution**: - Verify source file integrity - Re-download or re-dump from cartridge - Try different source ### "Compression failed" **Problem**: Not enough disk space or memory **Solution**: - Check available disk space (need 2x file size) - Check RAM (need ~1GB) - Try lower compression level: `-l 18` ### NSZ won't install on Switch **Problem**: Corrupted NSZ or missing keys during compression **Solution**: - Decompress NSZ → NSP - Verify NSP integrity - Re-compress with verification: `nsz -C -V file.nsp` ### Slow compression speed **Problem**: Too many threads or high compression level **Solution**: - Use fewer threads: `-t 4` - Use faster level: `-l 18` instead of `-l 22` - Disable solid compression ## Best Practices ### Compression Strategy 1. **Archive storage**: Use level 22 for maximum savings 2. **Frequent use**: Use level 18 for faster access 3. **Keep originals**: Use `-k` flag until verified 4. **Verify**: Always use `-V` for important files ### Organization ``` /switch/ ├── nsp/ # Original NSP files (for emulators) ├── nsz/ # Compressed NSZ (for Atmosphere/Tinfoil) ├── updates/ # Separate updates └── dlc/ # Separate DLC ``` ### Backup Strategy 1. Compress to NSZ for storage 2. Keep one copy as NSP for emulators 3. Verify NSZ can decompress before deleting NSP 4. Test NSZ on actual Switch before archiving ## Comparison with Other Switch Tools | Tool | Purpose | Compression | Speed | |------|---------|-------------|-------| | **nsz** | NSP/XCI compression | 20-30% | Good | | 4NXCI | XCI → NSP conversion | 0% | Fast | | nut | Title downloader | N/A | N/A | | Tinfoil | Game installer | N/A | N/A | ## References - **NSZ GitHub**: https://github.com/nicoboss/nsz - **Lockpick_RCM**: https://github.com/shchmue/Lockpick_RCM/releases - **Atmosphere CFW**: https://github.com/Atmosphere-NX/Atmosphere - **Tinfoil**: https://tinfoil.io/ - **ROM Farmer**: [Local documentation](../../README.md) ## Legal Notice ⚠️ **Important**: - NSZ requires cryptographic keys from your own Nintendo Switch - Downloading or sharing keys from others is illegal - Only compress games you legally own - Respect Nintendo's intellectual property ## Version Installed via pip3: v4.6.1 Python package: `nsz` Location: `~/.local/bin/nsz` Install date: 2025-10-12