{"record":{"id":"12c7ecdf628be6ff","repo":"hyperledger/fabric","slug":"error-reading-as-gzip-stream","errorCode":null,"errorMessage":"error reading as gzip stream","messagePattern":"error reading as gzip stream","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/persistence/chaincode_package.go","lineNumber":174,"sourceCode":"\n\treturn tarFileStream, nil\n}\n\nfunc (cps *ChaincodePackageStreamer) File(name string) (tarFileStream *TarFileStream, err error) {\n\tfile, err := os.Open(cps.PackagePath)\n\tif err != nil {\n\t\treturn nil, errors.WithMessagef(err, \"could not open chaincode package at '%s'\", cps.PackagePath)\n\t}\n\n\tdefer func() {\n\t\tif err != nil {\n\t\t\tfile.Close()\n\t\t}\n\t}()\n\n\tgzReader, err := gzip.NewReader(file)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"error reading as gzip stream\")\n\t}\n\n\ttarReader := tar.NewReader(gzReader)\n\n\tfor {\n\t\theader, err := tarReader.Next()\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrapf(err, \"error inspecting next tar header\")\n\t\t}\n\n\t\tif header.Name != name {\n\t\t\tcontinue\n\t\t}\n","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/persistence/chaincode_package.go#L156-L192","documentation":"This error wraps the failure of gzip.NewReader when the code (Metadata/MetadataBytes/Code) tries to open a chaincode package file as a gzip stream. It means the file on disk is not a valid gzip archive (bad magic bytes, truncated, or empty). The library requires all chaincode packages to be gzipped tar archives, so a non-gzip file is rejected at the first read.","triggerScenarios":"Calling Metadata(), MetadataBytes(), or Code() on a File whose underlying path contains bytes that fail gzip header validation — e.g. a plain (uncompressed) tar, a raw binary, or a zero-byte file.","commonSituations":"A user packaged chaincode without gzip compression, a download/copy was truncated or corrupted, an empty placeholder file was created, or an old/non-standard packaging tool produced a plain tar.","solutions":["Re-create the package using the fabric tooling (peer lifecycle chaincode package) which always produces gzip'd tars.","Verify the file starts with the gzip magic bytes (1f 8b): file <packagepath> or head -c2 | xxd.","Check file size and integrity (cmp against original, re-download/copy).","Ensure you are pointing Metadata/Code at the packaged .tar.gz file, not the extracted source directory."],"exampleFix":"// before: passing a plain tar produced manually\ntar -cf mycc.tar metadata.json code.tar\n// after: gzip the tar so gzip.NewReader succeeds\ntar -czf mycc.tgz metadata.json code.tar","handlingStrategy":"validation","validationCode":"func looksGzip(path string) bool {\n\tf, err := os.Open(path)\n\tif err != nil { return false }\n\tdefer f.Close()\n\tmagic := make([]byte, 2)\n\tif _, err := io.ReadFull(f, magic); err != nil { return false }\n\treturn magic[0] == 0x1f && magic[1] == 0x8b\n}\nif !looksGzip(pkgPath) { return fmt.Errorf(\"%s is not a gzip archive\", pkgPath) }","typeGuard":"func isGzipBytes(b []byte) bool { return len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b }","tryCatchPattern":"file, err := os.Open(pkgPath)\nif err != nil { return err }\nmeta, err := p.Metadata(pkgPath)\nif err != nil {\n\tvar wrapped interface{ Unwrap() error }\n\tif errors.As(err, &target) { log.Fatalf(\"not gzip: %v\", err) }\n\treturn err\n}","preventionTips":["Always package with 'peer lifecycle chaincode package', never plain tar.","Checksum packages after creation and before use.","Verify gzip magic bytes before calling Metadata/Code.","Use binary-safe transfers (no ASCII ftp/text mode)."],"tags":["gzip","chaincode-package","corrupt-input"],"backgroundTag":"invalid-gzip-stream","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}