generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Compress interface for Xz (#157)
* Implement the Compress interface for XZ * Fix the logic of finding a target file from xz * Move a common funtion extraFile to types.go Co-authored-by: rick <[email protected]>
- Loading branch information
1 parent
b2f3676
commit 38d64ee
Showing
7 changed files
with
121 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package compress | ||
|
||
import ( | ||
"archive/tar" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Compress is a common compress interface | ||
type Compress interface { | ||
ExtractFiles(sourceFile, targetName string) error | ||
} | ||
|
||
func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *tar.Reader) (err error) { | ||
if name != targetName && !strings.HasSuffix(name, "/"+targetName) { | ||
return | ||
} | ||
var targetFile *os.File | ||
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), targetName), | ||
os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil { | ||
return | ||
} | ||
if _, err = io.Copy(targetFile, tarReader); err != nil { | ||
return | ||
} | ||
_ = targetFile.Close() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,79 @@ | ||
package compress | ||
|
||
// TODO implement extract files, for instance, you can use https://github.com/ulikunitz/xz | ||
import ( | ||
"archive/tar" | ||
"errors" | ||
"fmt" | ||
"github.com/xi2/xz" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
// Xz implements a compress which based is based on xz | ||
type Xz struct { | ||
additionBinaries []string | ||
} | ||
|
||
// NewXz creates an instance of Xz | ||
func NewXz(additionBinaries []string) *Xz { | ||
return &Xz{additionBinaries: additionBinaries} | ||
} | ||
|
||
// ExtractFiles extracts files from a target compress file | ||
func (x *Xz) ExtractFiles(sourceFile, targetName string) (err error) { | ||
if targetName == "" { | ||
err = errors.New("target filename is empty") | ||
return | ||
} | ||
var f *os.File | ||
if f, err = os.Open(sourceFile); err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = f.Close() | ||
}() | ||
|
||
// Create a xz Reader | ||
r, err := xz.NewReader(f, 0) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
|
||
var header *tar.Header | ||
var found bool | ||
// Create a tar Reader | ||
tarReader := tar.NewReader(r) | ||
for { | ||
if header, err = tarReader.Next(); err == io.EOF { | ||
err = nil | ||
break | ||
} | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
name := header.Name | ||
|
||
switch header.Typeflag { | ||
case tar.TypeReg: | ||
if err = extraFile(name, targetName, sourceFile, header, tarReader); err == nil { | ||
found = true | ||
} else { | ||
break | ||
} | ||
|
||
for i := range x.additionBinaries { | ||
addition := x.additionBinaries[i] | ||
if err = extraFile(addition, addition, sourceFile, header, tarReader); err != nil { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
if err == nil && !found { | ||
err = fmt.Errorf("cannot find item '%s' from '%s'", targetName, sourceFile) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters