circu.js
    Preparing search index...

    Namespace CModuleZLib

    ZLib compression/decompression module for circu.js

    const zlib = import.meta.use('zlib')

    const original = import.meta.use('engine').encodeString('Hello, World!'.repeat(100));

    // Compress
    const compressed = zlib.deflate(original);
    console.log(`Original: ${original.length} bytes`);
    console.log(`Compressed: ${compressed.byteLength} bytes`);

    // Decompress
    const decompressed = zlib.inflate(compressed);
    const text = import.meta.use('engine').decodeString(decompressed);
    const zlib = import.meta.use('zlib')

    const data = import.meta.use('engine').encodeString('Large data...');

    // Compress with gzip (includes header/footer)
    const gzipped = zlib.gzip(data, zlib.BEST_COMPRESSION);

    // Decompress
    const ungzipped = zlib.gunzip(gzipped);
    const zlib = import.meta.use('zlib')

    const deflate = zlib.createDeflate(zlib.BEST_SPEED);

    // Process data in chunks
    const chunk1 = deflate.deflate(new Uint8Array([1, 2, 3]));
    const chunk2 = deflate.deflate(new Uint8Array([4, 5, 6]));

    // Finish and get final output
    const final = deflate.finish();

    console.log(`Compressed ${deflate.getTotalIn()} bytes to ${deflate.getTotalOut()} bytes`);
    const zlib = import.meta.use('zlib')

    const inflate = zlib.createInflate();

    // Process compressed data in chunks
    const compressed = zlib.deflate(import.meta.use('engine').encodeString('test'));
    const output = inflate.inflate(compressed);

    const text = import.meta.use('engine').decodeString(output);
    console.log(text); // 'test'
    const zlib = import.meta.use('zlib')

    const data = import.meta.use('engine').encodeString('Hello, World!');
    const crc = zlib.crc32(data);
    console.log(`CRC32: 0x${crc.toString(16)}`);

    // Incremental CRC32
    let crc2 = zlib.crc32(import.meta.use('engine').encodeString('Hello, '));
    crc2 = zlib.crc32(import.meta.use('engine').encodeString('World!'), crc2);
    console.log(crc === crc2); // true
    const zlib = import.meta.use('zlib')

    const data = import.meta.use('engine').encodeString('Test data'.repeat(1000));

    // Fast compression
    const fast = zlib.deflate(data, zlib.BEST_SPEED);

    // Best compression
    const best = zlib.deflate(data, zlib.BEST_COMPRESSION);

    console.log(`Fast: ${fast.byteLength} bytes`);
    console.log(`Best: ${best.byteLength} bytes`);
    const zlib = import.meta.use('zlib')

    const deflate = zlib.createDeflate(zlib.DEFAULT_COMPRESSION);

    // Compress some data
    deflate.deflate(new Uint8Array(100));

    // Change to best compression for important data
    deflate.params(zlib.BEST_COMPRESSION, zlib.DEFAULT_STRATEGY);

    // Continue compressing
    deflate.deflate(new Uint8Array(100));
    const result = deflate.finish();
    const zlib = import.meta.use('zlib')
    import * as fs from '@tjs/fs';

    // Read file
    const data = await fs.readFile('input.txt');

    // Compress
    const compressed = zlib.gzip(data);

    // Write compressed file
    await fs.writeFile('output.txt.gz', compressed);

    // Calculate checksum
    const crc = zlib.crc32(data);
    console.log(`CRC32: ${crc}`);
    const zlib = import.meta.use('zlib')

    const gzip = zlib.createGzip(zlib.DEFAULT_COMPRESSION);
    const output: Uint8Array[] = [];

    // Simulate reading file in chunks
    const chunks = [
    new Uint8Array(1024),
    new Uint8Array(1024),
    new Uint8Array(1024)
    ];

    for (const chunk of chunks) {
    const compressed = gzip.deflate(chunk);
    if (compressed.byteLength > 0) {
    output.push(new Uint8Array(compressed));
    }
    }

    // Finish
    const final = gzip.finish();
    if (final.byteLength > 0) {
    output.push(new Uint8Array(final));
    }

    console.log(`Total output: ${gzip.getTotalOut()} bytes`);
    const zlib = import.meta.use('zlib')

    // For text data, use default strategy
    const textDeflate = zlib.createDeflate(
    zlib.DEFAULT_COMPRESSION,
    zlib.DEFAULT_STRATEGY,
    8 // memory level
    );

    // For image data, RLE strategy might work better
    const imageDeflate = zlib.createDeflate(
    zlib.DEFAULT_COMPRESSION,
    zlib.RLE,
    9 // higher memory for better compression
    );
    const zlib = import.meta.use('zlib')

    const original = import.meta.use('engine').encodeString('Important data');

    // Calculate checksums before compression
    const originalCrc = zlib.crc32(original);
    const originalAdler = zlib.adler32(original);

    // Compress and transmit...
    const compressed = zlib.deflate(original);

    // Decompress
    const decompressed = zlib.inflate(compressed);

    // Verify integrity
    const newCrc = zlib.crc32(decompressed);
    const newAdler = zlib.adler32(decompressed);

    if (originalCrc === newCrc && originalAdler === newAdler) {
    console.log('Data integrity verified!');
    } else {
    console.error('Data corruption detected!');
    }

    Interfaces

    Deflate
    Inflate

    Variables

    BEST_COMPRESSION
    BEST_SPEED
    BLOCK
    DEFAULT_COMPRESSION
    DEFAULT_STRATEGY
    FILTERED
    FINISH
    FIXED
    FULL_FLUSH
    HUFFMAN_ONLY
    NO_COMPRESSION
    NO_FLUSH
    PARTIAL_FLUSH
    RLE
    SYNC_FLUSH

    Functions

    adler32
    crc32
    createDeflate
    createDeflateRaw
    createGunzip
    createGzip
    createInflate
    createInflateRaw
    deflate
    deflateRaw
    gunzip
    gzip
    inflate
    inflateRaw