Add option to compress a single file

This commit is contained in:
Evan Su 2022-05-19 23:56:45 -04:00 committed by GitHub
parent ba275ff342
commit 9aa62adf51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -471,10 +471,16 @@ func draw() {
giu.Checkbox("Paranoid mode", &paranoid), giu.Checkbox("Paranoid mode", &paranoid),
giu.Tooltip("Provides the highest level of security attainable."), giu.Tooltip("Provides the highest level of security attainable."),
giu.Dummy(-170, 0), giu.Dummy(-170, 0),
giu.Style().SetDisabled(!(len(allFiles) > 1 || len(onlyFolders) > 0)).To( giu.Checkbox("Compress files", &compress).OnChange(func() {
giu.Checkbox("Compress files", &compress), if !(len(allFiles) > 1 || len(onlyFolders) > 0) {
giu.Tooltip("Compress files with Deflate before encrypting."), if compress {
), outputFile = filepath.Join(filepath.Dir(outputFile), "Encrypted") + ".zip.pcv"
} else {
outputFile = filepath.Join(filepath.Dir(outputFile), filepath.Base(inputFile)) + ".pcv"
}
}
}),
giu.Tooltip("Compress files with Deflate before encrypting."),
).Build() ).Build()
giu.Row( giu.Row(
@ -538,7 +544,7 @@ func draw() {
// Prefill the filename // Prefill the filename
tmp := strings.TrimSuffix(filepath.Base(outputFile), ".pcv") tmp := strings.TrimSuffix(filepath.Base(outputFile), ".pcv")
f.SetInitFilename(strings.TrimSuffix(tmp, filepath.Ext(tmp))) f.SetInitFilename(strings.TrimSuffix(tmp, filepath.Ext(tmp)))
if mode == "encrypt" && (len(allFiles) > 1 || len(onlyFolders) > 0) { if mode == "encrypt" && (len(allFiles) > 1 || len(onlyFolders) > 0 || compress) {
f.SetInitFilename("Encrypted") f.SetInitFilename("Encrypted")
} }
@ -550,7 +556,7 @@ func draw() {
// Add the correct extensions // Add the correct extensions
if mode == "encrypt" { if mode == "encrypt" {
if len(allFiles) > 1 || len(onlyFolders) > 0 { if len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
file += ".zip.pcv" file += ".zip.pcv"
} else { } else {
file += filepath.Ext(inputFile) + ".pcv" file += filepath.Ext(inputFile) + ".pcv"
@ -881,7 +887,13 @@ func work() {
var authTag []byte // 64-byte authentication tag (BLAKE2b or HMAC-SHA3) var authTag []byte // 64-byte authentication tag (BLAKE2b or HMAC-SHA3)
// Combine/compress all files into a .zip file if needed // Combine/compress all files into a .zip file if needed
if len(allFiles) > 1 || len(onlyFolders) > 0 { if len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
// Consider case where compressing only one file
files := allFiles
if len(allFiles) == 0 {
files = onlyFiles
}
// Get the root directory of the selected files // Get the root directory of the selected files
var rootDir string var rootDir string
if len(onlyFolders) > 0 { if len(onlyFolders) > 0 {
@ -900,7 +912,7 @@ func work() {
// Calculate total size of uncompressed files // Calculate total size of uncompressed files
compressTotal = 0 compressTotal = 0
for _, path := range allFiles { for _, path := range files {
stat, _ := os.Stat(path) stat, _ := os.Stat(path)
compressTotal += stat.Size() compressTotal += stat.Size()
} }
@ -910,8 +922,9 @@ func work() {
compressStart = time.Now() compressStart = time.Now()
// Add each file to the .zip // Add each file to the .zip
for i, path := range allFiles {
progressInfo = fmt.Sprintf("%d/%d", i+1, len(allFiles)) for i, path := range files {
progressInfo = fmt.Sprintf("%d/%d", i+1, len(files))
giu.Update() giu.Update()
// Create file info header (size, last modified, etc.) // Create file info header (size, last modified, etc.)
@ -1350,7 +1363,7 @@ func work() {
cancel() cancel()
fin.Close() fin.Close()
fout.Close() fout.Close()
if recombine || len(allFiles) > 1 || len(onlyFolders) > 0 { if recombine || len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
os.Remove(inputFile) os.Remove(inputFile)
} }
os.Remove(outputFile) os.Remove(outputFile)
@ -1476,12 +1489,13 @@ func work() {
serpent.XORKeyStream(dst, src) serpent.XORKeyStream(dst, src)
} }
} }
_, err = fout.Write(dst) _, err = fout.Write(dst)
if err != nil { if err != nil {
insufficientSpace() insufficientSpace()
fin.Close() fin.Close()
fout.Close() fout.Close()
if recombine || len(allFiles) > 1 || len(onlyFolders) > 0 { if recombine || len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
os.Remove(inputFile) os.Remove(inputFile)
} }
os.Remove(outputFile) os.Remove(outputFile)
@ -1610,7 +1624,7 @@ func work() {
cancel() cancel()
fin.Close() fin.Close()
fout.Close() fout.Close()
if len(allFiles) > 1 || len(onlyFolders) > 0 { if len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
os.Remove(inputFile) os.Remove(inputFile)
} }
os.Remove(outputFile) os.Remove(outputFile)
@ -1630,7 +1644,7 @@ func work() {
insufficientSpace() insufficientSpace()
fin.Close() fin.Close()
fout.Close() fout.Close()
if len(allFiles) > 1 || len(onlyFolders) > 0 { if len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
os.Remove(inputFile) os.Remove(inputFile)
} }
os.Remove(outputFile) os.Remove(outputFile)
@ -1681,7 +1695,7 @@ func work() {
} }
// Delete the temporary .zip used to encrypt files // Delete the temporary .zip used to encrypt files
if len(allFiles) > 1 || len(onlyFolders) > 0 { if len(allFiles) > 1 || len(onlyFolders) > 0 || compress {
os.Remove(inputFile) os.Remove(inputFile)
} }