decompressFilesInDir decompresses all compressed (zstd/gzip) files it finds under root (recursively), and removes the compressed files in files with the same name but without the extension. decompressFilesInDir returns the absolute path of all files containing decompressed data.
(tb testing.TB, root string)
| 467 | // same name but without the extension. decompressFilesInDir returns the |
| 468 | // absolute path of all files containing decompressed data. |
| 469 | func decompressFilesInDir(tb testing.TB, root string) []string { |
| 470 | var ( |
| 471 | rm []string // files to delete after a successfull walk |
| 472 | decompressed []string // files with decompressed data |
| 473 | ) |
| 474 | |
| 475 | err := filepath.WalkDir(root, func(fullpath string, dirent fs.DirEntry, err error) error { |
| 476 | if err != nil { |
| 477 | return err |
| 478 | } |
| 479 | |
| 480 | if dirent.IsDir() { |
| 481 | return nil |
| 482 | } |
| 483 | |
| 484 | switch filepath.Ext(fullpath) { |
| 485 | case ".gz", ".zst", ".zstd": |
| 486 | default: |
| 487 | return nil |
| 488 | } |
| 489 | |
| 490 | inf, err := os.Open(fullpath) |
| 491 | if err != nil { |
| 492 | return fmt.Errorf("can't open input file: %v", err) |
| 493 | } |
| 494 | defer inf.Close() |
| 495 | zr, err := zt.NewReader(inf) |
| 496 | if err != nil { |
| 497 | return fmt.Errorf("can't read input file: %v", err) |
| 498 | } |
| 499 | defer zr.Close() |
| 500 | |
| 501 | outPath := strings.TrimSuffix(fullpath, filepath.Ext(fullpath)) |
| 502 | fout, err := os.Create(outPath) |
| 503 | if err != nil { |
| 504 | return fmt.Errorf("can't create output file: %v", err) |
| 505 | } |
| 506 | defer fout.Close() |
| 507 | |
| 508 | if _, err := io.Copy(fout, zr); err != nil { |
| 509 | return err |
| 510 | } |
| 511 | rm = append(rm, fullpath) |
| 512 | decompressed = append(decompressed, outPath) |
| 513 | return nil |
| 514 | }) |
| 515 | |
| 516 | if err != nil { |
| 517 | tb.Fatalf("decompressFilesInDir: error walking directory: %v", err) |
| 518 | } |
| 519 | |
| 520 | for _, name := range rm { |
| 521 | if err := os.Remove(name); err != nil { |
| 522 | tb.Fatalf("after Walk, can't remove %s: %v", name, err) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return decompressed |
no test coverage detected