Giter Site home page Giter Site logo

go-cpio's Introduction

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()
}

go-cpio's People

Contributors

cavaliercoder avatar stapelberg avatar thegrumpylion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.