Files
NanoKVM-MIRROR/server/utils/untar.go
wj-xiao 6eb4a4ea62 update version to 2.1.6
feat: support downloading image from online URL
feat: add keyboard shortcut Ctrl+Alt+Del
fix: fix the CSRF issue
perf: add an option to configure custom ICE servers
perf: removed unnecessary modifications to DNS configuration
perf: add an SSH enable/disable toggle in the web UI
perf: add a Tailscale enable/disable toggle in the web UI
perf: download Tailscale installation package from the official source
perf: automatic enable/disable GOMEMLIMIT on tailscale start/stop
perf: add JWT configuration
perf: implement secure password storage using bcrypt hashing
perf: implement integrity checks for online updates
refactor: refactor HDMI module and remove the dependency libmaixcam_lib.so
refactor: web terminal use pty instead of SSH
refactor: move Tailscale APIs from the network module to the extensions module
2025-02-14 14:18:13 +08:00

83 lines
1.3 KiB
Go

package utils
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
)
func UnTarGz(srcFile string, destDir string) (string, error) {
if err := os.MkdirAll(destDir, 0755); err != nil {
return "", err
}
fr, err := os.Open(srcFile)
if err != nil {
return "", err
}
defer func() {
_ = fr.Close()
}()
gr, err := gzip.NewReader(fr)
if err != nil {
return "", err
}
defer func() {
_ = gr.Close()
}()
tr := tar.NewReader(gr)
targetFile := ""
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
if targetFile == "" {
parts := strings.Split(header.Name, "/")
if len(parts) > 0 {
targetFile = filepath.Join(destDir, parts[0])
}
}
filename := filepath.Join(destDir, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(filename, os.FileMode(header.Mode)); err != nil {
return "", err
}
case tar.TypeReg:
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return "", err
}
if _, err := io.Copy(file, tr); err != nil {
_ = file.Close()
return "", err
}
_ = file.Close()
case tar.TypeSymlink:
if err := os.Symlink(header.Linkname, filename); err != nil {
return "", err
}
}
}
return targetFile, nil
}