-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
146 lines (119 loc) · 3.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"file-compressor/compressor"
"file-compressor/constants"
"file-compressor/encryption"
"file-compressor/utils"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
func handleDecompress(fileName, outputDir, password string) {
// Create a progress callback function
progressCallback := func(progress float64, message string) {
// Convert progress to percentage
percentage := int(progress * 100)
// Clear the current line and print new progress
utils.ColorPrint(utils.YELLOW, fmt.Sprintf("\r\033[KProgress: %d%% - %s", percentage, message))
}
encryptedFile, err := os.Open(fileName)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FILE_OPEN_ERROR, err.Error()))
os.Exit(-1)
}
defer encryptedFile.Close()
decryptedFilePath := fileName + ".decrypted"
decryptedFile, err := os.Create(decryptedFilePath)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FILE_CREATE_ERROR, err.Error())+"\n")
os.Exit(-1)
}
err = encryption.DecryptStream(encryptedFile, decryptedFile, password)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FAILED_TO_DECRYPT, err.Error())+"\n")
//release file
decryptedFile.Close()
// delete the decrypted file
utils.SafeDeleteFile(decryptedFilePath)
os.Exit(-1)
}
decryptedFile.Close()
paths, err := compressor.Decompress(decryptedFilePath, outputDir, progressCallback)
if err != nil {
utils.ColorPrint(utils.RED, err.Error()+"\n")
// delete the decrypted file
//utils.SafeDeleteFile(decryptedFilePath)
os.Exit(-1)
}
// Print newline after progress bar
utils.ColorPrint(utils.YELLOW, "\n\n")
// delete the decrypted file
utils.SafeDeleteFile(decryptedFilePath)
for _, path := range paths {
utils.ColorPrint(utils.GREEN, "Output file: "+path+"\n")
}
}
func handleCompress(fileNames []string, outputDir, password, algorithm string) {
// Create a progress callback function
progressCallback := func(progress float64, message string) {
// Convert progress to percentage
percentage := int(progress * 100)
// Clear the current line and print new progress
utils.ColorPrint(utils.YELLOW, fmt.Sprintf("\r\033[KProgress: %d%% - %s", percentage, message))
}
outputPath, fileMeta, err := compressor.Compress(fileNames, outputDir, algorithm, progressCallback)
if err != nil {
utils.ColorPrint(utils.RED, err.Error()+"\n")
utils.SafeDeleteFile(outputPath)
os.Exit(-1)
}
// Print newline after progress bar
utils.ColorPrint(utils.YELLOW, "\n\n")
fileMeta.PrintFileInfo()
fileMeta.PrintCompressionRatio()
compressedFile, err := os.Open(outputPath)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FILE_OPEN_ERROR, err.Error())+"\n")
os.Exit(-1)
}
fileName := outputPath
fileExt := filepath.Ext(fileName)
fileName = strings.TrimSuffix(fileName, fileExt)
finalFileName := utils.InvalidateFileName(fileName+".sq", "")
finalFile, err := os.Create(finalFileName)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FILE_CREATE_ERROR, err.Error())+"\n")
//release file
compressedFile.Close()
utils.SafeDeleteFile(outputPath)
os.Exit(-1)
}
err = encryption.EncryptStream(compressedFile, finalFile, password)
if err != nil {
utils.ColorPrint(utils.RED, fmt.Sprintf(constants.FAILED_TO_ENCRYPT, err.Error())+"\n")
//release file
compressedFile.Close()
finalFile.Close()
utils.SafeDeleteFile(outputPath)
utils.SafeDeleteFile(finalFileName)
os.Exit(-1)
}
utils.ColorPrint(utils.GREEN, "Output file: "+finalFileName+"\n")
compressedFile.Close()
// delete the compressed file
utils.SafeDeleteFile(outputPath)
}
func main() {
startTime := time.Now()
//cli arguments
filenameStrs, outputDir, password, mode, algorithm := utils.ParseCLI()
if mode == utils.DECOMPRESS {
handleDecompress(filenameStrs[0], outputDir, password)
} else {
handleCompress(filenameStrs, outputDir, password, algorithm)
}
endTime := time.Now()
utils.ColorPrint(utils.GREEN, "Time taken: "+utils.TimeTrack(startTime, endTime)+"\n")
}