0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

fix file split bugs and sending server:port display error

This commit is contained in:
陈博 2018-02-07 16:24:44 +08:00
parent 33e41b2b0a
commit 407d85270c
3 changed files with 13 additions and 29 deletions

View file

@ -13,7 +13,7 @@ import (
"sync" "sync"
"time" "time"
humanize "github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/schollz/progressbar" "github.com/schollz/progressbar"
"github.com/schollz/tarinator-go" "github.com/schollz/tarinator-go"

View file

@ -254,7 +254,7 @@ func receiveMessage(connection net.Conn) string {
logger.Warn("read deadline, no response") logger.Warn("read deadline, no response")
return "" return ""
} }
return strings.Replace(string(messageByte), ":", "", -1) return strings.TrimRight(string(messageByte), ":")
} }
func fillString(retunString string, toLength int) string { func fillString(retunString string, toLength int) string {

View file

@ -49,38 +49,22 @@ func SplitFile(fileName string, numPieces int) (err error) {
} }
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces))) bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
bytesRead := 0
i := 0 buf := make([]byte, bytesPerPiece)
out, err := os.Create(fileName + "." + strconv.Itoa(i)) for i := 0; i < numPieces; i++ {
if err != nil {
return err out, err := os.Create(fileName + "." + strconv.Itoa(i))
} if err != nil {
buf := make([]byte, 4096) return err
if bytesPerPiece < 4096/numPieces { }
buf = make([]byte, bytesPerPiece)
}
for {
n, err := file.Read(buf) n, err := file.Read(buf)
out.Write(buf[:n]) out.Write(buf[:n])
// If written bytes count is smaller than lenght of buffer out.Close()
// then we don't create one more empty file
if err == io.EOF || n < len(buf) { if err == io.EOF {
break break
} }
bytesRead += n
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 return nil
} }