You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
1 year ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"archive/tar"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/klauspost/compress/zstd"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
tarFile, err := os.Create("test.tar.zst")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer tarFile.Close()
|
||
|
|
||
|
compressor, err := zstd.NewWriter(tarFile, zstd.WithEncoderLevel(4))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer compressor.Close()
|
||
|
|
||
|
tw := tar.NewWriter(compressor)
|
||
|
defer tw.Close()
|
||
|
|
||
|
entries, err := os.ReadDir("/home/cheetah/dev/gma-puzzles/zstd-tar-test/testpayload")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
for _, e := range entries {
|
||
|
originalPath := filepath.Join("/home/cheetah/dev/gma-puzzles/zstd-tar-test/testpayload", e.Name())
|
||
|
|
||
|
file, err := os.Open(originalPath)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
info, err := file.Stat()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
tarFileHeader, err := tar.FileInfoHeader(info, info.Name())
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
err = tw.WriteHeader(tarFileHeader)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
_, err = io.Copy(tw, file)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
fmt.Println(info.Name())
|
||
|
}
|
||
|
|
||
|
err = tw.Close()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
err = compressor.Close()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
err = tarFile.Close()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|