The TAR package implements access to the TAR archive file, which is designed to cover most types, including GNU and BSD-generated tars.
Constant
Const ( //Types typereg = ' 0 ' //Normal file typerega = ' \x00 '//plain file typelink = ' 1 ' // Hard Connect typesymlink = ' 2 ' //symbol connection, soft connect Typechar = ' 3 ' //Character device node typeblock = ' 4 ' //Block device node Typedir = ' 5 ' //directory Typefifo = ' 6 ' //FIFO node typecont = ' 7 ' //reserved Typexheader = ' x ' //expandable head typexglobalheader = ' g '// global expansion head typegnulongname = ' L ' //Next file has a long name Typegnulonglink = ' K ' //Next file symlinks to a file w/a long name typegnusparse = ' S ' //sparse file)
Variable
var ( errwritetoolong = errors. New ("Archive/tar:write Too Long")//write data too long errfieldtoolong = errors. New ("archive/tar:header field Too Long")//head too long errwriteafterclose = errors. New ("Archive/tar:write after Close") //closed after write) var ( errheader = errors. New ("Archive/tar:invalid tar header") //Invalid tar head)
Type header//The struct represents the head of a tar archive, some fields may not be populated, and the header contains information about the file primarily.
Type Header struct { name string //File name mode Int64 //file permissions and mode bit Uid int // The file owner's user ID Gid int //file owner's group ID Size Int64 //file byte length modtime time . Time//File modified typeflag byte //File type linkname string //link file target name Uname String //file owner's user name gname string //file owner's group name devmajor Int64 //character device or block device's main device number devminor Int64 //character device or block device the secondary device number accesstime time. Time//file access times changetime. Time//File status change times}
Func Fileinfoheader (fi os. FileInfo, link string) (*header, error)//The function can create a header,header of most of the content automatically by Os.fileinfo, and some content needs to be set by itself.
Func (H *header) FileInfo () OS. FileInfo//This function gets the Os.fileinfo information for the header
To illustrate header usage:
Package Mainimport ("Archive/tar" "FMT" "OS") Func main () {FileInfo, err: = OS. Stat ("/home/chenbaoke/test.go") if err! = Nil {fmt. PRINTLN (Err)}h, err: = Tar. Fileinfoheader (FileInfo, "") H.linkname = "haha" h.gname = "Test" if err! = Nil {fmt. PRINTLN (Err)}fmt. Println (H.accesstime, H.changetime, H.devmajor, H.devminor, H.gid, H.gname, H.linkname, H.modtime, H.Mode, H.Name, H. Size, H.typeflag, H.uid, H.uname, H.xattrs)} The output results are as follows: 2015-08-28 21:26:03.636592126 +0800 CST 2015-08-28 21:26:03.092592112 +0800 CST 0 0 test haha 2015-08-28 21:26:03.092592112 +0800 CST 33206 test.go 581 [map[] Visible, through Fileinfoheader can create Tar.header, and automatically fill out most of the information in Tar.header, of course, there is still some information not from the OS. FileInfo, so you need to add yourself, such as Linkname,gname.
type Reader
Type Reader struct {r io. Reader ERR Error pad Int64//Current file entity is populated after Curr Numbytesreader//Current file entity's Reader Hdrbuff [bl Ocksize]byte//Read the buffer cache used in the header}<span style= "FONT-SIZE:12PX;" ></span>
Func newreader (R io. Reader) *reader//create a new reader from R
func (tr *reader) Next (*header, error)//The function points to the next entity in the tar file, returning IO at the end of the input. EOF
Func (tr *reader) read (b []byte) (n int, err error)//The function reads the current entity in tar and returns IO when it reads to the end position of the entity. EOF, when next is called, reads the next entity.
The method of extracting the package, and reading the data from the. tar file is through tar. Reader, so the first thing to do is to create tar. Reader, can pass tar. Newreader method to create it, the method requires an OS to be provided. Reader object in order to read data from the object. You can open a. tar file first, and then make the file available to tar. Newreader use. This allows you to read the data in the. tar file:
examples of how to use it:
Package Mainimport ("Archive/tar" "FMT" "IO" "OS") Func main () {f, err: = OS. Open ("/home/chenbaoke/10.tar") if err! = Nil {fmt. PRINTLN (Err) Return}defer F.close () r: = Tar. Newreader (f) for HDR, ERR: = R.next (); Err! = Io. EOF; HDR, err = R.next () {if err! = Nil {fmt. PRINTLN (err) Return}fileinfo: = Hdr. FileInfo () fmt. Println (FileInfo. Name ()) F, err: = OS. Create ("/home/chenbaoke/develop/" + FileInfo. Name ()) if err! = Nil {fmt. PRINTLN (Err)}defer F.close () _, err = Io. Copy (f, R) If Err! = Nil {fmt. PRINTLN (Err)}}}
Type Writer
Type Writer struct { w io. Writer Err error NB Int64//unread bytes pad Int64//The number of writes required after the entity closed bool usedbinary BOOL //Whether the binary numeric field extension was used Preferpax bool //Use pax Heade R instead of binary numeric header hdrbuff [blocksize]byte//buffer to use in Writeheader when writing a Regula R header Paxhdrbuff [blocksize]byte//buffer to use in Writeheader when writing a pax header }
func newwriter (w io. Writer) *writer//Create a new writer and write to W.
func (TW *writer) Close () error//Closes the TAR archive file and writes the uncommitted data to the underlying Writer.
Func (TW *writer) Flush () error//Finish writing the current file
Func (TW *writer) Write (b []byte) (n int, err error)
func (TW *writer) writeheader (HDR *header) Error//This function writes HDR to the tar file, and if HDR is not the first Header, the function calls flush. After calling close, the function is called with an error errwriteafterclose.
Provides an example of how to write a tar file.
Package Mainimport ("Archive/tar" "FMT" "IO" "OS") Func main () {f, err: = OS. Create ("/home/chenbaoke/10.tar") //Creates a tar file if err! = Nil {fmt. PRINTLN (Err) return}defer f.close () TW: = tar. Newwriter (f) defer tw. Close () FileInfo, err: = OS. Stat ("/home/chenbaoke/1.go") //Get file related information if err! = Nil {fmt. PRINTLN (Err)}HDR, err: = Tar. Fileinfoheader (FileInfo, "") if err! = Nil {fmt. PRINTLN (err)}err = tw. Writeheader (HDR) //write header file information if Err! = Nil {fmt. PRINTLN (ERR)//RETURN}F1, err: = OS. Open ("/home/chenbaoke/1.go") if err! = Nil {fmt. PRINTLN (Err) return}m, err: = Io. Copy (TW, F1) //writes the information in file 1.go to the compressed package if err! = Nil {fmt. PRINTLN (ERR)//RETURN}FMT. Println (M)}
Reference: https://golang.org/pkg/archive/tar/
Golove Blog: http://www.cnblogs.com/golove/p/3454630.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Archive/tar Package usage in Golang