I am trying to copy the extracted content of a tar.gz to a single file in go lang. I will have only text inside the files.
The following is my current code.
f, err := os.Open(fullpath)
if err != nil {
log.Panicf("Can not open file %s: %v", fullpath, err)
return ""
}
defer f.Close()
reader, err := gzip.NewReader(f)
if err != nil{
log.Panicf("Cannot read gzip archive %v", err);
return ""
}
defer reader.Close()
tar_reader := tar.NewReader(reader)
var files []io.Reader
for{
header, err := tar_reader.Next()
if err == io.EOF {
break
} else if err != nil {
log.Panicf("Cannot read file inside tarball %v", err);
return 0, err
} else {
log.Println("Parsing file : ", header.Name)
files = append(files, tar_reader)
}
}
reader = io.MultiReader(files...)
writer, err := os.Create(target)
if err != nil {
log.Panicf("Cannot write unarchived file %v", err);
return 0, err
}
defer writer.Close()
result,err := io.Copy(writer, reader)
if(err != nil) {
log.Panicf("Cannot write unarchived file %v", err);
}
return result,err
While I am not getting any error with the above code, the files are just not read. The output has no data. Please point me on where I am going wrong.