2

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.

PiKaY
  • 279
  • 4
  • 15

1 Answers1

2

You're appending the same tar.Reader over and over to the files slice. You have to read the file from the archive after you read the header. Just copy the files at that point. You probably should check for regular files too, vs directories, symlinks, etc.

copied := 0
for {
    header, err := tar_reader.Next()
    if err == io.EOF {
        break
    } else if err != nil {
        log.Fatalf("Cannot read file inside tarball %v", err)
    }

    if !(header.Typeflag == tar.TypeReg || header.Typeflag == tar.TypeRegA) {
        continue
    }


    n, err = io.Copy(writer, tar_reader)
    if err != nil {
        log.Fatalf("Cannot write unarchived file %v", err)
    }
    copied += n
}
return copied, nil
JimB
  • 104,193
  • 13
  • 262
  • 255
  • Thanks @JimB but I want to append all the tar_readers to a single multireader. Is that possible? – PiKaY Oct 28 '16 at 14:51
  • @PiKaY: no, because there is only one tar.Reader. To use a MultiReader you would have to copy every file to individual buffers to make separate io.Readers. This is doing exactly what you want, but without the extra copy. – JimB Oct 28 '16 at 14:55
  • Thank You @JimB There was a little constraint due to the organization of my current code. I guess I could fit this :) . Thank You for the answer – PiKaY Oct 28 '16 at 15:02