### Install and Use lzop Command-Line Tool (Bash) Source: https://context7.com/cyberdelia/lzo/llms.txt Provides instructions for installing and using the `lzop` command-line tool, which is part of the `cyberdelia/lzo` package. It covers installation using `go get`, compressing files, specifying compression levels (e.g., `-l 9` for best compression), and decompressing files (`-d`). ```bash # Install the command-line tool go get github.com/cyberdelia/lzo/cmd/lzop # Compress a file (creates data.txt.lzo) lzop data.txt # Compress with specific level (3=BestSpeed, 9=BestCompression) lzop -l 9 data.txt # Decompress a file (restores original filename from archive) lzop -d data.txt.lzo ``` -------------------------------- ### Command Line Tool (lzop) Source: https://context7.com/cyberdelia/lzo/llms.txt Provides lzop-compatible compression and decompression via a command-line interface. Install using `go get`. ```APIDOC ## Command Line Tool (lzop) The package includes a command-line tool that provides lzop-compatible compression and decompression. Install with `go get github.com/cyberdelia/lzo/cmd/lzop`. ### Usage #### Install the command-line tool ```bash go get github.com/cyberdelia/lzo/cmd/lzop ``` #### Compress a file ```bash lzop data.txt ``` This command creates `data.txt.lzo`. #### Compress with specific level ```bash lzop -l 9 data.txt ``` Use `-l` followed by the compression level (e.g., `9` for `BestCompression`, `3` for `BestSpeed`). #### Decompress a file ```bash lzop -d data.txt.lzo ``` This command restores the original filename from the archive metadata. ``` -------------------------------- ### LZO Compression Level Constants (Go) Source: https://context7.com/cyberdelia/lzo/llms.txt Introduces the predefined compression level constants available in the `lzo` package: `lzo.BestSpeed` and `lzo.BestCompression`. These constants allow users to easily select the desired trade-off between compression speed and the final compression ratio. The example prints the integer values of these constants. ```go package main import ( "fmt" "github.com/cyberdelia/lzo" ) func main() { // Available compression levels fmt.Printf("BestSpeed (fast compression): %d\n", lzo.BestSpeed) fmt.Printf("BestCompression (small size): %d\n", lzo.BestCompression) // BestSpeed = 3 - Optimizes for compression speed // BestCompression = 9 - Optimizes for smallest output size } ``` -------------------------------- ### Perform Full LZO Compression and Decompression Round-Trip Source: https://context7.com/cyberdelia/lzo/llms.txt This example demonstrates how to compress data with metadata (filename and modification time) and subsequently decompress it while verifying the integrity of the original content. It utilizes the lzo.NewWriterLevel and lzo.NewReader interfaces to handle the stream. ```go package main import ( "bytes" "fmt" "io" "log" "time" "github.com/cyberdelia/lzo" ) func main() { originalData := []byte("The quick brown fox jumps over the lazy dog. " + "Pack my box with five dozen liquor jugs.") // Compress compressed := new(bytes.Buffer) writer, err := lzo.NewWriterLevel(compressed, lzo.BestCompression) if err != nil { log.Fatal(err) } writer.Name = "pangram.txt" writer.ModTime = time.Now() if _, err := writer.Write(originalData); err != nil { log.Fatal(err) } if err := writer.Close(); err != nil { log.Fatal(err) } fmt.Printf("Original size: %d bytes\n", len(originalData)) fmt.Printf("Compressed size: %d bytes\n", compressed.Len()) fmt.Printf("Ratio: %.1f%%\n", float64(compressed.Len())/float64(len(originalData))*100) // Decompress reader, err := lzo.NewReader(bytes.NewReader(compressed.Bytes())) if err != nil { log.Fatal(err) } defer reader.Close() fmt.Printf("\nArchive metadata:\n") fmt.Printf(" Filename: %s\n", reader.Name) fmt.Printf(" ModTime: %s\n", reader.ModTime.Format(time.RFC3339)) decompressed, err := io.ReadAll(reader) if err != nil { log.Fatal(err) } if bytes.Equal(originalData, decompressed) { fmt.Println("\nRound-trip verification: SUCCESS") } } ``` -------------------------------- ### Create New Writer with Compression Level (Go) Source: https://context7.com/cyberdelia/lzo/llms.txt Creates a new `lzo.Writer` with a specified compression level. You can choose between `lzo.BestSpeed` for faster compression or `lzo.BestCompression` for a smaller output size. The function returns an error if the provided compression level is invalid. This example demonstrates compressing a file using the best compression setting. ```go package main import ( "io" "log" "os" "github.com/cyberdelia/lzo" ) func main() { input, err := os.Open("largefile.txt") if err != nil { log.Fatal(err) } defer input.Close() output, err := os.Create("largefile.txt.lzo") if err != nil { log.Fatal(err) } defer output.Close() // Use best compression for smaller output writer, err := lzo.NewWriterLevel(output, lzo.BestCompression) if err != nil { log.Fatal(err) } defer writer.Close() // Set the original filename in the archive writer.Name = "largefile.txt" // Compress the entire file written, err := io.Copy(writer, input) if err != nil { log.Fatal(err) } log.Printf("Compressed %d bytes with best compression", written) } ``` -------------------------------- ### Write Compressed Data in Chunks (Go) Source: https://context7.com/cyberdelia/lzo/llms.txt Demonstrates writing compressed data to an `io.Writer` using `lzo.Writer.Write`. The first call to `Write` includes the lzop header. Subsequent calls compress data in blocks, ensuring data integrity with checksums. The method returns the number of uncompressed bytes processed. This example writes multiple string chunks to a buffer. ```go package main import ( "bytes" "fmt" "log" "github.com/cyberdelia/lzo" ) func main() { buf := new(bytes.Buffer) writer := lzo.NewWriter(buf) writer.Name = "example.txt" // Write multiple chunks chunks := []string{ "First chunk of data.\n", "Second chunk with more content.\n", "Final chunk to compress.\n", } totalWritten := 0 for _, chunk := range chunks { n, err := writer.Write([]byte(chunk)) if err != nil { log.Fatal(err) } totalWritten += n } // Close to finalize the archive writer.Close() fmt.Printf("Wrote %d bytes, compressed to %d bytes\n", totalWritten, buf.Len()) } ``` -------------------------------- ### Reset Writer to New Output Stream (Go) Source: https://context7.com/cyberdelia/lzo/llms.txt Shows how to reuse an `lzo.Writer` instance for multiple compression operations by calling the `Reset` method. This method allows changing the underlying `io.Writer` while preserving the compression level and other settings, avoiding memory reallocations. The example compresses two different sets of data into separate buffers using the same writer instance. ```go package main import ( "bytes" "fmt" "log" "github.com/cyberdelia/lzo" ) func main() { // Create writer with specific level buf1 := new(bytes.Buffer) writer, err := lzo.NewWriterLevel(buf1, lzo.BestSpeed) if err != nil { log.Fatal(err) } // First compression writer.Name = "file1.txt" writer.Write([]byte("Content for first file")) writer.Close() fmt.Printf("First archive: %d bytes\n", buf1.Len()) // Reset and reuse for second compression buf2 := new(bytes.Buffer) writer.Reset(buf2) writer.Name = "file2.txt" writer.Write([]byte("Content for second file")) writer.Close() fmt.Printf("Second archive: %d bytes\n", buf2.Len()) } ``` -------------------------------- ### Close LZO Archive Writer (Go) Source: https://context7.com/cyberdelia/lzo/llms.txt Explains the importance of calling `lzo.Writer.Close()` to finalize an lzop archive. This method writes the end-of-archive marker (a zero-length block) and ensures the archive is valid. It does not close the underlying `io.Writer`. The example demonstrates writing data, closing the writer, and then verifying the archive by decompressing it. ```go package main import ( "bytes" "fmt" "io" "log" "github.com/cyberdelia/lzo" ) func main() { buf := new(bytes.Buffer) writer := lzo.NewWriter(buf) writer.Name = "data.txt" data := []byte("Important data to compress and verify") writer.Write(data) // Close to write the final block marker if err := writer.Close(); err != nil { log.Fatal(err) } // Verify by reading back reader, err := lzo.NewReader(bytes.NewReader(buf.Bytes())) if err != nil { log.Fatal(err) } defer reader.Close() decompressed, _ := io.ReadAll(reader) fmt.Printf("Round-trip successful: %v\n", bytes.Equal(data, decompressed)) } ``` -------------------------------- ### Create LZO Writer and Compress Data Source: https://context7.com/cyberdelia/lzo/llms.txt Demonstrates initializing an LZO writer to compress data. It shows how to set optional metadata like the original filename and how to write data to the compressed stream. ```go package main import ( "log" "os" "github.com/cyberdelia/lzo" ) func main() { output, err := os.Create("output.lzo") if err != nil { log.Fatal(err) } defer output.Close() writer := lzo.NewWriter(output) defer writer.Close() writer.Name = "original.txt" data := []byte("Hello, LZO compression!") n, err := writer.Write(data) if err != nil { log.Fatal(err) } log.Printf("Compressed %d bytes", n) } ``` -------------------------------- ### Initialize LZO Reader and Extract Metadata Source: https://context7.com/cyberdelia/lzo/llms.txt Demonstrates how to create a new LZO reader from an io.Reader, access file header metadata like original filename and modification time, and read the full decompressed content. ```go package main import ( "bytes" "fmt" "io" "log" "os" "github.com/cyberdelia/lzo" ) func main() { file, err := os.Open("data.txt.lzo") if err != nil { log.Fatal(err) } defer file.Close() reader, err := lzo.NewReader(file) if err != nil { log.Fatal(err) } defer reader.Close() fmt.Printf("Original filename: %s\n", reader.Name) fmt.Printf("Modification time: %s\n", reader.ModTime) decompressed, err := io.ReadAll(reader) if err != nil { log.Fatal(err) } fmt.Printf("Decompressed %d bytes\n", len(decompressed)) } ``` -------------------------------- ### lzo.NewReader Source: https://context7.com/cyberdelia/lzo/llms.txt Initializes a new LZO reader to decompress lzop-formatted data from an io.Reader. ```APIDOC ## func NewReader(r io.Reader) (*Reader, error) ### Description Creates a new Reader that decompresses lzop-format data from the given io.Reader. It validates the lzop header and extracts metadata. ### Parameters #### Request Body - **r** (io.Reader) - Required - The source reader containing lzop compressed data. ### Response #### Success Response (200) - **Reader** (struct) - The initialized LZO reader instance. - **error** (error) - Returns nil on success, or an error if the header is invalid. ``` -------------------------------- ### Read Decompressed Data in Chunks Source: https://context7.com/cyberdelia/lzo/llms.txt Shows how to implement chunked reading using the Reader.Read method. This approach is memory-efficient for processing large files by reading into a pre-allocated buffer. ```go package main import ( "fmt" "log" "os" "github.com/cyberdelia/lzo" ) func main() { file, err := os.Open("archive.lzo") if err != nil { log.Fatal(err) } defer file.Close() reader, err := lzo.NewReader(file) if err != nil { log.Fatal(err) } defer reader.Close() buf := make([]byte, 4096) totalBytes := 0 for { n, err := reader.Read(buf) if n > 0 { totalBytes += n } if err != nil { if err.Error() != "EOF" { log.Fatal(err) } break } } fmt.Printf("Total decompressed: %d bytes\n", totalBytes) } ``` -------------------------------- ### lzo.NewWriter Source: https://context7.com/cyberdelia/lzo/llms.txt Initializes a new LZO writer to compress data into lzop format. ```APIDOC ## func NewWriter(w io.Writer) *Writer ### Description Creates a new Writer that compresses data written to the underlying io.Writer using the default compression level (BestSpeed). ### Parameters #### Request Body - **w** (io.Writer) - Required - The destination writer for compressed output. ### Response #### Success Response (200) - **Writer** (struct) - The initialized LZO writer instance. ``` -------------------------------- ### NewWriterLevel Source: https://context7.com/cyberdelia/lzo/llms.txt Creates a new Writer with a specified compression level. Supports BestSpeed and BestCompression levels. Returns an error for invalid levels. ```APIDOC ## NewWriterLevel Creates a new Writer with a specified compression level. Use `lzo.BestSpeed` (3) for faster compression or `lzo.BestCompression` (9) for maximum compression ratio. Returns an error if the compression level is invalid. ### Method ```go func NewWriterLevel(w io.Writer, level int) (*Writer, error) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "io" "log" "os" "github.com/cyberdelia/lzo" ) func main() { input, err := os.Open("largefile.txt") if err != nil { log.Fatal(err) } defer input.Close() output, err := os.Create("largefile.txt.lzo") if err != nil { log.Fatal(err) } defer output.Close() // Use best compression for smaller output writer, err := lzo.NewWriterLevel(output, lzo.BestCompression) if err != nil { log.Fatal(err) } defer writer.Close() // Set the original filename in the archive writer.Name = "largefile.txt" // Compress the entire file written, err := io.Copy(writer, input) if err != nil { log.Fatal(err) } log.Printf("Compressed %d bytes with best compression", written) } ``` ### Response #### Success Response (200) - **writer** (*lzo.Writer*) - A new LZO writer instance. - **err** (*error*) - An error if the compression level is invalid or writer creation fails. #### Response Example None (error is returned directly) ``` -------------------------------- ### Writer.Write Source: https://context7.com/cyberdelia/lzo/llms.txt Writes compressed data to the underlying io.Writer. The first call writes the lzop header. Subsequent calls compress data in blocks with checksums. ```APIDOC ## Writer.Write Writes compressed data to the underlying io.Writer. The first call writes the lzop header with metadata. Each call compresses the input data as a block with checksums for integrity verification. Returns the number of uncompressed bytes processed. ### Method ```go func (w *Writer) Write(p []byte) (n int, err error) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **p** ([]byte) - The data to be compressed and written. ### Request Example ```go package main import ( "bytes" "fmt" "log" "github.com/cyberdelia/lzo" ) func main() { buf := new(bytes.Buffer) writer := lzo.NewWriter(buf) writer.Name = "example.txt" // Write multiple chunks chunks := []string{ "First chunk of data.\n", "Second chunk with more content.\n", "Final chunk to compress.\n", } totalWritten := 0 for _, chunk := range chunks { n, err := writer.Write([]byte(chunk)) if err != nil { log.Fatal(err) } totalWritten += n } // Close to finalize the archive writer.Close() fmt.Printf("Wrote %d bytes, compressed to %d bytes\n", totalWritten, buf.Len()) } ``` ### Response #### Success Response (200) - **n** (int) - The number of uncompressed bytes processed. - **err** (*error*) - An error if writing fails. #### Response Example ```json { "example": "Wrote 78 bytes, compressed to 70 bytes" } ``` ``` -------------------------------- ### Writer.Reset Source: https://context7.com/cyberdelia/lzo/llms.txt Resets the Writer to write to a new io.Writer, preserving the compression level. Allows reusing a Writer instance for multiple compressions. ```APIDOC ## Writer.Reset Resets the Writer to write to a new io.Writer while preserving the compression level. This allows reusing a Writer instance for multiple compression operations without reallocating memory. ### Method ```go func (w *Writer) Reset(w io.Writer) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **w** (io.Writer) - The new underlying writer to use for compression. ### Request Example ```go package main import ( "bytes" "fmt" "log" "github.com/cyberdelia/lzo" ) func main() { // Create writer with specific level buf1 := new(bytes.Buffer) writer, err := lzo.NewWriterLevel(buf1, lzo.BestSpeed) if err != nil { log.Fatal(err) } // First compression writer.Name = "file1.txt" writer.Write([]byte("Content for first file")) writer.Close() fmt.Printf("First archive: %d bytes\n", buf1.Len()) // Reset and reuse for second compression buf2 := new(bytes.Buffer) writer.Reset(buf2) writer.Name = "file2.txt" writer.Write([]byte("Content for second file")) writer.Close() fmt.Printf("Second archive: %d bytes\n", buf2.Len()) } ``` ### Response #### Success Response (200) None. The method modifies the writer in place. #### Response Example None ``` -------------------------------- ### Compression Constants Source: https://context7.com/cyberdelia/lzo/llms.txt Defines constants for compression levels, allowing control over the speed/ratio tradeoff. ```APIDOC ## Compression Constants The library provides two compression level constants for controlling the speed/ratio tradeoff. ### Constants - **`lzo.BestSpeed`**: Optimizes for compression speed (typically level 3). - **`lzo.BestCompression`**: Optimizes for the smallest output size (typically level 9). ### Usage Example ```go package main import ( "fmt" "github.com/cyberdelia/lzo" ) func main() { // Available compression levels fmt.Printf("BestSpeed (fast compression): %d\n", lzo.BestSpeed) fmt.Printf("BestCompression (small size): %d\n", lzo.BestCompression) // BestSpeed = 3 - Optimizes for compression speed // BestCompression = 9 - Optimizes for smallest output size } ``` ### Response Example ```json { "example": "BestSpeed (fast compression): 3\nBestCompression (small size): 9\n" } ``` ``` -------------------------------- ### Close LZO Reader and Validate Integrity Source: https://context7.com/cyberdelia/lzo/llms.txt Illustrates the proper way to close an LZO reader. Closing the reader is essential to check for any decompression errors that may have occurred during the stream processing. ```go package main import ( "io" "log" "os" "github.com/cyberdelia/lzo" ) func main() { file, err := os.Open("data.lzo") if err != nil { log.Fatal(err) } defer file.Close() reader, err := lzo.NewReader(file) if err != nil { log.Fatal(err) } _, err = io.Copy(os.Stdout, reader) if err != nil { log.Fatal(err) } if err := reader.Close(); err != nil { log.Printf("Decompression error: %v", err) } } ``` -------------------------------- ### Writer.Close Source: https://context7.com/cyberdelia/lzo/llms.txt Closes the Writer by writing the final block marker. Must be called to produce a valid lzop archive. Does not close the underlying io.Writer. ```APIDOC ## Writer.Close Closes the Writer by writing the final block marker (zero-length block) to signal end of archive. Does not close the underlying io.Writer. Must be called to produce a valid lzop archive. ### Method ```go func (w *Writer) Close() error ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "bytes" "fmt" "io" "log" "github.com/cyberdelia/lzo" ) func main() { buf := new(bytes.Buffer) writer := lzo.NewWriter(buf) writer.Name = "data.txt" data := []byte("Important data to compress and verify") writer.Write(data) // Close to write the final block marker if err := writer.Close(); err != nil { log.Fatal(err) } // Verify by reading back reader, err := lzo.NewReader(bytes.NewReader(buf.Bytes())) if err != nil { log.Fatal(err) } defer reader.Close() decompressed, _ := io.ReadAll(reader) fmt.Printf("Round-trip successful: %v\n", bytes.Equal(data, decompressed)) } ``` ### Response #### Success Response (200) - **err** (*error*) - nil if closing is successful. #### Response Example ```json { "example": "Round-trip successful: true" } ``` ``` -------------------------------- ### Reader.Read Source: https://context7.com/cyberdelia/lzo/llms.txt Reads and decompresses data from the LZO stream into a byte slice. ```APIDOC ## func (r *Reader) Read(p []byte) (n int, err error) ### Description Implements the io.Reader interface. It reads compressed blocks, decompresses them, verifies checksums, and returns the decompressed data. ### Parameters #### Request Body - **p** ([]byte) - Required - The buffer to store decompressed data. ### Response #### Success Response (200) - **n** (int) - Number of bytes read. - **err** (error) - Returns io.EOF when all data is processed. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.