update unpackArchive to use both flags

also remove comments that add no value
This commit is contained in:
Evan Su 2025-01-27 00:32:03 -05:00
parent 10e8a1af82
commit bf73698c52

View file

@ -2141,6 +2141,8 @@ func work() {
giu.Update() giu.Update()
return return
} }
os.Remove(outputFile)
} }
// All done, reset the UI // All done, reset the UI
@ -2364,33 +2366,28 @@ func sizeify(size int64) string {
} }
func unpackArchive(zipPath string) error { func unpackArchive(zipPath string) error {
// Open the .zip
reader, err := zip.OpenReader(zipPath) reader, err := zip.OpenReader(zipPath)
if err != nil { if err != nil {
return err return err
} }
defer reader.Close() defer reader.Close()
// Calculate total unpack size by summing each entrys UncompressedSize64
var totalSize int64 var totalSize int64
for _, f := range reader.File { for _, f := range reader.File {
totalSize += int64(f.UncompressedSize64) totalSize += int64(f.UncompressedSize64)
} }
// Directory containing the .zip var extractDir string
extractDir := filepath.Dir(zipPath) if sameLevel {
extractDir = filepath.Dir(zipPath)
} else {
extractDir = filepath.Join(filepath.Dir(zipPath), strings.TrimSuffix(filepath.Base(zipPath), ".zip"))
}
// Setup progress tracking
var done int64 var done int64
startTime := time.Now() startTime := time.Now()
// Iterate over each file in the archive
for i, f := range reader.File { for i, f := range reader.File {
// If user clicked "Cancel" elsewhere, stop
if !working {
return errors.New("operation canceled by user")
}
outPath := filepath.Join(extractDir, f.Name) outPath := filepath.Join(extractDir, f.Name)
// Make directory if current entry is a folder // Make directory if current entry is a folder
@ -2413,20 +2410,14 @@ func unpackArchive(zipPath string) error {
} }
defer fileInArchive.Close() defer fileInArchive.Close()
// Create/overwrite the destination file dstFile, err := os.Create(outPath)
dstFile, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil { if err != nil {
return err return err
} }
// Read from zip in chunks to update progress // Read from zip in chunks to update progress
buffer := make([]byte, MiB) // e.g., 1 MiB chunk buffer := make([]byte, MiB)
for { for {
if !working {
dstFile.Close()
os.Remove(outPath) // remove partial extraction if canceled
return errors.New("operation canceled by user")
}
n, readErr := fileInArchive.Read(buffer) n, readErr := fileInArchive.Read(buffer)
if n > 0 { if n > 0 {
_, writeErr := dstFile.Write(buffer[:n]) _, writeErr := dstFile.Write(buffer[:n])