blob: 39d9d83acd4932394f3cb32ad84362043500e12e [file]
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
package suitar_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"time"
"github.com/google/wuffs/lib/suitar"
)
func Example_minimal() {
// This mimics Example_minimal from the standard library's archive/tar.
//
// This package's Writer is a little more strict, rejecting an (implicit)
// zero-valued Header.Typeflag or Header.ModTime that archive/tar accepts.
// Header.Mode must also be 0o644 (or 0o755), not 0o600.
// Create and add some files to the archive.
var buf bytes.Buffer
tw := suitar.NewWriter(&buf)
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 := &suitar.Header{
Typeflag: suitar.TypeReg,
Name: file.Name,
Size: int64(len(file.Body)),
Mode: suitar.Mode644,
ModTime: time.Unix(0, 0),
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatal(err)
}
if _, err := tw.Write([]byte(file.Body)); err != nil {
log.Fatal(err)
}
}
if err := tw.Close(); err != nil {
log.Fatal(err)
}
// Open and iterate through the files in the archive.
tr := suitar.NewReader(&buf)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("Contents of %s:\n", hdr.Name)
if _, err := io.Copy(os.Stdout, tr); err != nil {
log.Fatal(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 license.
}