mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
SplitFile will not create an empty file anymore. Also files created by tests will be removed.
This commit is contained in:
parent
33de3066d5
commit
60cb509ff9
2 changed files with 6 additions and 4 deletions
9
utils.go
9
utils.go
|
@ -36,7 +36,7 @@ func CatFiles(files []string, outfile string, remove bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SplitFile
|
// SplitFile creates a bunch of smaller files with the data from source splited into them
|
||||||
func SplitFile(fileName string, numPieces int) (err error) {
|
func SplitFile(fileName string, numPieces int) (err error) {
|
||||||
file, err := os.Open(fileName)
|
file, err := os.Open(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -62,10 +62,13 @@ func SplitFile(fileName string, numPieces int) (err error) {
|
||||||
for {
|
for {
|
||||||
n, err := file.Read(buf)
|
n, err := file.Read(buf)
|
||||||
out.Write(buf[:n])
|
out.Write(buf[:n])
|
||||||
bytesRead += n
|
// If written bytes count is smaller than lenght of buffer
|
||||||
if err == io.EOF {
|
// then we don't create one more empty file
|
||||||
|
if err == io.EOF || n < len(buf) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
bytesRead += n
|
||||||
|
|
||||||
if bytesRead >= bytesPerPiece {
|
if bytesRead >= bytesPerPiece {
|
||||||
// Close file and open a new one
|
// Close file and open a new one
|
||||||
out.Close()
|
out.Close()
|
||||||
|
|
|
@ -12,5 +12,4 @@ func TestSplitFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
os.Remove("README.md.0")
|
os.Remove("README.md.0")
|
||||||
os.Remove("README.md.1")
|
os.Remove("README.md.1")
|
||||||
os.Remove("README.md.2")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue