NewReaderFromFile returns a Reader for the specified path. The caller should call .Close() on the returned archive when done.
(sys *types.SystemContext, path string)
| 28 | // NewReaderFromFile returns a Reader for the specified path. |
| 29 | // The caller should call .Close() on the returned archive when done. |
| 30 | func NewReaderFromFile(sys *types.SystemContext, path string) (*Reader, error) { |
| 31 | file, err := os.Open(path) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("opening file %q: %w", path, err) |
| 34 | } |
| 35 | defer file.Close() |
| 36 | |
| 37 | // If the file is seekable and already not compressed we can just return the file itself |
| 38 | // as a source. Otherwise we pass the stream to NewReaderFromStream. |
| 39 | var stream io.Reader = file |
| 40 | if _, err := file.Seek(0, io.SeekCurrent); err == nil { // seeking is possible |
| 41 | decompressed, isCompressed, err := compression.AutoDecompress(file) |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("detecting compression for file %q: %w", path, err) |
| 44 | } |
| 45 | defer decompressed.Close() |
| 46 | stream = decompressed |
| 47 | if !isCompressed { |
| 48 | return newReader(path, false) |
| 49 | } |
| 50 | } |
| 51 | return NewReaderFromStream(sys, stream) |
| 52 | } |
| 53 | |
| 54 | // NewReaderFromStream returns a Reader for the specified inputStream, |
| 55 | // which can be either compressed or uncompressed. The caller can close the |
no test coverage detected
searching dependent graphs…