gzip.go 845 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package readers
  2. import (
  3. "compress/gzip"
  4. "io"
  5. )
  6. // gzipReader wraps a *gzip.Reader so it closes the underlying stream
  7. // which the gzip library doesn't.
  8. type gzipReader struct {
  9. *gzip.Reader
  10. in io.ReadCloser
  11. }
  12. // NewGzipReader returns an io.ReadCloser which will read the stream
  13. // and close it when Close is called.
  14. //
  15. // Unfortunately gz.Reader does not close the underlying stream so we
  16. // can't use that directly.
  17. func NewGzipReader(in io.ReadCloser) (io.ReadCloser, error) {
  18. zr, err := gzip.NewReader(in)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &gzipReader{
  23. Reader: zr,
  24. in: in,
  25. }, nil
  26. }
  27. // Close the underlying stream and the gzip reader
  28. func (gz *gzipReader) Close() error {
  29. zrErr := gz.Reader.Close()
  30. inErr := gz.in.Close()
  31. if inErr != nil {
  32. return inErr
  33. }
  34. if zrErr != nil {
  35. return zrErr
  36. }
  37. return nil
  38. }