workgroups/vendor/github.com/cavaliercoder/go-cpio
Marvin Preuss 1d4ae27878
All checks were successful
continuous-integration/drone/push Build is passing
ci: drone yaml with reusable anchors
2021-09-24 17:34:17 +02:00
..
.gitignore ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
.travis.yml ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
cpio.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
fileinfo.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
fuzz.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
hash.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
header.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
LICENSE ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
Makefile ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
reader.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
README.md ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
svr4.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00
writer.go ci: drone yaml with reusable anchors 2021-09-24 17:34:17 +02:00

go-cpio GoDoc Build Status Go Report Card

This package provides a Go native implementation of the CPIO archive file format.

Currently, only the SVR4 (New ASCII) format is supported, both with and without checksums.

// Create a buffer to write our archive to.
buf := new(bytes.Buffer)

// Create a new cpio archive.
w := cpio.NewWriter(buf)

// Add some files to the archive.
var files = []struct {
  Name, Body string
}{
  {"readme.txt", "This archive contains some text files."},
  {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
  {"todo.txt", "Get animal handling license."},
}
for _, file := range files {
  hdr := &cpio.Header{
    Name: file.Name,
    Mode: 0600,
    Size: int64(len(file.Body)),
  }
  if err := w.WriteHeader(hdr); err != nil {
    log.Fatalln(err)
  }
  if _, err := w.Write([]byte(file.Body)); err != nil {
    log.Fatalln(err)
  }
}
// Make sure to check the error on Close.
if err := w.Close(); err != nil {
  log.Fatalln(err)
}

// Open the cpio archive for reading.
b := bytes.NewReader(buf.Bytes())
r := cpio.NewReader(b)

// Iterate through the files in the archive.
for {
  hdr, err := r.Next()
  if err == io.EOF {
    // end of cpio archive
    break
  }
  if err != nil {
    log.Fatalln(err)
  }
  fmt.Printf("Contents of %s:\n", hdr.Name)
  if _, err := io.Copy(os.Stdout, r); err != nil {
    log.Fatalln(err)
  }
  fmt.Println()
}