77 "io"
88 "os"
99 "path/filepath"
10- "strings"
1110)
1211
1312// Tar return an tar io.Reader from the gived directory. It returns an error when the file is not a directory.
@@ -44,8 +43,11 @@ func Tar(path *os.File) (io.Reader, error) {
4443 if err != nil {
4544 return errors .New ("(common::tar::Tar::Walk) Error creating '" + file + "' header. " + err .Error ())
4645 }
47- // update the name to correctly reflect the desired destination when untaring
48- header .Name = strings .TrimPrefix (strings .Replace (file , path .Name (), "" , - 1 ), string (filepath .Separator ))
46+ relativePath , err := filepath .Rel (path .Name (), file )
47+ if err != nil {
48+ return errors .New ("(common::tar::Tar::Walk) A relative path on'" + file + "' could not be made from '" + path .Name () + "'. " + err .Error ())
49+ }
50+ header .Name = relativePath
4951
5052 // write the header
5153 if err := tw .WriteHeader (header ); err != nil {
@@ -55,7 +57,7 @@ func Tar(path *os.File) (io.Reader, error) {
5557 // open files for taring
5658 f , err := os .Open (file )
5759 if err != nil {
58- return err
60+ return errors . New ( "(common::tar::Tar::Walk) Error opening '" + file + "'. " + err . Error ())
5961 }
6062
6163 if _ , err := io .Copy (tw , f ); err != nil {
@@ -68,7 +70,7 @@ func Tar(path *os.File) (io.Reader, error) {
6870 return nil
6971 })
7072 if err != nil {
71- return nil , errors .New ("(common::tar::Tar) Error explorint '" + path .Name () + "'. " + err .Error ())
73+ return nil , errors .New ("(common::tar::Tar) Error exploring '" + path .Name () + "'. " + err .Error ())
7274 }
7375
7476 return bytes .NewReader (tarBuff .Bytes ()), nil
0 commit comments