Skip to content

Commit

Permalink
Writer: initialize the writer on Flush() if wasn't initialized before
Browse files Browse the repository at this point in the history
This allows compressing empty files, because without this change
the proper headers were not emitted when reading an empty file
via e.g. io.Copy().

Signed-off-by: Nikolay Edigaryev <edigaryev@gmail.com>
  • Loading branch information
edigaryev committed Nov 22, 2023
1 parent ef495ee commit 3ac62b5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
Empty file added testdata/empty.txt
Empty file.
4 changes: 4 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (w *Writer) Flush() (err error) {
case writeState:
case errorState:
return w.state.err
case newState:
if err = w.init(); w.state.next(err) {
return
}
default:
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func TestWriter(t *testing.T) {
goldenFiles := []string{
"testdata/empty.txt",
"testdata/e.txt",
"testdata/gettysburg.txt",
"testdata/Mark.Twain-Tom.Sawyer.txt",
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestWriter(t *testing.T) {
t.Errorf("invalid sizes: got %d; want %d", got, want)
}

if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) {
if got, want := out.Bytes(), raw; !bytes.Equal(got, want) {
t.Fatal("uncompressed data does not match original")
}

Expand Down

0 comments on commit 3ac62b5

Please sign in to comment.