MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / DecompressGzFile

Method DecompressGzFile

agent/utils/files/file_op.go:1269–1317  ·  view source on GitHub ↗
(ctx context.Context, srcFile, dst string)

Source from the content-addressed store, hash-verified

1267}
1268
1269func (f FileOp) DecompressGzFile(ctx context.Context, srcFile, dst string) error {
1270 var archiveModTime time.Time
1271 if st, err := f.Fs.Stat(srcFile); err == nil {
1272 archiveModTime = st.ModTime()
1273 }
1274
1275 in, err := f.Fs.Open(srcFile)
1276 if err != nil {
1277 return fmt.Errorf("open source file failed: %w", err)
1278 }
1279 defer in.Close()
1280
1281 gr, err := gzip.NewReader(&contextReader{ctx: ctx, r: in})
1282 if err != nil {
1283 return fmt.Errorf("gzip reader creation failed: %w", err)
1284 }
1285 defer gr.Close()
1286
1287 outName := ""
1288 if gr.Name != "" {
1289 outName = filepath.Base(gr.Name)
1290 }
1291 if outName == "" || outName == "." {
1292 outName = strings.TrimSuffix(filepath.Base(srcFile), ".gz")
1293 }
1294 outPath := filepath.Join(dst, outName)
1295 parentDir := filepath.Dir(outPath)
1296 if !f.Stat(parentDir) {
1297 if err := f.Fs.MkdirAll(parentDir, 0755); err != nil {
1298 return err
1299 }
1300 }
1301
1302 fw, err := f.Fs.OpenFile(outPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
1303 if err != nil {
1304 return fmt.Errorf("create output file failed: %w", err)
1305 }
1306 defer fw.Close()
1307
1308 if _, err := io.Copy(fw, gr); err != nil {
1309 return fmt.Errorf("copy content failed: %w", err)
1310 }
1311
1312 if !archiveModTime.IsZero() {
1313 _ = os.Chtimes(outPath, archiveModTime, archiveModTime)
1314 }
1315
1316 return nil
1317}
1318
1319func (f FileOp) TarGzCompressPro(withDir bool, src, dst, secret, exclusionRules string) error {
1320 if !f.Stat(path.Dir(dst)) {

Callers 1

decompressWithSDKMethod · 0.95

Calls 6

StatMethod · 0.95
ModTimeMethod · 0.80
MkdirAllMethod · 0.80
OpenFileMethod · 0.80
CopyMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected