archive / tar

        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

[html]  view plain copy  
  1. const (  
  2.   
  3.     // Types  
  4.     TypeReg            =  '0'     // normal file  
  5.     TypeRegA           =  '\x00'  // normal file  
  6.     TypeLink           =  '1'     // hard link  
  7.     TypeSymlink        =  '2'     // symbolic link, soft link  
  8.     TypeChar           =  '3'     // character device node  
  9.     TypeBlock          =  '4'     // block device node  
  10.     TypeDir            =  '5'     // directory  
  11.     TypeFifo          = '6'    // fifo node  
  12.     TypeCont           =  '7'     // reserved  
  13.     TypeXHeader        =  'x'     // expandable header  
  14.     TypeXGlobalHeader  =  'g'     // global extension header  
  15.     TypeGNULongName   = 'L'    // Next file has a long name  
  16.     TypeGNULongLink   = 'K'    // Next file symlinks to a file w/ a long name  
  17.     TypeGNUSparse      =  'S'     // sparse file  
  18. )  

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

[html]  view plain copy  
  1. where (  
  2.     ErrWriteTooLong     =  errors .New("archive/tar: write too long") //The write data is too long  
  3.     ErrFieldTooLong    = errors.New("archive/tar: header field too long") //头部太长  
  4.     ErrWriteAfterClose  =  errors .New("archive/tar: write after close") //Write after close  
  5. )  
  6.   
  7. where (  
  8.     ErrHeader = errors.New("archive/tar: invalid tar header")  //无效tar 头部  
  9. )  


type Header //该结构体代表了一个tar归档的头部,一些字段可能不被填充,Header中主要包含文件相关信息。

[html]  view plain  copy
  1. type Header struct {  
  2.    Name       string    // 文件名称  
  3.    Mode       int64     // 文件的权限和模式位  
  4.    Uid        int       // 文件所有者的用户 ID  
  5.    Gid        int       // 文件所有者的组 ID  
  6.    Size       int64     // 文件的字节长度  
  7.    ModTime    time.Time // 文件的修改时间  
  8.    Typeflag   byte      // 文件的类型  
  9.    Linkname   string    // 链接文件的目标名称  
  10.    Uname      string    // 文件所有者的用户名  
  11.    Gname      string    // 文件所有者的组名  
  12.    Devmajor   int64     // 字符设备或块设备的主设备号  
  13.    Devminor   int64     // 字符设备或块设备的次设备号  
  14.    AccessTime time.Time // 文件的访问时间  
  15.    ChangeTime time.Time // 文件的状态更改时间  
  16. }  

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. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325822007&siteId=291194637
tar