XZ XZ compressed data

AI-powered detection and analysis of XZ compressed data files.

📂 Archive
🏷️ .xz
🎯 application/x-xz
🔍

Instant XZ File Detection

Use our advanced AI-powered tool to instantly detect and analyze XZ compressed data files with precision and speed.

File Information

File Description

XZ compressed data

Category

Archive

Extensions

.xz

MIME Type

application/x-xz

XZ Compressed Data Format

Overview

XZ is a general-purpose data compression format that uses the LZMA2 compression algorithm. It provides excellent compression ratios while maintaining reasonable compression and decompression speeds, making it popular for software distribution and archival purposes.

Technical Specifications

  • Format Type: Data compression format
  • File Extension: .xz
  • MIME Type: application/x-xz
  • Compression Algorithm: LZMA2 (default), LZMA
  • Block Size: Configurable (default 8MB)
  • CRC: CRC32 or CRC64 integrity checking
  • Maximum File Size: ~8 EiB (exbibytes)

Format Structure

XZ files consist of:

  • Stream header with magic bytes and stream flags
  • One or more blocks containing compressed data
  • Block headers with size and filter information
  • Block padding for alignment
  • Index section mapping blocks
  • Stream footer with CRC and backward size

History and Development

  • 2009: XZ format specification published by Lasse Collin
  • 2010: Adopted by major Linux distributions
  • 2011: Added to GNU core utilities
  • 2014: Parallel compression support added
  • Present: Standard compression format for many software packages

Advantages

  • High compression ratio: Often 20-30% better than gzip
  • Integrity checking: Built-in CRC verification
  • Streaming support: Can compress/decompress on-the-fly
  • Parallel processing: Multi-threaded compression
  • Block structure: Random access to compressed data

Use Cases

  • Software package distribution (Linux packages)
  • Kernel and firmware compression
  • Database backups and archival
  • Scientific data compression
  • Web content compression
  • Embedded systems firmware

Code Examples

Python XZ Processing with lzma Module

import lzma
import os
import time
import hashlib
from pathlib import Path
from typing import Optional, Dict, Any

class XZProcessor:
    def __init__(self):
        self.default_preset = 6  # Balanced compression/speed
        self.presets = {
            0: {'description': 'Fastest compression', 'level': 0},
            1: {'description': 'Fast compression', 'level': 1},
            3: {'description': 'Good compression', 'level': 3},
            6: {'description': 'Default compression', 'level': 6},
            9: {'description': 'Best compression', 'level': 9}
        }
    
    def compress_file(self, input_path: str, output_path: str, 
                     preset: int = None, check: int = lzma.CHECK_CRC64) -> Dict[str, Any]:
        """Compress a file using XZ format."""
        if preset is None:
            preset = self.default_preset
        
        try:
            start_time = time.time()
            input_size = os.path.getsize(input_path)
            
            with open(input_path, 'rb') as input_file:
                with lzma.LZMAFile(output_path, 'wb', preset=preset, check=check) as output_file:
                    # Process in chunks for large files
                    chunk_size = 1024 * 1024  # 1MB chunks
                    while True:
                        chunk = input_file.read(chunk_size)
                        if not chunk:
                            break
                        output_file.write(chunk)
            
            end_time = time.time()
            output_size = os.path.getsize(output_path)
            compression_ratio = (1 - output_size / input_size) * 100
            
            result = {
                'success': True,
                'input_size': input_size,
                'output_size': output_size,
                'compression_ratio': compression_ratio,
                'compression_time': end_time - start_time,
                'preset': preset,
                'check': check
            }
            
            print(f"Compressed {input_path} -> {output_path}")
            print(f"Size: {input_size} -> {output_size} bytes ({compression_ratio:.1f}% reduction)")
            print(f"Time: {result['compression_time']:.2f} seconds")
            
            return result
            
        except Exception as e:
            print(f"Error compressing file: {e}")
            return {'success': False, 'error': str(e)}
    
    def decompress_file(self, input_path: str, output_path: str) -> Dict[str, Any]:
        """Decompress an XZ file."""
        try:
            start_time = time.time()
            input_size = os.path.getsize(input_path)
            
            with lzma.LZMAFile(input_path, 'rb') as input_file:
                with open(output_path, 'wb') as output_file:
                    # Process in chunks
                    chunk_size = 1024 * 1024  # 1MB chunks
                    while True:
                        chunk = input_file.read(chunk_size)
                        if not chunk:
                            break
                        output_file.write(chunk)
            
            end_time = time.time()
            output_size = os.path.getsize(output_path)
            
            result = {
                'success': True,
                'input_size': input_size,
                'output_size': output_size,
                'decompression_time': end_time - start_time
            }
            
            print(f"Decompressed {input_path} -> {output_path}")
            print(f"Size: {input_size} -> {output_size} bytes")
            print(f"Time: {result['decompression_time']:.2f} seconds")
            
            return result
            
        except Exception as e:
            print(f"Error decompressing file: {e}")
            return {'success': False, 'error': str(e)}
    
    def compress_data(self, data: bytes, preset: int = None) -> bytes:
        """Compress raw data using XZ."""
        if preset is None:
            preset = self.default_preset
        
        return lzma.compress(data, preset=preset)
    
    def decompress_data(self, compressed_data: bytes) -> bytes:
        """Decompress XZ compressed data."""
        return lzma.decompress(compressed_data)
    
    def analyze_xz_file(self, file_path: str) -> Dict[str, Any]:
        """Analyze XZ file properties."""
        try:
            file_size = os.path.getsize(file_path)
            
            # Get file info
            with lzma.LZMAFile(file_path, 'rb') as xz_file:
                # Read first chunk to verify format
                chunk = xz_file.read(1024)
                if not chunk:
                    raise ValueError("Empty or invalid XZ file")
                
                # Get back to start and read more to estimate decompressed size
                xz_file.seek(0)
                
                # Sample decompression to estimate ratio
                sample_size = min(file_size, 10 * 1024 * 1024)  # Max 10MB sample
                decompressed_sample = xz_file.read(sample_size)
                
            # Calculate approximate compression ratio
            if len(decompressed_sample) > 0:
                # This is an approximation based on the sample
                estimated_decompressed_size = len(decompressed_sample)
                compression_ratio = (1 - file_size / estimated_decompressed_size) * 100
            else:
                compression_ratio = 0
            
            analysis = {
                'filename': os.path.basename(file_path),
                'file_size': file_size,
                'estimated_decompressed_size': estimated_decompressed_size,
                'estimated_compression_ratio': compression_ratio,
                'format': 'XZ',
                'valid': True
            }
            
            return analysis
            
        except Exception as e:
            return {
                'filename': os.path.basename(file_path),
                'file_size': os.path.getsize(file_path) if os.path.exists(file_path) else 0,
                'valid': False,
                'error': str(e)
            }
    
    def test_compression_presets(self, data: bytes) -> Dict[int, Dict[str, Any]]:
        """Test different compression presets on data."""
        results = {}
        
        for preset in [0, 1, 3, 6, 9]:
            try:
                start_time = time.time()
                compressed = lzma.compress(data, preset=preset)
                compression_time = time.time() - start_time
                
                start_time = time.time()
                decompressed = lzma.decompress(compressed)
                decompression_time = time.time() - start_time
                
                # Verify data integrity
                if decompressed != data:
                    raise ValueError("Data integrity check failed")
                
                compression_ratio = (1 - len(compressed) / len(data)) * 100
                
                results[preset] = {
                    'original_size': len(data),
                    'compressed_size': len(compressed),
                    'compression_ratio': compression_ratio,
                    'compression_time': compression_time,
                    'decompression_time': decompression_time,
                    'description': self.presets.get(preset, {}).get('description', f'Preset {preset}')
                }
                
            except Exception as e:
                results[preset] = {
                    'error': str(e),
                    'description': self.presets.get(preset, {}).get('description', f'Preset {preset}')
                }
        
        return results
    
    def create_archive_with_multiple_files(self, files: list, output_path: str, 
                                         preset: int = None) -> Dict[str, Any]:
        """Create XZ archive containing multiple files (using tar+xz approach)."""
        import tarfile
        
        if preset is None:
            preset = self.default_preset
        
        try:
            start_time = time.time()
            total_size = 0
            
            # Calculate total input size
            for file_path in files:
                if os.path.exists(file_path):
                    total_size += os.path.getsize(file_path)
            
            # Create tar.xz archive
            with tarfile.open(output_path, 'w:xz', preset=preset) as tar:
                for file_path in files:
                    if os.path.exists(file_path):
                        arcname = os.path.basename(file_path)
                        tar.add(file_path, arcname=arcname)
                        print(f"Added {file_path} as {arcname}")
            
            end_time = time.time()
            output_size = os.path.getsize(output_path)
            compression_ratio = (1 - output_size / total_size) * 100
            
            result = {
                'success': True,
                'files_count': len(files),
                'total_input_size': total_size,
                'output_size': output_size,
                'compression_ratio': compression_ratio,
                'compression_time': end_time - start_time,
                'preset': preset
            }
            
            print(f"Created archive: {output_path}")
            print(f"Files: {len(files)}, Total size: {total_size} -> {output_size} bytes")
            print(f"Compression ratio: {compression_ratio:.1f}%")
            
            return result
            
        except Exception as e:
            print(f"Error creating archive: {e}")
            return {'success': False, 'error': str(e)}
    
    def verify_integrity(self, xz_file_path: str) -> Dict[str, bool]:
        """Verify XZ file integrity."""
        try:
            with lzma.LZMAFile(xz_file_path, 'rb') as xz_file:
                # Try to read the entire file to verify integrity
                while True:
                    chunk = xz_file.read(1024 * 1024)  # 1MB chunks
                    if not chunk:
                        break
            
            return {'valid': True, 'error': None}
            
        except Exception as e:
            return {'valid': False, 'error': str(e)}

def compare_compression_formats(input_file: str):
    """Compare XZ compression with other formats."""
    import gzip
    import bz2
    
    if not os.path.exists(input_file):
        print(f"Input file not found: {input_file}")
        return
    
    with open(input_file, 'rb') as f:
        data = f.read()
    
    original_size = len(data)
    results = {}
    
    # Test XZ compression
    processor = XZProcessor()
    xz_results = processor.test_compression_presets(data)
    best_xz = min(xz_results.values(), key=lambda x: x.get('compressed_size', float('inf')))
    
    # Test gzip
    try:
        start_time = time.time()
        gzip_data = gzip.compress(data)
        gzip_time = time.time() - start_time
        gzip_ratio = (1 - len(gzip_data) / original_size) * 100
        
        results['gzip'] = {
            'compressed_size': len(gzip_data),
            'compression_ratio': gzip_ratio,
            'compression_time': gzip_time
        }
    except Exception as e:
        results['gzip'] = {'error': str(e)}
    
    # Test bzip2
    try:
        start_time = time.time()
        bz2_data = bz2.compress(data)
        bz2_time = time.time() - start_time
        bz2_ratio = (1 - len(bz2_data) / original_size) * 100
        
        results['bz2'] = {
            'compressed_size': len(bz2_data),
            'compression_ratio': bz2_ratio,
            'compression_time': bz2_time
        }
    except Exception as e:
        results['bz2'] = {'error': str(e)}
    
    # Print comparison
    print(f"\nCompression Comparison for {input_file}")
    print(f"Original size: {original_size:,} bytes")
    print("-" * 60)
    
    print(f"{'Format':<10} {'Size':<12} {'Ratio':<8} {'Time':<8}")
    print("-" * 60)
    
    # XZ best result
    if 'compressed_size' in best_xz:
        print(f"{'XZ':<10} {best_xz['compressed_size']:<12,} {best_xz['compression_ratio']:<7.1f}% {best_xz['compression_time']:<7.2f}s")
    
    # Other formats
    for format_name, result in results.items():
        if 'compressed_size' in result:
            print(f"{format_name:<10} {result['compressed_size']:<12,} {result['compression_ratio']:<7.1f}% {result['compression_time']:<7.2f}s")
        else:
            print(f"{format_name:<10} {'ERROR':<12} {'':<8} {'':<8}")

def batch_compress_directory(directory: str, output_dir: str = None, preset: int = 6):
    """Compress all files in a directory."""
    if not output_dir:
        output_dir = directory + "_compressed"
    
    os.makedirs(output_dir, exist_ok=True)
    
    processor = XZProcessor()
    results = []
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.xz'):
                continue  # Skip already compressed files
            
            input_path = os.path.join(root, file)
            relative_path = os.path.relpath(input_path, directory)
            output_path = os.path.join(output_dir, relative_path + '.xz')
            
            # Create output directory if needed
            os.makedirs(os.path.dirname(output_path), exist_ok=True)
            
            print(f"\nCompressing: {relative_path}")
            result = processor.compress_file(input_path, output_path, preset)
            result['input_file'] = relative_path
            results.append(result)
    
    # Summary
    successful = [r for r in results if r.get('success', False)]
    if successful:
        total_input = sum(r['input_size'] for r in successful)
        total_output = sum(r['output_size'] for r in successful)
        overall_ratio = (1 - total_output / total_input) * 100
        
        print(f"\n{'='*60}")
        print(f"Compression Summary:")
        print(f"Files processed: {len(successful)}")
        print(f"Total input size: {total_input:,} bytes")
        print(f"Total output size: {total_output:,} bytes")
        print(f"Overall compression ratio: {overall_ratio:.1f}%")
        print(f"Space saved: {total_input - total_output:,} bytes")

# Usage examples
def main():
    processor = XZProcessor()
    
    # Create sample file for testing
    sample_file = "sample.txt"
    with open(sample_file, 'w') as f:
        # Create a file with repetitive content (compresses well)
        for i in range(1000):
            f.write(f"This is line {i} with some repetitive content for testing compression.\n")
    
    print("Created sample file for testing")
    
    # Test compression
    result = processor.compress_file(sample_file, "sample.txt.xz", preset=6)
    
    if result['success']:
        # Analyze compressed file
        analysis = processor.analyze_xz_file("sample.txt.xz")
        print(f"\nFile analysis: {analysis}")
        
        # Test decompression
        decomp_result = processor.decompress_file("sample.txt.xz", "sample_decompressed.txt")
        
        if decomp_result['success']:
            # Verify integrity
            with open(sample_file, 'rb') as f1, open("sample_decompressed.txt", 'rb') as f2:
                original_hash = hashlib.md5(f1.read()).hexdigest()
                decompressed_hash = hashlib.md5(f2.read()).hexdigest()
                
                if original_hash == decompressed_hash:
                    print("✓ Integrity check passed")
                else:
                    print("✗ Integrity check failed")
        
        # Test different presets
        print("\nTesting compression presets:")
        with open(sample_file, 'rb') as f:
            data = f.read()
        
        preset_results = processor.test_compression_presets(data)
        
        print(f"{'Preset':<8} {'Size':<10} {'Ratio':<8} {'Comp.Time':<10} {'Decomp.Time':<12}")
        print("-" * 60)
        
        for preset, result in preset_results.items():
            if 'compressed_size' in result:
                print(f"{preset:<8} {result['compressed_size']:<10,} {result['compression_ratio']:<7.1f}% "
                      f"{result['compression_time']:<9.3f}s {result['decompression_time']:<11.3f}s")
    
    # Compare with other compression formats
    compare_compression_formats(sample_file)
    
    # Clean up
    for file in ["sample.txt", "sample.txt.xz", "sample_decompressed.txt"]:
        if os.path.exists(file):
            os.remove(file)

if __name__ == "__main__":
    main()

C XZ Processing with liblzma

#include <lzma.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#define BUFFER_SIZE (1024 * 1024)  // 1MB buffer

typedef struct {
    size_t input_size;
    size_t output_size;
    double compression_time;
    double compression_ratio;
    int success;
} compression_result_t;

// Error message helper
const char* lzma_error_string(lzma_ret ret) {
    switch (ret) {
        case LZMA_OK: return "OK";
        case LZMA_STREAM_END: return "Stream end";
        case LZMA_NO_CHECK: return "No check";
        case LZMA_UNSUPPORTED_CHECK: return "Unsupported check";
        case LZMA_GET_CHECK: return "Get check";
        case LZMA_MEM_ERROR: return "Memory error";
        case LZMA_MEMLIMIT_ERROR: return "Memory limit error";
        case LZMA_FORMAT_ERROR: return "Format error";
        case LZMA_OPTIONS_ERROR: return "Options error";
        case LZMA_DATA_ERROR: return "Data error";
        case LZMA_BUF_ERROR: return "Buffer error";
        case LZMA_PROG_ERROR: return "Programming error";
        default: return "Unknown error";
    }
}

compression_result_t compress_file_xz(const char* input_path, const char* output_path, int preset) {
    compression_result_t result = {0};
    FILE* input_file = NULL;
    FILE* output_file = NULL;
    lzma_stream strm = LZMA_STREAM_INIT;
    uint8_t* input_buffer = NULL;
    uint8_t* output_buffer = NULL;
    clock_t start_time, end_time;
    
    // Allocate buffers
    input_buffer = malloc(BUFFER_SIZE);
    output_buffer = malloc(BUFFER_SIZE);
    
    if (!input_buffer || !output_buffer) {
        fprintf(stderr, "Memory allocation failed\n");
        goto cleanup;
    }
    
    // Open files
    input_file = fopen(input_path, "rb");
    if (!input_file) {
        fprintf(stderr, "Cannot open input file: %s\n", input_path);
        goto cleanup;
    }
    
    output_file = fopen(output_path, "wb");
    if (!output_file) {
        fprintf(stderr, "Cannot create output file: %s\n", output_path);
        goto cleanup;
    }
    
    // Initialize encoder
    lzma_ret ret = lzma_easy_encoder(&strm, preset, LZMA_CHECK_CRC64);
    if (ret != LZMA_OK) {
        fprintf(stderr, "Failed to initialize encoder: %s\n", lzma_error_string(ret));
        goto cleanup;
    }
    
    start_time = clock();
    
    // Compression loop
    lzma_action action = LZMA_RUN;
    strm.next_out = output_buffer;
    strm.avail_out = BUFFER_SIZE;
    
    while (1) {
        // Read input if buffer is empty
        if (strm.avail_in == 0 && action == LZMA_RUN) {
            strm.next_in = input_buffer;
            strm.avail_in = fread(input_buffer, 1, BUFFER_SIZE, input_file);
            result.input_size += strm.avail_in;
            
            if (feof(input_file)) {
                action = LZMA_FINISH;
            }
        }
        
        // Compress data
        ret = lzma_code(&strm, action);
        
        // Write output if buffer is full or compression is done
        if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
            size_t write_size = BUFFER_SIZE - strm.avail_out;
            if (fwrite(output_buffer, 1, write_size, output_file) != write_size) {
                fprintf(stderr, "Write error\n");
                goto cleanup;
            }
            
            result.output_size += write_size;
            strm.next_out = output_buffer;
            strm.avail_out = BUFFER_SIZE;
        }
        
        if (ret == LZMA_STREAM_END) {
            break;
        }
        
        if (ret != LZMA_OK) {
            fprintf(stderr, "Compression error: %s\n", lzma_error_string(ret));
            goto cleanup;
        }
    }
    
    end_time = clock();
    result.compression_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
    result.compression_ratio = (1.0 - (double)result.output_size / result.input_size) * 100.0;
    result.success = 1;
    
    printf("Compressed %s -> %s\n", input_path, output_path);
    printf("Size: %zu -> %zu bytes (%.1f%% reduction)\n", 
           result.input_size, result.output_size, result.compression_ratio);
    printf("Time: %.2f seconds\n", result.compression_time);

cleanup:
    if (input_file) fclose(input_file);
    if (output_file) fclose(output_file);
    if (input_buffer) free(input_buffer);
    if (output_buffer) free(output_buffer);
    lzma_end(&strm);
    
    return result;
}

compression_result_t decompress_file_xz(const char* input_path, const char* output_path) {
    compression_result_t result = {0};
    FILE* input_file = NULL;
    FILE* output_file = NULL;
    lzma_stream strm = LZMA_STREAM_INIT;
    uint8_t* input_buffer = NULL;
    uint8_t* output_buffer = NULL;
    clock_t start_time, end_time;
    
    // Allocate buffers
    input_buffer = malloc(BUFFER_SIZE);
    output_buffer = malloc(BUFFER_SIZE);
    
    if (!input_buffer || !output_buffer) {
        fprintf(stderr, "Memory allocation failed\n");
        goto cleanup;
    }
    
    // Open files
    input_file = fopen(input_path, "rb");
    if (!input_file) {
        fprintf(stderr, "Cannot open input file: %s\n", input_path);
        goto cleanup;
    }
    
    output_file = fopen(output_path, "wb");
    if (!output_file) {
        fprintf(stderr, "Cannot create output file: %s\n", output_path);
        goto cleanup;
    }
    
    // Initialize decoder
    lzma_ret ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
    if (ret != LZMA_OK) {
        fprintf(stderr, "Failed to initialize decoder: %s\n", lzma_error_string(ret));
        goto cleanup;
    }
    
    start_time = clock();
    
    // Decompression loop
    lzma_action action = LZMA_RUN;
    strm.next_out = output_buffer;
    strm.avail_out = BUFFER_SIZE;
    
    while (1) {
        // Read input if buffer is empty
        if (strm.avail_in == 0 && action == LZMA_RUN) {
            strm.next_in = input_buffer;
            strm.avail_in = fread(input_buffer, 1, BUFFER_SIZE, input_file);
            result.input_size += strm.avail_in;
            
            if (feof(input_file)) {
                action = LZMA_FINISH;
            }
        }
        
        // Decompress data
        ret = lzma_code(&strm, action);
        
        // Write output if buffer is full or decompression is done
        if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
            size_t write_size = BUFFER_SIZE - strm.avail_out;
            if (fwrite(output_buffer, 1, write_size, output_file) != write_size) {
                fprintf(stderr, "Write error\n");
                goto cleanup;
            }
            
            result.output_size += write_size;
            strm.next_out = output_buffer;
            strm.avail_out = BUFFER_SIZE;
        }
        
        if (ret == LZMA_STREAM_END) {
            break;
        }
        
        if (ret != LZMA_OK) {
            fprintf(stderr, "Decompression error: %s\n", lzma_error_string(ret));
            goto cleanup;
        }
    }
    
    end_time = clock();
    result.compression_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
    result.success = 1;
    
    printf("Decompressed %s -> %s\n", input_path, output_path);
    printf("Size: %zu -> %zu bytes\n", result.input_size, result.output_size);
    printf("Time: %.2f seconds\n", result.compression_time);

cleanup:
    if (input_file) fclose(input_file);
    if (output_file) fclose(output_file);
    if (input_buffer) free(input_buffer);
    if (output_buffer) free(output_buffer);
    lzma_end(&strm);
    
    return result;
}

int verify_xz_file(const char* file_path) {
    FILE* file = fopen(file_path, "rb");
    if (!file) {
        fprintf(stderr, "Cannot open file: %s\n", file_path);
        return 0;
    }
    
    lzma_stream strm = LZMA_STREAM_INIT;
    uint8_t buffer[BUFFER_SIZE];
    lzma_ret ret;
    
    // Initialize decoder
    ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
    if (ret != LZMA_OK) {
        fprintf(stderr, "Failed to initialize decoder: %s\n", lzma_error_string(ret));
        fclose(file);
        return 0;
    }
    
    // Try to decompress the entire file
    strm.next_out = buffer;
    strm.avail_out = BUFFER_SIZE;
    
    int valid = 1;
    
    while (1) {
        if (strm.avail_in == 0) {
            strm.next_in = buffer;
            strm.avail_in = fread(buffer, 1, BUFFER_SIZE, file);
            
            if (feof(file)) {
                ret = lzma_code(&strm, LZMA_FINISH);
            } else {
                ret = lzma_code(&strm, LZMA_RUN);
            }
        } else {
            ret = lzma_code(&strm, LZMA_RUN);
        }
        
        if (ret == LZMA_STREAM_END) {
            break;
        }
        
        if (ret != LZMA_OK) {
            fprintf(stderr, "Verification failed: %s\n", lzma_error_string(ret));
            valid = 0;
            break;
        }
        
        // Reset output buffer when full
        if (strm.avail_out == 0) {
            strm.next_out = buffer;
            strm.avail_out = BUFFER_SIZE;
        }
    }
    
    lzma_end(&strm);
    fclose(file);
    
    return valid;
}

void test_compression_presets(const char* input_path) {
    printf("Testing compression presets for: %s\n", input_path);
    printf("Preset | Size      | Ratio  | Time\n");
    printf("-------|-----------|--------|--------\n");
    
    for (int preset = 0; preset <= 9; preset++) {
        char output_path[256];
        snprintf(output_path, sizeof(output_path), "test_preset_%d.xz", preset);
        
        compression_result_t result = compress_file_xz(input_path, output_path, preset);
        
        if (result.success) {
            printf("   %d   | %9zu | %5.1f%% | %6.2fs\n", 
                   preset, result.output_size, result.compression_ratio, result.compression_time);
        } else {
            printf("   %d   | ERROR     |        |\n", preset);
        }
        
        // Clean up test file
        remove(output_path);
    }
}

int main(int argc, char* argv[]) {
    if (argc < 3) {
        printf("Usage: %s <command> <input_file> [output_file] [preset]\n", argv[0]);
        printf("Commands: compress, decompress, verify, test\n");
        return 1;
    }
    
    const char* command = argv[1];
    const char* input_file = argv[2];
    const char* output_file = argc > 3 ? argv[3] : NULL;
    int preset = argc > 4 ? atoi(argv[4]) : 6;
    
    if (strcmp(command, "compress") == 0) {
        if (!output_file) {
            printf("Output file required for compression\n");
            return 1;
        }
        
        compression_result_t result = compress_file_xz(input_file, output_file, preset);
        return result.success ? 0 : 1;
    }
    else if (strcmp(command, "decompress") == 0) {
        if (!output_file) {
            printf("Output file required for decompression\n");
            return 1;
        }
        
        compression_result_t result = decompress_file_xz(input_file, output_file);
        return result.success ? 0 : 1;
    }
    else if (strcmp(command, "verify") == 0) {
        int valid = verify_xz_file(input_file);
        printf("File %s is %s\n", input_file, valid ? "valid" : "invalid");
        return valid ? 0 : 1;
    }
    else if (strcmp(command, "test") == 0) {
        test_compression_presets(input_file);
        return 0;
    }
    else {
        printf("Unknown command: %s\n", command);
        return 1;
    }
}

Command Line Usage

Basic XZ Operations

# Compress file
xz filename.txt
# Creates filename.txt.xz and removes original

# Decompress file
xz -d filename.txt.xz
# Creates filename.txt and removes .xz file

# Keep original files
xz -k filename.txt
xz -dk filename.txt.xz

# Compress with specific preset (0-9)
xz -6 filename.txt  # Default preset
xz -9 filename.txt  # Best compression
xz -1 filename.txt  # Fastest compression

# Verbose output
xz -v filename.txt

# Test file integrity
xz -t filename.txt.xz

# List file information
xz -l filename.txt.xz

Advanced XZ Options

# Use specific compression level and memory
xz --preset=6 --memlimit=128MiB filename.txt

# Parallel compression (multiple threads)
xz -T4 filename.txt  # Use 4 threads

# Custom block size
xz --block-size=1MiB filename.txt

# Different integrity check
xz --check=crc32 filename.txt
xz --check=crc64 filename.txt
xz --check=sha256 filename.txt

# Compress multiple files
xz file1.txt file2.txt file3.txt

# Compress directory (with tar)
tar -cJf archive.tar.xz directory/

Performance Optimization

  • Choose appropriate preset based on speed vs. compression ratio needs
  • Use parallel compression for multi-core systems
  • Consider memory limitations when setting compression levels
  • Use block-based compression for better random access
  • Monitor system resources during compression operations

Security Considerations

  • XZ files include integrity checking (CRC32/CRC64/SHA256)
  • Verify file integrity after transfer or long-term storage
  • Be aware of compression bombs (malicious compressed files)
  • Use appropriate memory limits to prevent DoS attacks
  • Validate file sizes before decompression

Best Practices

  • Use XZ for long-term archival where compression ratio is important
  • Choose gzip for faster compression when speed is priority
  • Test different presets to find optimal balance for your use case
  • Use parallel compression on multi-core systems
  • Implement proper error handling for compression operations
  • Monitor disk space during compression operations
  • Regular integrity checks for archived data

AI-Powered XZ File Analysis

🔍

Instant Detection

Quickly identify XZ compressed data files with high accuracy using Google's advanced Magika AI technology.

🛡️

Security Analysis

Analyze file structure and metadata to ensure the file is legitimate and safe to use.

📊

Detailed Information

Get comprehensive details about file type, MIME type, and other technical specifications.

🔒

Privacy First

All analysis happens in your browser - no files are uploaded to our servers.

Related File Types

Explore other file types in the Archive category and discover more formats:

Start Analyzing XZ Files Now

Use our free AI-powered tool to detect and analyze XZ compressed data files instantly with Google's Magika technology.

Try File Detection Tool