ulikunitz / xz

Pure golang package for reading and writing xz-compressed files
Other
477 stars 45 forks source link

Compressing and decompressing empty string fails #12

Closed tumdum closed 7 years ago

tumdum commented 7 years ago

Following program:

package main

import (
    "bytes"
    "io"
    "log"
    "os"

    "github.com/ulikunitz/xz"
)

func main() {
    const text = ""
    var buf bytes.Buffer
    // compress text
    w, err := xz.NewWriter(&buf)
    if err != nil {
        log.Fatalf("xz.NewWriter error %s", err)
    }
    if _, err := io.WriteString(w, text); err != nil {
        log.Fatalf("WriteString error %s", err)
    }
    if err := w.Close(); err != nil {
        log.Fatalf("w.Close error %s", err)
    }
    // decompress buffer and write output to stdout
    r, err := xz.NewReader(&buf)
    if err != nil {
        log.Fatalf("NewReader error %s", err)
    }
    if _, err = io.Copy(os.Stdout, r); err != nil {
        log.Fatalf("io.Copy error %s", err)
    }
}

when executed fails:

$ go run xztest.go
2017/02/15 10:55:38 io.Copy error xz: invalid header magic bytes
exit status 1

I would expect that it should correctly handle empty string.

ulikunitz commented 7 years ago

Hi Tomasz, thank you for reporting the bug. I'm working on it.

ulikunitz commented 7 years ago

The issue is fixed with release 0.5.3. The LZMA2 reader (lzma.Reader2) couldn't be created for an empty LZMA stream. The magic header error was a subsequent error.