The tar package in the Go standard library implements access to compressed files in the tar format. The goal of this package is to cover most tar variants, including GNU and BSD-generated tar files. The previous sentence is said in the go standard library. In fact, this package can be used, and then it can be run on windows, linux, mac, and unix systems, so that it can be used across platforms without knowing the underlying implementation of tar files of different operating systems.
Next, learn the use of this standard library:
First, the first is the constants in this package
- const (
- // Types
- TypeReg = '0' // normal file
- TypeRegA = '\x00' // normal file
- TypeLink = '1' // hard link
- TypeSymlink = '2' // symbolic link, soft link
- TypeChar = '3' // character device node
- TypeBlock = '4' // block device node
- TypeDir = '5' // directory
- TypeFifo = '6' // fifo node
- TypeCont = '7' // reserved
- TypeXHeader = 'x' // expandable header
- TypeXGlobalHeader = 'g' // global extension header
- TypeGNULongName = 'L' // Next file has a long name
- TypeGNULongLink = 'K' // Next file symlinks to a file w/ a long name
- TypeGNUSparse = 'S' // sparse file
- )
Reference for in-depth understanding of hard links and soft links: https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/
Character devices: devices such as keyboards, printers, etc.
Block device: This is the device that stores data in block units, such as U disk, SD card, hard disk, etc.
Websites to help understand:
https://blog.csdn.net/u013904227/article/details/50483662
https://baike.baidu.com/item/%E5%9D%97%E8%AE%BE%E5%A4%87/2413231?fr=aladdin,https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6%E8%AE%BE%E5%A4%87
Sparse files: https://blog.csdn.net/cymm_liu/article/details/8760033
variable
- where (
- ErrWriteTooLong = errors .New("archive/tar: write too long") //The write data is too long
- ErrFieldTooLong = errors.New("archive/tar: header field too long") //头部太长
- ErrWriteAfterClose = errors .New("archive/tar: write after close") //Write after close
- )
- where (
- ErrHeader = errors.New("archive/tar: invalid tar header") //无效tar 头部
- )
type Header //该结构体代表了一个tar归档的头部,一些字段可能不被填充,Header中主要包含文件相关信息。
- type Header struct {
- Name string // 文件名称
- Mode int64 // 文件的权限和模式位
- Uid int // 文件所有者的用户 ID
- Gid int // 文件所有者的组 ID
- Size int64 // 文件的字节长度
- ModTime time.Time // 文件的修改时间
- Typeflag byte // 文件的类型
- Linkname string // 链接文件的目标名称
- Uname string // 文件所有者的用户名
- Gname string // 文件所有者的组名
- Devmajor int64 // 字符设备或块设备的主设备号
- Devminor int64 // 字符设备或块设备的次设备号
- AccessTime time.Time // 文件的访问时间
- ChangeTime time.Time // 文件的状态更改时间
- }
func FileInfoHeader(fi os.FileInfo, link string) (*Header, error)//该函数通过os.fileInfo便可创建一个Header,Header中大部分内容自动填充,一些内容需要自己设定。
func (h *Header) FileInfo() os.FileInfo //该函数获取该Header的os.FileInfo信息
fileinfo头信息:
package main import ( "os" "fmt" ) func main() { fileinfo, err := os.Stat("D:\\go_code\\1.tar") if err != nil { panic(err) } var type1 string if fileinfo.IsDir() { fmt.Println("这是一个目录") type1 = "目录" } else { fmt.Println("这是一个文件") type1 = "file" } fmt.Println(type1 + "information: " ) fmt.Println( "The file name of " + type1 + ": " + fileinfo.Name()) fmt.Println("该" + type1 + "的大小:", float32(fileinfo.Size()) / 1024.0, "KB") fmt.Println( "Permission of "this" + type1 + ":" , fileinfo.Mode()) fmt.Println( "The modification time of this" + type1 + ":" , fileinfo.ModTime()) }
The article is gradually improving. . .