mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
lib/suitar: add new Go package
$ TZ=UTC tar -tvf test/data/archive.dense.suitar
-rw-r--r-- nobody/nobody 0 2020-02-05 06:10 artificial/0.bytes
-rw-r--r-- nobody/nobody 853 2020-08-12 14:04 github-tags.json
-rwxr-xr-x nobody/nobody 693 2021-05-03 06:19 hello.sh
-rw-r--r-- nobody/nobody 104 2021-05-03 06:16 non-ascii/αβ.txt
-rw-r--r-- nobody/nobody 151 2021-05-03 06:23 non-ascii/😻.txt
-rw-r--r-- nobody/nobody 208 2020-02-05 06:10 pjw-thumbnail.png
-rw-r--r-- nobody/nobody 942 2020-02-05 06:10 romeo.txt
-rw-r--r-- nobody/nobody 558 2020-02-05 06:10 romeo.txt.gz
This commit is contained in:
1 parent
ce863060d6
commit
504f8485f4
7 files changed
+1153
No files matched your search
@@ -0,0 +1,87 @@
|
||||
// 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.
|
||||
}
|
||||
Reference in new issue
Block a user