mirror of https://github.com/schollz/croc.git
Add SplitFile func
This commit is contained in:
parent
7e6169dc03
commit
746f1fe193
47
utils.go
47
utils.go
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitFile(t *testing.T) {
|
||||
err := SplitFile("README.md", 3)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue