CodeSampleX

示例

github.com/klauspost/compress v1.19.1: zlib.NewWriter

已验证示例 — golang github.com/klauspost/compress v1.19.1: zlib.NewWriter. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: zlib.NewWriter writes compressed…

sha256:ddaec8b898032d5c2eb640f1f70a8bb2b1691f517fea833b0837d1e7ffca6307

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-10

案例

HOW
目标
verify github.com/klauspost/compress/zlib.NewWriter in pkg:golang/github.com/klauspost/compress@v1.19.1
包
符号
  • github.com/klauspost/compress/zlib.NewWriter
创建时间
2026-09-10T22:29:09Z

契约

  1. zlib.NewWriter writes compressed data stream that decompresses to match original uncompressed bytes
  2. zlib.NewWriterLevel initializes a writer with specified compression levels including BestSpeed and BestCompression
  3. zlib.NewWriter handles empty input buffers and emits a valid zlib stream on Close
  4. zlib.NewWriter Flush flushes pending compressed data to the underlying writer
  5. zlib.NewWriter Reset reinitializes the writer to write to a new destination without reallocating state
  6. zlib.NewWriterLevelDict initializes a writer with a custom dictionary and compresses data successfully

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/klauspost/compress/zlib.NewWriter in pkg:golang/github.com/klauspost/compress@v1.19.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.19.1
Demonstrate these symbols/APIs:
  - github.com/klauspost/compress/zlib.NewWriter

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:4e172e4e722773fea0cb0d2481805b992d294107859aafaa0ef3cb4935c9b8f2","contract":["zlib.NewWriter writes compressed data stream that decompresses to match original uncompressed bytes","zlib.NewWriterLevel initializes a writer with specified compression levels including BestSpeed and BestCompression","zlib.NewWriter handles empty input buffers and emits a valid zlib stream on Close","zlib.NewWriter Flush flushes pending compressed data to the underlying writer","zlib.NewWriter Reset reinitializes the writer to write to a new destination without reallocating state","zlib.NewWriterLevelDict initializes a writer with a custom dictionary and compresses data successfully"],"goal":"verify github.com/klauspost/compress/zlib.NewWriter in pkg:golang/github.com/klauspost/compress@v1.19.1","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"symbols":["github.com/klauspost/compress/zlib.NewWriter"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.19.1","symbols":["github.com/klauspost/compress/zlib.NewWriter"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/klauspost/compress v1.19.1 // indirect
go.sum
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"log"

	"github.com/klauspost/compress/zlib"
)

func main() {
	input := []byte("Demonstrating zlib.NewWriter compression with github.com/klauspost/compress.")

	var compressedBuf bytes.Buffer
	writer := zlib.NewWriter(&compressedBuf)
	if _, err := writer.Write(input); err != nil {
		log.Fatalf("failed to write compressed data: %v", err)
	}
	if err := writer.Close(); err != nil {
		log.Fatalf("failed to close zlib writer: %v", err)
	}

	reader, err := zlib.NewReader(&compressedBuf)
	if err != nil {
		log.Fatalf("failed to open zlib reader: %v", err)
	}
	defer reader.Close()

	decompressed, err := io.ReadAll(reader)
	if err != nil {
		log.Fatalf("failed to read decompressed data: %v", err)
	}

	fmt.Printf("Original:     %s\n", string(input))
	fmt.Printf("Decompressed: %s\n", string(decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/klauspost/compress/zlib.NewWriter in pkg:golang/github.com/klauspost/compress@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.19.1"
  ],
  "symbols": [
    "github.com/klauspost/compress/zlib.NewWriter"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/zlib"
)

func main() {
	// 1. zlib.NewWriter writes compressed data stream that decompresses to match original uncompressed bytes
	{
		original := []byte("CodeSampleX contract verification: klauspost/compress/zlib.NewWriter standard stream roundtrip test.")
		var buf bytes.Buffer
		zw := zlib.NewWriter(&buf)
		if _, err := zw.Write(original); err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewWriter Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewWriter Close error: %v\n", err)
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewReader error: %v\n", err)
			os.Exit(1)
		}
		decompressed, err := io.ReadAll(zr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewReader ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zr.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewReader Close error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(original, decompressed) {
			fmt.Fprintf(os.Stderr, "decompressed payload mismatch: got %q, want %q\n", string(decompressed), string(original))
			os.Exit(1)
		}
	}

	// 2. zlib.NewWriterLevel initializes a writer with specified compression levels including BestSpeed and BestCompression
	{
		sampleData := bytes.Repeat([]byte("Evaluating compression ratio and integrity across distinct zlib compression levels. "), 30)
		levels := []int{
			zlib.NoCompression,
			zlib.BestSpeed,
			zlib.DefaultCompression,
			zlib.BestCompression,
			zlib.HuffmanOnly,
		}

		for _, lvl := range levels {
			var buf bytes.Buffer
			zw, err := zlib.NewWriterLevel(&buf, lvl)
			if err != nil {
				fmt.Fprintf(os.Stderr, "zlib.NewWriterLevel(%d) error: %v\n", lvl, err)
				os.Exit(1)
			}
			if _, err := zw.Write(sampleData); err != nil {
				fmt.Fprintf(os.Stderr, "zw.Write with level %d error: %v\n", lvl, err)
				os.Exit(1)
			}
			if err := zw.Close(); err != nil {
				fmt.Fprintf(os.Stderr, "zw.Close with level %d error: %v\n", lvl, err)
				os.Exit(1)
			}

			zr, err := zlib.NewReader(&buf)
			if err != nil {
				fmt.Fprintf(os.Stderr, "zlib.NewReader with level %d error: %v\n", lvl, err)
				os.Exit(1)
			}
			decomp, err := io.ReadAll(zr)
			if err != nil {
				fmt.Fprintf(os.Stderr, "zlib.NewReader ReadAll with level %d error: %v\n", lvl, err)
				os.Exit(1)
			}
			if err := zr.Close(); err != nil {
				fmt.Fprintf(os.Stderr, "zlib.NewReader Close with level %d error: %v\n", lvl, err)
				os.Exit(1)
			}
			if !bytes.Equal(sampleData, decomp) {
				fmt.Fprintf(os.Stderr, "level %d roundtrip mismatch\n", lvl)
				os.Exit(1)
			}
		}
	}

	// 3. zlib.NewWriter handles empty input buffers and emits a valid zlib stream on Close
	{
		var buf bytes.Buffer
		zw := zlib.NewWriter(&buf)
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "empty zlib.NewWriter Close error: %v\n", err)
			os.Exit(1)
		}
		if buf.Len() == 0 {
			fmt.Fprintf(os.Stderr, "expected zlib header and footer for empty stream, got 0 bytes\n")
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "empty stream zlib.NewReader error: %v\n", err)
			os.Exit(1)
		}
		out, err := io.ReadAll(zr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "empty stream ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zr.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "empty stream zr.Close error: %v\n", err)
			os.Exit(1)
		}
		if len(out) != 0 {
			fmt.Fprintf(os.Stderr, "expected 0 decompressed bytes, got %d\n", len(out))
			os.Exit(1)
		}
	}

	// 4. zlib.NewWriter Flush flushes pending compressed data to the underlying writer
	{
		var buf bytes.Buffer
		zw := zlib.NewWriter(&buf)
		msg := []byte("buffered message for verifying intermediate Flush operation")
		if _, err := zw.Write(msg); err != nil {
			fmt.Fprintf(os.Stderr, "Flush test Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Flush(); err != nil {
			fmt.Fprintf(os.Stderr, "zw.Flush error: %v\n", err)
			os.Exit(1)
		}
		if buf.Len() == 0 {
			fmt.Fprintf(os.Stderr, "expected flushed bytes in underlying buffer, got 0\n")
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "zw.Close after flush error: %v\n", err)
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewReader after Flush error: %v\n", err)
			os.Exit(1)
		}
		decomp, err := io.ReadAll(zr)
		if err != nil || !bytes.Equal(decomp, msg) {
			fmt.Fprintf(os.Stderr, "flushed stream decomp mismatch: %v\n", err)
			os.Exit(1)
		}
		_ = zr.Close()
	}

	// 5. zlib.NewWriter Reset reinitializes the writer to write to a new destination without reallocating state
	{
		var buf1, buf2 bytes.Buffer
		zw := zlib.NewWriter(&buf1)
		p1 := []byte("first stream payload for reset verification")
		if _, err := zw.Write(p1); err != nil {
			fmt.Fprintf(os.Stderr, "reset test stream 1 write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "reset test stream 1 close error: %v\n", err)
			os.Exit(1)
		}

		zw.Reset(&buf2)
		p2 := []byte("second stream payload written after reset invocation")
		if _, err := zw.Write(p2); err != nil {
			fmt.Fprintf(os.Stderr, "reset test stream 2 write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "reset test stream 2 close error: %v\n", err)
			os.Exit(1)
		}

		zr1, err := zlib.NewReader(&buf1)
		if err != nil {
			fmt.Fprintf(os.Stderr, "reader 1 init error: %v\n", err)
			os.Exit(1)
		}
		out1, err := io.ReadAll(zr1)
		if err != nil || !bytes.Equal(out1, p1) {
			fmt.Fprintf(os.Stderr, "out1 mismatch: %v\n", err)
			os.Exit(1)
		}
		_ = zr1.Close()

		zr2, err := zlib.NewReader(&buf2)
		if err != nil {
			fmt.Fprintf(os.Stderr, "reader 2 init error: %v\n", err)
			os.Exit(1)
		}
		out2, err := io.ReadAll(zr2)
		if err != nil || !bytes.Equal(out2, p2) {
			fmt.Fprintf(os.Stderr, "out2 mismatch: %v\n", err)
			os.Exit(1)
		}
		_ = zr2.Close()
	}

	// 6. zlib.NewWriterLevelDict initializes a writer with a custom dictionary and compresses data successfully
	{
		dict := []byte("specialized terminology dictionary tokens keys values protocol")
		payload := []byte("specialized terminology dictionary protocol message with repeating dictionary words")
		var buf bytes.Buffer
		zwDict, err := zlib.NewWriterLevelDict(&buf, zlib.DefaultCompression, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewWriterLevelDict error: %v\n", err)
			os.Exit(1)
		}
		if _, err := zwDict.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "zwDict.Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zwDict.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "zwDict.Close error: %v\n", err)
			os.Exit(1)
		}

		zrDict, err := zlib.NewReaderDict(&buf, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zlib.NewReaderDict error: %v\n", err)
			os.Exit(1)
		}
		decompDict, err := io.ReadAll(zrDict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "zrDict ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zrDict.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "zrDict Close error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(payload, decompDict) {
			fmt.Fprintf(os.Stderr, "dict decompression mismatch: got %q, want %q\n", string(decompDict), string(payload))
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed.")
}

原始种子者

匿名