Changes to compression

- Writes now directly to file instead to ram
- Some messages are now starting their own thread
This commit is contained in:
netbenix 2021-11-28 13:42:12 +01:00
parent 8ae3eca9f4
commit b84c82c30c

View file

@ -2,7 +2,6 @@ package Compressor
import (
"archive/tar"
"bytes"
"compress/flate"
"compress/gzip"
"fmt"
@ -18,9 +17,6 @@ import (
func CreateBakFile(fileName string, folderPath string, destinationPath string, backupName string) string {
logger := Logging.DetailedLogger("Compression", "CreateBakFile")
var buf bytes.Buffer
compress(folderPath, &buf, backupName)
pathToFile := destinationPath + string(os.PathSeparator) + fileName + ".bak"
@ -28,10 +24,8 @@ func CreateBakFile(fileName string, folderPath string, destinationPath string, b
if err != nil {
logger.Fatal(err)
}
compress(fileToWrite, folderPath, backupName)
if _, err := io.Copy(fileToWrite, &buf); err != nil {
logger.Fatal(err)
}
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
@ -39,14 +33,13 @@ func CreateBakFile(fileName string, folderPath string, destinationPath string, b
return fileName
}
func compress(folderPath string, buf io.Writer, backupName string){
func compress(fileToWrite *os.File, folderPath string, backupName string){
logger := Logging.DetailedLogger("Gzip", "compress")
zr, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
zr, _ := gzip.NewWriterLevel(fileToWrite, flate.BestCompression)
tw := tar.NewWriter(zr)
fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath))
go fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath))
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Start compression", time.Now())
filepath.Walk(folderPath, func(file string, fi os.FileInfo, err error) error {
header, err := tar.FileInfoHeader(fi, file)
@ -67,11 +60,12 @@ func compress(folderPath string, buf io.Writer, backupName string){
logger.Fatal(err)
}
fmt.Printf("[%s] Compressing: %s (%d bytes)\n", filepath.Base(folderPath) ,relPath, fi.Size())
go fmt.Printf("[%s] Compressing: %s (%d bytes)\n", filepath.Base(folderPath) ,relPath, fi.Size())
if _, err := io.Copy(tw, data); err != nil {
logger.Fatal(err)
}
}
return nil
})
@ -84,6 +78,6 @@ func compress(folderPath string, buf io.Writer, backupName string){
}
fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath))
go fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath))
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now())
}