@@ -7,6 +7,7 @@ package acquisition
7
7
8
8
import (
9
9
"archive/zip"
10
+ "bytes"
10
11
"fmt"
11
12
"io"
12
13
"os"
@@ -65,6 +66,7 @@ func NewEncryptedZipWriter(uuid string) (*EncryptedZipWriter, error) {
65
66
encWriter , err := age .Encrypt (file , recipient )
66
67
if err != nil {
67
68
file .Close ()
69
+ os .Remove (outputPath ) // Clean up the created file
68
70
return nil , fmt .Errorf ("failed to create encrypted writer: %v" , err )
69
71
}
70
72
@@ -84,8 +86,12 @@ func NewEncryptedZipWriter(uuid string) (*EncryptedZipWriter, error) {
84
86
85
87
// CreateFile creates a new file in the encrypted zip and returns a writer
86
88
func (ezw * EncryptedZipWriter ) CreateFile (name string ) (io.Writer , error ) {
87
- if ezw .closed {
88
- return nil , fmt .Errorf ("encrypted zip writer is closed" )
89
+ if err := ezw .checkClosed (); err != nil {
90
+ return nil , err
91
+ }
92
+
93
+ if name == "" {
94
+ return nil , fmt .Errorf ("file name cannot be empty" )
89
95
}
90
96
91
97
header := & zip.FileHeader {
@@ -99,8 +105,8 @@ func (ezw *EncryptedZipWriter) CreateFile(name string) (io.Writer, error) {
99
105
100
106
// CreateFileFromReader copies data from a reader to a file in the encrypted zip
101
107
func (ezw * EncryptedZipWriter ) CreateFileFromReader (name string , src io.Reader ) error {
102
- if ezw . closed {
103
- return fmt .Errorf ("encrypted zip writer is closed " )
108
+ if src == nil {
109
+ return fmt .Errorf ("source reader cannot be nil " )
104
110
}
105
111
106
112
writer , err := ezw .CreateFile (name )
@@ -118,112 +124,57 @@ func (ezw *EncryptedZipWriter) CreateFileFromReader(name string, src io.Reader)
118
124
119
125
// CreateFileFromString creates a file with string content in the encrypted zip
120
126
func (ezw * EncryptedZipWriter ) CreateFileFromString (name , content string ) error {
121
- if ezw .closed {
122
- return fmt .Errorf ("encrypted zip writer is closed" )
123
- }
124
-
125
- writer , err := ezw .CreateFile (name )
126
- if err != nil {
127
- return fmt .Errorf ("failed to create file in zip: %v" , err )
128
- }
129
-
130
- _ , err = writer .Write ([]byte (content ))
131
- if err != nil {
132
- return fmt .Errorf ("failed to write content to zip file: %v" , err )
133
- }
134
-
135
- return nil
127
+ return ezw .CreateFileFromReader (name , strings .NewReader (content ))
136
128
}
137
129
138
130
// CreateFileFromBytes creates a file with byte content in the encrypted zip
139
131
func (ezw * EncryptedZipWriter ) CreateFileFromBytes (name string , content []byte ) error {
140
- if ezw .closed {
141
- return fmt .Errorf ("encrypted zip writer is closed" )
142
- }
143
-
144
- writer , err := ezw .CreateFile (name )
145
- if err != nil {
146
- return fmt .Errorf ("failed to create file in zip: %v" , err )
147
- }
148
-
149
- _ , err = writer .Write (content )
150
- if err != nil {
151
- return fmt .Errorf ("failed to write content to zip file: %v" , err )
152
- }
153
-
154
- return nil
132
+ return ezw .CreateFileFromReader (name , bytes .NewReader (content ))
155
133
}
156
134
157
135
// CreateFileFromPath reads a file from disk and adds it to the encrypted zip
158
136
func (ezw * EncryptedZipWriter ) CreateFileFromPath (name , filePath string ) error {
159
- if ezw .closed {
160
- return fmt .Errorf ("encrypted zip writer is closed" )
161
- }
162
-
163
137
file , err := os .Open (filePath )
164
138
if err != nil {
165
- return fmt .Errorf ("failed to open source file: %v" , err )
139
+ return fmt .Errorf ("failed to open source file %q : %v" , filePath , err )
166
140
}
167
141
defer file .Close ()
168
142
169
143
return ezw .CreateFileFromReader (name , file )
170
144
}
171
145
172
- // AddDirectory recursively adds all files from a directory to the encrypted zip
173
- func (ezw * EncryptedZipWriter ) AddDirectory (dirPath , basePath string ) error {
174
- if ezw .closed {
175
- return fmt .Errorf ("encrypted zip writer is closed" )
176
- }
177
-
178
- return filepath .Walk (dirPath , func (path string , info os.FileInfo , err error ) error {
179
- if err != nil {
180
- return err
181
- }
182
-
183
- // Skip directories
184
- if info .IsDir () {
185
- return nil
186
- }
187
-
188
- // Calculate relative path for zip entry
189
- relPath , err := filepath .Rel (basePath , path )
190
- if err != nil {
191
- return fmt .Errorf ("failed to get relative path: %v" , err )
192
- }
193
-
194
- // Use forward slashes for zip paths
195
- zipPath := filepath .ToSlash (relPath )
196
-
197
- // Add file to zip
198
- return ezw .CreateFileFromPath (zipPath , path )
199
- })
200
- }
201
-
202
146
// Close finalizes and closes the encrypted zip
203
147
func (ezw * EncryptedZipWriter ) Close () error {
204
148
if ezw .closed {
205
149
return nil
206
150
}
207
151
208
152
ezw .closed = true
153
+ var lastErr error
209
154
210
155
// Close zip writer first
211
156
if err := ezw .zipWriter .Close (); err != nil {
212
- return fmt .Errorf ("failed to close zip writer: %v" , err )
157
+ lastErr = fmt .Errorf ("failed to close zip writer: %v" , err )
213
158
}
214
159
215
160
// Close encryption writer
216
161
if err := ezw .encWriter .Close (); err != nil {
217
- return fmt .Errorf ("failed to close encryption writer: %v" , err )
162
+ if lastErr == nil {
163
+ lastErr = fmt .Errorf ("failed to close encryption writer: %v" , err )
164
+ }
218
165
}
219
166
220
167
// Close file
221
168
if err := ezw .file .Close (); err != nil {
222
- return fmt .Errorf ("failed to close output file: %v" , err )
169
+ if lastErr == nil {
170
+ lastErr = fmt .Errorf ("failed to close output file: %v" , err )
171
+ }
223
172
}
224
173
225
- log .Infof ("Encrypted archive created successfully at %s" , ezw .outputPath )
226
- return nil
174
+ if lastErr == nil {
175
+ log .Infof ("Encrypted archive created successfully at %s" , ezw .outputPath )
176
+ }
177
+ return lastErr
227
178
}
228
179
229
180
// GetOutputPath returns the path to the encrypted zip file
@@ -235,3 +186,11 @@ func (ezw *EncryptedZipWriter) GetOutputPath() string {
235
186
func (ezw * EncryptedZipWriter ) IsClosed () bool {
236
187
return ezw .closed
237
188
}
189
+
190
+ // checkClosed is a helper method to check if the writer is closed
191
+ func (ezw * EncryptedZipWriter ) checkClosed () error {
192
+ if ezw .closed {
193
+ return fmt .Errorf ("encrypted zip writer is closed" )
194
+ }
195
+ return nil
196
+ }
0 commit comments