Add SplitFile func

This commit is contained in:
Zack Scholl 2017-10-20 17:26:04 -06:00
parent 7e6169dc03
commit 746f1fe193
2 changed files with 60 additions and 0 deletions

View File

@ -4,9 +4,56 @@ import (
"crypto/md5"
"fmt"
"io"
"math"
"os"
"strconv"
)
// SplitFile
func SplitFile(fileName string, numPieces int) (err error) {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
bytesRead := 0
i := 0
out, err := os.Create(fileName + "." + strconv.Itoa(i))
if err != nil {
return err
}
buf := make([]byte, 4096)
if bytesPerPiece < 4096/numPieces {
buf = make([]byte, bytesPerPiece)
}
for {
n, err := file.Read(buf)
out.Write(buf[:n])
bytesRead += n
if err == io.EOF {
break
}
if bytesRead >= bytesPerPiece {
// Close file and open a new one
out.Close()
i++
out, err = os.Create(fileName + "." + strconv.Itoa(i))
if err != nil {
return err
}
bytesRead = 0
}
}
out.Close()
return nil
}
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.

13
utils_test.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"testing"
)
func TestSplitFile(t *testing.T) {
err := SplitFile("README.md", 3)
if err != nil {
t.Error(err)
}
}