コード例 #1
0
ファイル: deb_writer.go プロジェクト: gussan/debgo-v0.2
func (bdeb *DebWriter) Build(tempDir, destDir string) error {
	wtr, err := os.Create(filepath.Join(destDir, bdeb.Filename))
	if err != nil {
		return err
	}
	defer wtr.Close()

	aw := ar.NewWriter(wtr)

	err = bdeb.writeBytes(aw, "debian-binary", []byte(bdeb.DebianBinaryVersion+"\n"))
	if err != nil {
		return fmt.Errorf("Error writing debian-binary into .ar archive: %v", err)
	}
	err = bdeb.writeFromFile(aw, filepath.Join(tempDir, bdeb.ControlArchive))
	if err != nil {
		return fmt.Errorf("Error writing control archive into .ar archive: %v", err)
	}
	err = bdeb.writeFromFile(aw, filepath.Join(tempDir, bdeb.DataArchive))
	if err != nil {
		return fmt.Errorf("Error writing data archive into .ar archive: %v", err)
	}
	err = aw.Close()
	if err != nil {
		return fmt.Errorf("Error closing .ar archive: %v", err)
	}
	return nil
}
コード例 #2
0
ファイル: example_test.go プロジェクト: himanshugpt/evergreen
func Example() {
	// Create a buffer to write our archive to.
	wtr := new(bytes.Buffer)

	// Create a new ar archive.
	aw := ar.NewWriter(wtr)

	// 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 licence."},
	}
	for _, file := range files {
		hdr := &ar.Header{
			Name: file.Name,
			Size: int64(len(file.Body)),
		}
		if err := aw.WriteHeader(hdr); err != nil {
			log.Fatalln(err)
		}
		if _, err := aw.Write([]byte(file.Body)); err != nil {
			log.Fatalln(err)
		}
	}
	// Make sure to check the error on Close.
	if err := aw.Close(); err != nil {
		log.Fatalln(err)
	}

	// Open the ar archive for reading.
	rdr := bytes.NewReader(wtr.Bytes())

	arr, err := ar.NewReader(rdr)
	if err != nil {
		log.Fatalln(err)
	}

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

	// Output:
	// Contents of readme.txt:
	// This archive contains some text files.
	// Contents of gopher.txt:
	// Gopher names:
	// George
	// Geoffrey
	// Gonzo
	// Contents of todo.txt:
	// Get animal handling licence.
}