| 1 |
// Copyright (C) 2024 Sumner Evans |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package gnuzip |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"compress/gzip" |
| 12 |
"errors" |
| 13 |
"io" |
| 14 |
) |
| 15 |
|
| 16 |
func MaybeGUnzip(body []byte) ([]byte, error) { |
| 17 |
reader, err := gzip.NewReader(bytes.NewReader(body)) |
| 18 |
if err != nil { |
| 19 |
if errors.Is(err, gzip.ErrHeader) { |
| 20 |
return body, nil |
| 21 |
} else { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
} |
| 25 |
defer reader.Close() |
| 26 |
return io.ReadAll(reader) |
| 27 |
} |