MCPcopy
hub / github.com/containerd/containerd / DecompressStream

Function DecompressStream

pkg/archive/compression/compression.go:187–241  ·  view source on GitHub ↗

DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.

(archive io.Reader)

Source from the content-addressed store, hash-verified

185
186// DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
187func DecompressStream(archive io.Reader) (DecompressReadCloser, error) {
188 buf := newBufferedReader(archive)
189 bs, err := buf.Peek(10)
190 if err != nil && err != io.EOF {
191 // Note: we'll ignore any io.EOF error because there are some odd
192 // cases where the layer.tar file will be empty (zero bytes) and
193 // that results in an io.EOF from the Peek() call. So, in those
194 // cases we'll just treat it as a non-compressed stream and
195 // that means just create an empty layer.
196 // See Issue docker/docker#18170
197 return nil, err
198 }
199
200 switch compression := DetectCompression(bs); compression {
201 case Uncompressed:
202 return &readCloserWrapper{
203 Reader: buf,
204 compression: compression,
205 }, nil
206 case Gzip:
207 ctx, cancel := context.WithCancel(context.Background())
208 gzReader, err := gzipDecompress(ctx, buf)
209 if err != nil {
210 cancel()
211 return nil, err
212 }
213
214 return &readCloserWrapper{
215 Reader: gzReader,
216 compression: compression,
217 closer: func() error {
218 cancel()
219 return gzReader.Close()
220 },
221 }, nil
222 case Zstd:
223 zstdReader, err := zstd.NewReader(buf,
224 zstd.WithDecoderLowmem(false),
225 )
226 if err != nil {
227 return nil, err
228 }
229 return &readCloserWrapper{
230 Reader: zstdReader,
231 compression: compression,
232 closer: func() error {
233 zstdReader.Close()
234 return nil
235 },
236 }, nil
237
238 default:
239 return nil, fmt.Errorf("unsupported compression format %s", (&compression).Extension())
240 }
241}
242
243// CompressStream compresses the dest with specified compression algorithm.
244func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) {

Callers 10

InstallMethod · 0.92
compressedHandlerFunction · 0.92
ImportMethod · 0.92
CreateMethod · 0.92
GetDiffIDFunction · 0.92
resolveLayersFunction · 0.92
LayerConvertFuncFunction · 0.92
TestPrefixHeaderReadableFunction · 0.92
FuzzDecompressStreamFunction · 0.85
testDecompressFunction · 0.85

Calls 6

newBufferedReaderFunction · 0.85
DetectCompressionFunction · 0.85
gzipDecompressFunction · 0.85
PeekMethod · 0.80
ExtensionMethod · 0.80
CloseMethod · 0.65

Tested by 3

TestPrefixHeaderReadableFunction · 0.74
FuzzDecompressStreamFunction · 0.68
testDecompressFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…