SplitFile will not create an empty file anymore. Also files created by tests will be removed.

This commit is contained in:
h3yEllex 2017-10-24 12:56:50 +03:00
parent 33de3066d5
commit 60cb509ff9
2 changed files with 6 additions and 4 deletions

View File

@ -36,7 +36,7 @@ func CatFiles(files []string, outfile string, remove bool) error {
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) {
file, err := os.Open(fileName)
if err != nil {
@ -62,10 +62,13 @@ func SplitFile(fileName string, numPieces int) (err error) {
for {
n, err := file.Read(buf)
out.Write(buf[:n])
bytesRead += n
if err == io.EOF {
// If written bytes count is smaller than lenght of buffer
// then we don't create one more empty file
if err == io.EOF || n < len(buf) {
break
}
bytesRead += n
if bytesRead >= bytesPerPiece {
// Close file and open a new one
out.Close()

View File

@ -12,5 +12,4 @@ func TestSplitFile(t *testing.T) {
}
os.Remove("README.md.0")
os.Remove("README.md.1")
os.Remove("README.md.2")
}