mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Add SplitFile func
This commit is contained in:
parent
7e6169dc03
commit
746f1fe193
2 changed files with 60 additions and 0 deletions
47
utils.go
47
utils.go
|
@ -4,9 +4,56 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"os"
|
"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
|
// 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
|
// 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.
|
// between the two files. If that fail, copy the file contents from src to dst.
|
||||||
|
|
13
utils_test.go
Normal file
13
utils_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue