pierrec / lz4

LZ4 compression and decompression in pure Go
BSD 3-Clause "New" or "Revised" License
878 stars 142 forks source link

Empty compressed value if input string is less than 27 characters #157

Closed satheshshiva closed 2 years ago

satheshshiva commented 2 years ago
        s := "hello worldhello worldhel"
    data := []byte(s)
    buf := make([]byte, len(data))

    var c lz4.Compressor
    n, err := c.CompressBlock(data, buf)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("compressed data length is " , n)

Expected Output: compressed data length > 0

Actual Output: compressed data length = 0

pierrec commented 2 years ago

See https://pkg.go.dev/github.com/pierrec/lz4/v4#Compressor, esp. the last sentence: "The return value (0, nil) means the data is likely incompressible and a buffer of length CompressBlockBound(len(src)) should be passed in."

Your data is not compressible. So it doesnt do anything, unless your input buffer has the CompressBlockBound size.