@@ -3,7 +3,6 @@ package compressor
33import (
44 "fmt"
55 "os"
6- "path"
76 "path/filepath"
87 "strings"
98 "sync"
@@ -17,7 +16,7 @@ import (
1716func Compress (filenameStrs []string , outputDir string , password string ) error {
1817 // Set default output directory if not provided
1918 if outputDir == "" {
20- outputDir = path .Dir (filenameStrs [0 ])
19+ outputDir = filepath .Dir (filenameStrs [0 ])
2120 }
2221
2322 // Prepare to store files' content
@@ -26,9 +25,14 @@ func Compress(filenameStrs []string, outputDir string, password string) error {
2625 var compressedSize int64
2726
2827 // Read all files concurrently
29- err := ReadAllFilesConcurrently (filenameStrs , & files , & originalSize )
30- if err != nil {
31- return err
28+ errs := ReadAllFilesConcurrently (filenameStrs , & files , & originalSize )
29+
30+ // Check for errors from goroutines
31+ if len (errs ) > 0 {
32+ for _ , err := range errs {
33+ utils .ColorPrint (utils .RED , fmt .Sprintf ("Error: %v\n " , err ))
34+ }
35+ os .Exit (1 )
3236 }
3337
3438 // Compress files using Huffman coding
@@ -83,7 +87,7 @@ func Compress(filenameStrs []string, outputDir string, password string) error {
8387 return nil
8488}
8589
86- func ReadAllFilesConcurrently (filenameStrs []string , files * []utils.File , originalSize * int64 ) error {
90+ func ReadAllFilesConcurrently (filenameStrs []string , files * []utils.File , originalSize * int64 ) [] error {
8791 // Use a wait group to synchronize goroutines
8892 var wg sync.WaitGroup
8993 var errMutex sync.Mutex // Mutex to handle errors safely
@@ -101,16 +105,19 @@ func ReadAllFilesConcurrently(filenameStrs []string, files *[]utils.File, origin
101105 wg .Wait ()
102106 close (errChan )
103107
108+ errors := make ([]error , 0 )
109+
104110 // Check for errors from goroutines
105111 for err := range errChan {
106112 if err != nil {
113+ //print all errors
107114 errMutex .Lock ()
108- defer errMutex . Unlock ( )
109- return err // Return the first error encountered
115+ errors = append ( errors , err )
116+ errMutex . Unlock ()
110117 }
111118 }
112119
113- return nil
120+ return errors
114121}
115122
116123func readFileFromDisk (filePath string , files * []utils.File , originalSize * int64 , wg * sync.WaitGroup , errChan chan error ) {
@@ -120,19 +127,21 @@ func readFileFromDisk(filePath string, files *[]utils.File, originalSize *int64,
120127 // Check if the file or folder exists
121128 info , err := os .Stat (filePath )
122129 if os .IsNotExist (err ) {
123- errChan <- fmt .Errorf ("file or folder does not exist: %s" , filePath )
130+ errChan <- fmt .Errorf ("file or folder does not exist: '%s'" , filePath )
131+ return
124132 }
125133
126134 utils .ColorPrint (utils .YELLOW , fmt .Sprintf ("Compressing file (%s)\n " , filePath ))
127135 // Read file content
128136 content , err := os .ReadFile (filePath )
129137 if err != nil {
130- errChan <- fmt .Errorf ("failed to read file %s: %w" , filePath , err )
138+ errChan <- fmt .Errorf ("failed to read file '%s': %w" , filePath , err )
139+ return
131140 }
132141
133142 // Store file information (name and content)
134143 * files = append (* files , utils.File {
135- Name : path .Base (filePath ),
144+ Name : filepath .Base (filePath ),
136145 Content : content ,
137146 })
138147
@@ -145,7 +154,7 @@ func Decompress(compressedFilename string, outputDir string, password string) er
145154
146155 // Set default output directory if not provided
147156 if outputDir == "" {
148- outputDir = path .Dir (compressedFilename )
157+ outputDir = filepath .Dir (compressedFilename )
149158 }
150159
151160 compressedContent := make ([]byte , 0 )
@@ -166,7 +175,7 @@ func Decompress(compressedFilename string, outputDir string, password string) er
166175
167176 // Decompress file using Huffman coding
168177 files , err := Unzip (utils.File {
169- Name : path .Base (compressedFilename ),
178+ Name : filepath .Base (compressedFilename ),
170179 Content : compressedContent ,
171180 })
172181
@@ -244,9 +253,9 @@ func writeFileToDisk(file utils.File, outputDir string, wg *sync.WaitGroup, errC
244253}
245254
246255func InvalidateFileName (file * utils.File , outputDir * string ) {
247- fileExt := path .Ext (file .Name )
256+ fileExt := filepath .Ext (file .Name )
248257 //extract the file name without the extension
249- filename := path .Base (file .Name )
258+ filename := filepath .Base (file .Name )
250259 filename = strings .TrimSuffix (filename , fileExt )
251260
252261 count := 1
0 commit comments