### Goheatshrink CLI Tool Usage Source: https://context7.com/currantlabs/goheatshrink/llms.txt Demonstrates how to use the goheatshrink command-line interface for compressing and decompressing files. Examples include basic compression/decompression, custom window/lookahead settings, and verbose output. ```bash # Install the CLI tool go install github.com/currantlabs/goheatshrink/heatshrink # Compress a file (default: encode mode) heatshrink -e input.bin output.bin.hz # Decompress a file heatshrink -d input.bin.hz output.bin # Use custom window and lookahead settings heatshrink -e -w 10 -l 4 input.bin output.bin.hz # Verbose output with compression statistics heatshrink -e -v input.bin output.bin.hz ``` -------------------------------- ### Complete Compression and Decompression Round-Trip in Go Source: https://context7.com/currantlabs/goheatshrink/llms.txt Provides a comprehensive example of compressing and decompressing data, including error handling. This pattern is suitable for production environments, ensuring consistent window and lookahead settings for both operations. ```go package main import ( "bytes" "fmt" "io" "io/ioutil" "log" "github.com/currantlabs/goheatshrink" ) func compress(data []byte, window, lookahead uint8) ([]byte, error) { var compressed bytes.Buffer w := goheatshrink.NewWriter(&compressed, goheatshrink.Window(window), goheatshrink.Lookahead(lookahead)) _, err := io.Copy(w, bytes.NewReader(data)) if err != nil { return nil, fmt.Errorf("compression error: %w", err) } if err := w.Close(); err != nil { return nil, fmt.Errorf("close error: %w", err) } return compressed.Bytes(), nil } func decompress(data []byte, window, lookahead uint8) ([]byte, error) { r := goheatshrink.NewReader(bytes.NewReader(data), goheatshrink.Window(window), goheatshrink.Lookahead(lookahead)) decompressed, err := ioutil.ReadAll(r) if err != nil { return nil, fmt.Errorf("decompression error: %w", err) } return decompressed, nil } func main() { // Original data with repetitive patterns compresses well original := []byte("abcabcdabcdeabcdefabcdefgabcdefgh") // Use consistent window and lookahead for compress/decompress window := uint8(8) lookahead := uint8(4) compressed, err := compress(original, window, lookahead) if err != nil { log.Fatal(err) } decompressed, err := decompress(compressed, window, lookahead) if err != nil { log.Fatal(err) } fmt.Printf("Original: %d bytes\n", len(original)) fmt.Printf("Compressed: %d bytes\n", len(compressed)) fmt.Printf("Decompressed: %d bytes\n", len(decompressed)) fmt.Printf("Match: %v\n", bytes.Equal(original, decompressed)) // Output: // Original: 33 bytes // Compressed: 18 bytes (approximately) // Decompressed: 33 bytes // Match: true } ``` -------------------------------- ### Compress and Decompress Data via Stdin/Stdout Source: https://context7.com/currantlabs/goheatshrink/llms.txt Demonstrates how to compress data from stdin to stdout and then decompress it back using the heatshrink command-line tool. Ensure the input file exists and the output file can be written. ```bash cat input.bin | heatshrink -e > output.bin.hz cat output.bin.hz | heatshrink -d > output.bin ``` -------------------------------- ### Configure Lookahead Size in Go Source: https://context7.com/currantlabs/goheatshrink/llms.txt Demonstrates how to configure the lookahead size for compression, affecting the maximum back-reference length. Different lookahead values (3, 4, 6 bits) are shown with a fixed window size. ```go package main import ( "bytes" "github.com/currantlabs/goheatshrink" ) func main() { var output bytes.Buffer input := []byte("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") // Small lookahead (3 bits) - max back-ref length of 8 bytes w1 := goheatshrink.NewWriter(&output, goheatshrink.Window(8), goheatshrink.Lookahead(3)) w1.Write(input) w1.Close() output.Reset() // Default lookahead (4 bits) - max back-ref length of 16 bytes w2 := goheatshrink.NewWriter(&output, goheatshrink.Window(8), goheatshrink.Lookahead(4)) w2.Write(input) w2.Close() output.Reset() // Larger lookahead (6 bits) - max back-ref length of 64 bytes w3 := goheatshrink.NewWriter(&output, goheatshrink.Window(8), goheatshrink.Lookahead(6)) w3.Write(input) w3.Close() } ``` -------------------------------- ### Configure Window Size for Compression Source: https://context7.com/currantlabs/goheatshrink/llms.txt The Window option configures the base-2 logarithm of the sliding window size. Valid values are 4 to 16. Larger windows offer better compression at the cost of increased memory usage. ```go package main import ( "bytes" "io" "github.com/currantlabs/goheatshrink" ) func main() { var output bytes.Buffer input := []byte("repeated patterns repeated patterns repeated") // Small window (2^4 = 16 bytes) - less memory, faster, less compression w1 := goheatshrink.NewWriter(&output, goheatshrink.Window(4)) w1.Write(input) w1.Close() smallWindowSize := output.Len() output.Reset() // Default window (2^8 = 256 bytes) - balanced for embedded systems w2 := goheatshrink.NewWriter(&output, goheatshrink.Window(8)) w2.Write(input) w2.Close() defaultWindowSize := output.Len() output.Reset() // Large window (2^16 = 65536 bytes) - best compression, more memory w3 := goheatshrink.NewWriter(&output, goheatshrink.Window(16)) w3.Write(input) w3.Close() largeWindowSize := output.Len() // largeWindowSize <= defaultWindowSize <= smallWindowSize (typically) } ``` -------------------------------- ### Window - Configure Window Size Option Source: https://context7.com/currantlabs/goheatshrink/llms.txt Configures the base-2 logarithm of the sliding window size used for finding repeating patterns. A larger window searches more history, potentially achieving better compression, but uses more memory. Valid values range from 4 (MinWindow) to 16 (MaxWindow). ```APIDOC ## Window - Configure Window Size Option ### Description Configures the base-2 logarithm of the sliding window size used for finding repeating patterns. A larger window searches more history, potentially achieving better compression, but uses more memory. Valid values range from 4 (MinWindow) to 16 (MaxWindow). ### Method `Window` ### Parameters #### Function Parameters - `bits` (int) - Required - The base-2 logarithm of the window size. Must be between 4 and 16, inclusive. ### Request Example ```go package main import ( "bytes" "io" "github.com/currantlabs/goheatshrink" ) func main() { var output bytes.Buffer input := []byte("repeated patterns repeated patterns repeated") // Small window (2^4 = 16 bytes) - less memory, faster, less compression w1 := goheatshrink.NewWriter(&output, goheatshrink.Window(4)) w1.Write(input) w1.Close() smallWindowSize := output.Len() output.Reset() // Default window (2^8 = 256 bytes) - balanced for embedded systems w2 := goheatshrink.NewWriter(&output, goheatshrink.Window(8)) w2.Write(input) w2.Close() defaultWindowSize := output.Len() output.Reset() // Large window (2^16 = 65536 bytes) - best compression, more memory w3 := goheatshrink.NewWriter(&output, goheatshrink.Window(16)) w3.Write(input) w3.Close() largeWindowSize := output.Len() // largeWindowSize <= defaultWindowSize <= smallWindowSize (typically) } ``` ### Response #### Success Response - `goheatshrink.Option` - An option that can be passed to `NewWriter` or `NewReader` to configure the window size. ``` -------------------------------- ### Create Compression Writer with GoHeatshrink Source: https://context7.com/currantlabs/goheatshrink/llms.txt Use NewWriter to create an io.WriteCloser that compresses data. Remember to call Close() to flush remaining data. Custom settings can be applied using functional options. ```go package main import ( "bytes" "io" "os" "github.com/currantlabs/goheatshrink" ) func main() { // Compress data to a file in, _ := os.Open("bigfile.bin") defer in.Close() out, _ := os.Create("bigfile.bin.hz") defer out.Close() // Create writer with default settings (window=8, lookahead=4) w := goheatshrink.NewWriter(out) io.Copy(w, in) w.Close() // Important: must close to flush remaining data // Compress with custom settings for better compression var compressed bytes.Buffer w2 := goheatshrink.NewWriter(&compressed, goheatshrink.Window(10), // Larger window for better compression goheatshrink.Lookahead(4)) // Default lookahead data := []byte("abcabcdabcdeabcdefabcdefgabcdefgh") w2.Write(data) w2.Close() // compressed.Bytes() now contains the compressed data } ``` -------------------------------- ### Compress Data with goheatshrink Source: https://github.com/currantlabs/goheatshrink/blob/master/README.md Use goheatshrink.NewWriter to compress data from an input source to an output destination. Ensure files are closed after use. ```go package main import ( "io" "os" "github.com/currantlabs/goheatshrink" ) func main() { in, _ := os.Open("bigfile.bin") defer in.Close() out, _ := os.Create("bigfile.bin.hz") defer out.Close() w := goheatshrink.NewWriter(out) io.Copy(w, in) w.Close() } ``` -------------------------------- ### Create Decompression Reader with GoHeatshrink Source: https://context7.com/currantlabs/goheatshrink/llms.txt NewReader creates a ReadResetter for decompressing heatshrink data. It implements io.Reader and can be reset to read from a new source. Custom settings can be applied during initialization. ```go package main import ( "bytes" "io" "io/ioutil" "os" "github.com/currantlabs/goheatshrink" ) func main() { // Decompress from a file in, _ := os.Open("bigfile.bin.hz") defer in.Close() out, _ := os.Create("bigfile.bin") defer out.Close() r := goheatshrink.NewReader(in) io.Copy(out, r) // Decompress from bytes with custom settings compressedData := []byte{/* compressed bytes */} r2 := goheatshrink.NewReader( bytes.NewReader(compressedData), goheatshrink.Window(10), goheatshrink.Lookahead(4)) decompressed, _ := ioutil.ReadAll(r2) // decompressed contains the original data // Reuse reader with Reset() newCompressedData := []byte{/* more compressed bytes */} r2.Reset(bytes.NewReader(newCompressedData)) moreDecompressed, _ := ioutil.ReadAll(r2) } ``` -------------------------------- ### NewReader - Create Decompression Reader Source: https://context7.com/currantlabs/goheatshrink/llms.txt Creates a new `ReadResetter` that reads and decompresses heatshrink-compressed data from the underlying reader. The reader implements `io.Reader` and can be used with standard Go I/O functions. It also provides a `Reset()` method to reuse the reader with a new input source. ```APIDOC ## NewReader - Create Decompression Reader ### Description Creates a new `ReadResetter` that reads and decompresses heatshrink-compressed data from the underlying reader. The reader implements `io.Reader` and can be used with standard Go I/O functions. It also provides a `Reset()` method to reuse the reader with a new input source. ### Method `NewReader` ### Parameters #### Function Parameters - `r` (*io.Reader) - Required - The underlying reader from which compressed data will be read. - `options` (*goheatshrink.Option) - Optional - Configuration options for the decompression reader, such as `Window` and `Lookahead`. ### Request Example ```go package main import ( "bytes" "io" "io/ioutil" "os" "github.com/currantlabs/goheatshrink" ) func main() { // Decompress from a file in, _ := os.Open("bigfile.bin.hz") defer in.Close() out, _ := os.Create("bigfile.bin") defer out.Close() r := goheatshrink.NewReader(in) io.Copy(out, r) // Decompress from bytes with custom settings compressedData := []byte{/* compressed bytes */} r2 := goheatshrink.NewReader( bytes.NewReader(compressedData), goheatshrink.Window(10), goheatshrink.Lookahead(4)) decompressed, _ := ioutil.ReadAll(r2) // decompressed contains the original data // Reuse reader with Reset() newCompressedData := []byte{/* more compressed bytes */} r2.Reset(bytes.NewReader(newCompressedData)) moreDecompressed, _ := ioutil.ReadAll(r2) } ``` ### Response #### Success Response - `ReadResetter` - A reader that decompresses data read from the underlying reader. It also implements `Reset()` to allow reusing the reader with a new input source. ``` -------------------------------- ### Decompress Data with goheatshrink Source: https://github.com/currantlabs/goheatshrink/blob/master/README.md Use goheatshrink.NewReader to decompress data from a compressed input source to an output destination. Ensure files are closed after use. ```go package main import ( "io" "os" "github.com/currantlabs/goheatshrink" ) func main() { in, _ := os.Open("bigfile.bin.hz") defer in.Close() out, _ := os.Create("bigfile.bin") defer out.Close() r := goheatshrink.NewReader(in) io.Copy(out, r) } ``` -------------------------------- ### Reuse Readers with ReadResetter Interface in Go Source: https://context7.com/currantlabs/goheatshrink/llms.txt Illustrates using the `ReadResetter` interface to reuse a single decompressor instance for multiple streams. This avoids repeated memory allocations when decompressing several messages sequentially. ```go package main import ( "bytes" "io/ioutil" "github.com/currantlabs/goheatshrink" ) func main() { // Compress multiple messages messages := [][]byte{ []byte("First message with some repeated content content"), []byte("Second message also with repeated words words words"), []byte("Third message repeating pattern pattern pattern"), } var compressedMessages [][]byte for _, msg := range messages { var buf bytes.Buffer w := goheatshrink.NewWriter(&buf) w.Write(msg) w.Close() compressedMessages = append(compressedMessages, buf.Bytes()) } // Decompress using a single reusable reader reader := goheatshrink.NewReader(bytes.NewReader(compressedMessages[0])) for i, compressed := range compressedMessages { if i > 0 { reader.Reset(bytes.NewReader(compressed)) } decompressed, _ := ioutil.ReadAll(reader) // Process decompressed data _ = decompressed } } ``` -------------------------------- ### NewWriter - Create Compression Writer Source: https://context7.com/currantlabs/goheatshrink/llms.txt Creates a new `io.WriteCloser` that compresses data written to it using the heatshrink algorithm. Data written to the returned writer is compressed and written to the underlying writer. The caller must call `Close()` when done to flush any buffered data. ```APIDOC ## NewWriter - Create Compression Writer ### Description Creates a new `io.WriteCloser` that compresses data written to it using the heatshrink algorithm. Data written to the returned writer is compressed and written to the underlying writer. The caller must call `Close()` when done to flush any buffered data. ### Method `NewWriter` ### Parameters #### Function Parameters - `w` (*io.Writer) - Required - The underlying writer to which compressed data will be written. - `options` (*goheatshrink.Option) - Optional - Configuration options for the compression writer, such as `Window` and `Lookahead`. ### Request Example ```go package main import ( "bytes" "io" "os" "github.com/currantlabs/goheatshrink" ) func main() { // Compress data to a file in, _ := os.Open("bigfile.bin") defer in.Close() out, _ := os.Create("bigfile.bin.hz") defer out.Close() // Create writer with default settings (window=8, lookahead=4) w := goheatshrink.NewWriter(out) io.Copy(w, in) w.Close() // Important: must close to flush remaining data // Compress with custom settings for better compression var compressed bytes.Buffer w2 := goheatshrink.NewWriter(&compressed, goheatshrink.Window(10), // Larger window for better compression goheatshrink.Lookahead(4)) // Default lookahead data := []byte("abcabcdabcdeabcdefabcdefgabcdefgh") w2.Write(data) w2.Close() } ``` ### Response #### Success Response - `io.WriteCloser` - A writer that compresses data before writing it to the underlying writer. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.