From b84c82c30cce78b164ab93dd223ad764861718c7 Mon Sep 17 00:00:00 2001 From: netbenix Date: Sun, 28 Nov 2021 13:42:12 +0100 Subject: [PATCH] Changes to compression - Writes now directly to file instead to ram - Some messages are now starting their own thread --- Compressor/Compression.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Compressor/Compression.go b/Compressor/Compression.go index 063276c..3a959a7 100644 --- a/Compressor/Compression.go +++ b/Compressor/Compression.go @@ -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()) } \ No newline at end of file