Skip to content

Commit b86a6c8

Browse files
committed
optimize performance by copying read to write stream, rather than buffering entire files
1 parent 30998be commit b86a6c8

1 file changed

Lines changed: 1 addition & 9 deletions

File tree

tarsplitter.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"flag"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
)
@@ -49,7 +48,6 @@ func main() {
4948
newTarCounter := 0
5049
var byteCounter int64
5150
tr := tar.NewReader(file)
52-
var contents []byte
5351
filesProcessed := 0
5452

5553
var bytesBeforeWrite int64
@@ -81,19 +79,13 @@ func main() {
8179
os.Exit(1)
8280
}
8381

84-
contents, err = ioutil.ReadAll(tr)
85-
if err != nil {
86-
fmt.Println("failed while reading from original archive", info.Name, err)
87-
continue
88-
}
89-
9082
// add the file from the original archive to the new archive
9183
tempInfo, _ = newTarFile.Stat()
9284
bytesBeforeWrite = tempInfo.Size()
9385
if err = newTar.WriteHeader(info); err != nil {
9486
fatal("failed writing header between tars", err)
9587
}
96-
if _, err = newTar.Write(contents); err != nil {
88+
if _, err = io.Copy(newTar, tr); err != nil {
9789
fatal("failed writing file body between tars", err)
9890
}
9991

0 commit comments

Comments
 (0)