44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
//go:build linux || freebsd
|
|
// +build linux freebsd
|
|
|
|
package archive
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type ExtractConfig struct {
|
|
DestDir string
|
|
StripComponents int
|
|
IncludePatterns []string
|
|
ExcludePatterns []string
|
|
PreservePermissions bool
|
|
Overwrite bool
|
|
Verbose bool
|
|
}
|
|
|
|
func DefaultExtractConfig() *ExtractConfig {
|
|
return &ExtractConfig{
|
|
DestDir: ".", StripComponents: 0, IncludePatterns: []string{},
|
|
ExcludePatterns: []string{}, PreservePermissions: true, Overwrite: true, Verbose: false,
|
|
}
|
|
}
|
|
|
|
func ExtractFile(srcPath, destDir string, cfg *ExtractConfig) error {
|
|
if cfg == nil {
|
|
cfg = DefaultExtractConfig()
|
|
}
|
|
srcFile, err := os.Open(srcPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open source file: %w", err)
|
|
}
|
|
defer srcFile.Close()
|
|
return AutoExtract(filepath.Base(srcPath), srcFile, destDir)
|
|
}
|
|
|
|
func ShouldExtractFile(filename string) bool { return IsArchiveFormat(filename) }
|
|
func ListSupportedFormats() []string { return ListSupported() }
|
|
func SupportsFormat(format string) bool { return Supports(format) }
|