gitee.com/h79/goutils@v1.22.10/common/archive/tarxz/tarxz.go (about) 1 // Package tarxz implements the Archive interface providing tar.xz archiving 2 // and compression. 3 package tarxz 4 5 import ( 6 "gitee.com/h79/goutils/common/archive/tar" 7 fileconfig "gitee.com/h79/goutils/common/file/config" 8 "io" 9 10 "github.com/ulikunitz/xz" 11 ) 12 13 // Archive as tar.xz. 14 type Archive struct { 15 xzw *xz.Writer 16 tw *tar.Archive 17 } 18 19 // New tar.xz archive. 20 func New(target io.Writer) Archive { 21 xzw, _ := xz.WriterConfig{DictCap: 16 * 1024 * 1024}.NewWriter(target) 22 tw := tar.New(xzw) 23 return Archive{ 24 xzw: xzw, 25 tw: &tw, 26 } 27 } 28 29 // Close all closeables. 30 func (a Archive) Close() error { 31 if a.tw != nil { 32 err := a.tw.Close() 33 a.tw = nil 34 if err != nil { 35 return err 36 } 37 } 38 if a.xzw != nil { 39 err := a.xzw.Close() 40 a.xzw = nil 41 if err != nil { 42 return err 43 } 44 } 45 return nil 46 } 47 48 // Add file to the archive. 49 func (a Archive) Add(f fileconfig.File, stream ...fileconfig.ReaderStream) error { 50 return a.tw.Add(f, stream...) 51 }