Understanding Croc's Compression Algorithm and When to Disable It

Croc uses the DEFLATE algorithm via Go's compress/flate package, applying Huffman-only encoding by default to balance speed and size reduction, and you can disable it with the --no-compress flag when transferring already-compressed files or working on low-latency networks.

Croc is a secure cross-platform file transfer tool that automatically compresses data to minimize bandwidth usage. Understanding how its compression pipeline works—and recognizing scenarios where it adds unnecessary overhead—helps you optimize transfer speeds for different file types and network conditions.

How Croc's Compression Algorithm Works

Croc implements compression on-the-fly during data transfers using standard Go libraries. The implementation prioritizes speed while still achieving modest byte reduction.

The DEFLATE Implementation

The core compression logic resides in src/compress/compress.go. Croc leverages Go's compress/flate package to encode data streams using the DEFLATE algorithm. The compress.Compress function wraps the standard library with Croc-specific defaults, creating a writer that processes byte slices during active transfers.

Compression Levels and HuffmanOnly

By default, compress.Compress invokes the underlying engine with flate.HuffmanOnly. This mode applies Huffman coding without LZ77 dictionary encoding, offering faster CPU processing than full DEFLATE while still providing useful size reduction for text and uncompressed binary formats.

For granular control, the CompressWithOption(src []byte, level int) function exposes the full DEFLATE level range (-2 through 9). Passing higher levels (e.g., 9) enables maximum compression at the cost of increased CPU cycles, while level -2 uses the default algorithm. Decompression is handled by Decompress, a thin wrapper around flate.NewReader that reconstructs the original byte stream regardless of the compression level used.

When to Disable Compression in Croc

Despite its efficiency, compression can degrade performance in specific scenarios. The --no-compress (or -nc) flag skips the DEFLATE step entirely, sending raw bytes unchanged.

Already-Compressed Files

Media archives (JPEG, MP4, ZIP) and compressed documents (PDFs, GZIP files) have high entropy that DEFLATE cannot significantly reduce. Processing these files wastes CPU cycles without meaningfully shrinking payload size. Use --no-compress when transferring these formats.

Low-Latency Networks

On fast LAN connections or high-bandwidth links where raw throughput matters more than bandwidth conservation, compression becomes a bottleneck. Removing the encoding/decoding step reduces latency and maximizes transfer speed.

Deterministic Byte Integrity

Cryptographic hashing, forensic imaging, or bit-for-bit verification workflows require the exact original bytes without transformation. Compression alters the byte stream, making hash validation impossible unless you disable it with --no-compress.

Debugging Transfer Issues

When isolating network-related problems from application logic, eliminating compression simplifies packet inspection and removes variables from performance testing.

The flag is defined in src/cli/cli.go (&cli.BoolFlag{Name: "no-compress"}) and propagated to the core struct as NoCompress. In src/croc/croc.go, the transfer logic checks this boolean and conditionally bypasses compress.Compress, logging "disabling compression" when active.

Working with Compression in Code

Command Line Examples

Enable default compression (HuffmanOnly) or disable it entirely:


# Default: compression enabled

croc send database.sql

# Skip compression for a video file

croc send --no-compress presentation.mp4

Using the Go Library

Import the compression package directly for custom processing:

package main

import (
	"fmt"
	"github.com/schollz/croc/v10/src/compress"
)

func main() {
	data := []byte("example payload text")

	// Default: HuffmanOnly compression
	compressed := compress.Compress(data)
	
	// Custom level: best compression (-2 to 9)
	best := compress.CompressWithOption(data, 9)
	
	// Decompress
	original, _ := compress.Decompress(compressed)
}

Disabling Compression Programmatically

When embedding Croc's transfer logic, set the struct field before initiating the transfer:

c := croc.NewCroc(...)
c.NoCompress = true  // Bypasses compress.Compress calls
c.Send(...)

Key Source Files

  • src/compress/compress.go: Contains Compress, CompressWithOption, and Decompress functions using compress/flate.
  • src/croc/croc.go: Core transfer logic that conditionally invokes compression based on the NoCompress field.
  • src/cli/cli.go: Defines the --no-compress CLI flag and maps it to the configuration struct.
  • src/web/wasm/main.go: Exposes compression utilities to the WebAssembly bridge for browser-based transfers.

Summary

  • Croc uses DEFLATE via Go's compress/flate with Huffman-only encoding by default in src/compress/compress.go.
  • The CompressWithOption function allows custom compression levels from -2 (default) to 9 (best compression).
  • Use --no-compress or c.NoCompress = true to skip compression for media files, fast networks, or deterministic hashing workflows.
  • Compression is applied on-the-fly during transfers in src/croc/croc.go based on the boolean flag defined in src/cli/cli.go.

Frequently Asked Questions

What compression algorithm does Croc use?

Croc uses the DEFLATE algorithm as implemented in Go's standard compress/flate package. By default, it configures the compressor to use flate.HuffmanOnly mode, which applies Huffman coding without LZ77 sliding window compression for speed.

How do I disable compression in Croc?

Add the --no-compress flag (short form -nc) to any send command, like croc send --no-compress file.zip. When using the Go library, set c.NoCompress = true on your croc.Croc instance before calling Send().

Should I disable compression when sending videos or images?

Yes. Media files like MP4, JPEG, and PNG are already compressed using specialized codecs. Running them through DEFLATE consumes CPU without reducing file size significantly, slowing your transfer unnecessarily.

Can I adjust the compression level in Croc?

While the CLI uses the fixed HuffmanOnly default, the Go API exposes compress.CompressWithOption(data, level), which accepts any valid DEFLATE level from -2 (default algorithm) to 9 (maximum compression). This allows programmatic tuning for specific payload types.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →