FireinDark的个人博客

Go语言标准库-tar

Go

标准库:tar

import “archive/tar”

一、常量

  1. Header.Typeflag
    其中只有0表示正常文件, 1-6只有文件头信息,Body为空
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
TypeReg  = '0'

// 1-6只有文件头,文件信息为空
TypeLink = '1' // 硬链接
TypeSymlink = '2' // 软链接
TypeChar = '3' // 字符设备节点
TypeBlock = '4' // 块设备节点
TypeDir = '5' // 目录节点
TypeFifo = '6' // 栈节点

// 保留
TypeCont = '7'

// Type 'x' is used by the PAX format to store key-value records that
// are only relevant to the next file.
// This package transparently handles these types.
TypeXHeader = 'x'

// Type 'g' is used by the PAX format to store key-value records that
// are relevant to all subsequent files.
// This package only supports parsing and composing such headers,
// but does not currently support persisting the global state across files.
TypeXGlobalHeader = 'g'

// Type 'S' indicates a sparse file in the GNU format.
TypeGNUSparse = 'S'

// Types 'L' and 'K' are used by the GNU format for a meta file
// used to store the path or link name for the next file.
// This package transparently handles these types.
TypeGNULongName = 'L'
TypeGNULongLink = 'K'

二、变量

1
2
3
4
5
6
var (
ErrHeader = errors.New("archive/tar: invalid tar header")
ErrWriteTooLong = errors.New("archive/tar: write too long")
ErrFieldTooLong = errors.New("archive/tar: header field too long")
ErrWriteAfterClose = errors.New("archive/tar: write after close")
)

三、类

  1. Format
    tar文件呈现的格式原生格式是Unix V7.后来为了克服V7的一些局限性,发展了USTAR, PAX, and GNU 等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                各种格式的容量限制
| USTAR | PAX | GNU
------------------+--------+-----------+----------
Name | 256B | unlimited | unlimited
Linkname | 100B | unlimited | unlimited
Size | uint33 | unlimited | uint89
Mode | uint21 | uint21 | uint57
Uid/Gid | uint21 | unlimited | uint57
Uname/Gname | 32B | unlimited | 32B
ModTime | uint33 | unlimited | int89
AccessTime | n/a | unlimited | int89
ChangeTime | n/a | unlimited | int89
Devmajor/Devminor | uint21 | uint21 | uint57
------------------+--------+-----------+----------
string encoding | ASCII | UTF-8 | binary
sub-second times | no | yes | no
sparse files | no | yes | yes
  1. Header
    如果需要改变从Reader.Next()取得的Header信息,需要使用Writer.WriteHeader创建一个新的Header。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type Header struct {
Typeflag byte // 文件类型,默认为0

Name string // 文件名
Linkname string // Target name of link (valid for TypeLink or TypeSymlink)

Size int64 // 文件大小, Body的长度
Mode int64 // 权限如 0600
Uid int // User ID of owner
Gid int // Group ID of owner
Uname string // User name of owner
Gname string // Group name of owner

ModTime time.Time // Modification time
AccessTime time.Time // Access time (requires either PAX or GNU support)
ChangeTime time.Time // Change time (requires either PAX or GNU support)

Devmajor int64 // Major device number (valid for TypeChar or TypeBlock)
Devminor int64 // Minor device number (valid for TypeChar or TypeBlock)
Xattrs map[string]string // Go 1.3

PAXRecords map[string]string // Go 1.10

Format Format // Go 1.10
}
加载评论
打赏本文
二维码

9

文章

3

标签

 文章分类