aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-01 16:01:57 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-01 16:01:57 +0200
commit34a84a44b4dfa513d8ceb1cfeec50ac78fb311e0 (patch)
tree8647ba8b5c5b6b6ce9387076f0cf7115d9951e95
parentba102f52038ee1a47352443a44e8ccd4e20bcbcb (diff)
downloaddna-backup-34a84a44b4dfa513d8ceb1cfeec50ac78fb311e0.tar.gz
dna-backup-34a84a44b4dfa513d8ceb1cfeec50ac78fb311e0.zip
first add of tar streamer
-rw-r--r--tar.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/tar.go b/tar.go
new file mode 100644
index 0000000..e811840
--- /dev/null
+++ b/tar.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "archive/tar"
+ "io"
+ "log"
+ "os"
+)
+
+func streamFilesTar(files []File, stream io.WriteCloser) {
+ tarStream := tar.NewWriter(stream)
+ for _, f := range files {
+ file, err := os.Open(f.Path)
+ if err != nil {
+ log.Printf("Error reading file '%s': %s\n", f.Path, err)
+ continue
+ }
+ stat, err := file.Stat()
+ if err != nil {
+ log.Printf("Error getting stat of file '%s': %s\n", f.Path, err)
+ continue
+ }
+ hdr, err := tar.FileInfoHeader(stat, "")
+ if err != nil {
+ log.Printf("Error creating tar header for file '%s': %s\n", f.Path, err)
+ continue
+ }
+ if err := tarStream.WriteHeader(hdr); err != nil {
+ log.Printf("Error writing tar header to stream for file '%s': %s\n", f.Path, err)
+ continue
+ }
+ if _, err := io.Copy(tarStream, file); err != nil {
+ log.Printf("Error writing file to stream '%s': %s\n", f.Path, err)
+ continue
+ }
+ }
+ if err := tarStream.Close(); err != nil {
+ log.Fatal("Error closing tar stream:", err)
+ }
+ if err := stream.Close(); err != nil {
+ log.Fatal("Error closing stream:", err)
+ }
+}