@@ -153,6 +153,10 @@ func (x *Xtractr) decompressFolders(resp *Response) error {
153
153
}
154
154
155
155
func (x * Xtractr ) finishExtract (resp * Response , err error ) {
156
+ if resp .X .TempFolder {
157
+ x .cleanTempFolder (resp )
158
+ }
159
+
156
160
resp .Error = err
157
161
resp .Elapsed = time .Since (resp .Started )
158
162
resp .Done = true
@@ -265,40 +269,73 @@ func (x *Xtractr) processArchive(filename, tmpPath string, passwords ...string)
265
269
266
270
func (x * Xtractr ) cleanupProcessedArchives (resp * Response ) error {
267
271
if resp .X .LogFile {
268
- tmpFile := filepath .Join (resp .Output , x .config .Suffix + "." + filepath .Base (resp .X .SearchPath )+ ".txt" )
269
- resp .NewFiles = append (resp .NewFiles , tmpFile )
270
-
271
- msg := []byte (fmt .Sprintf ("# %s - this file may be removed with the extracted data\n ---\n " +
272
- "archives:%s\n extras:%v\n from_path:%s\n temp_path:%s\n relocated:%v\n time:%v\n files:\n - %v\n " ,
273
- x .config .Suffix , resp .Archives , resp .Extras , resp .X .SearchPath , resp .Output , ! resp .X .TempFolder , time .Now (),
274
- strings .Join (resp .NewFiles , "\n - " )))
275
-
276
- if err := ioutil .WriteFile (tmpFile , msg , x .config .FileMode ); err != nil {
277
- x .config .Printf ("Error: Creating Temporary Tracking File: %v" , err ) // continue anyway.
278
- }
272
+ x .createLogFile (resp )
279
273
}
280
274
281
275
if resp .X .DeleteOrig {
282
- for _ , archives := range resp .Archives {
283
- x .DeleteFiles (archives ... ) // as requested
284
- }
285
-
286
- for _ , archives := range resp .Extras {
287
- if len (archives ) != 0 {
288
- x .DeleteFiles (archives ... ) // these got extracted too
289
- }
290
- }
276
+ // as requested
277
+ x .deleteOriginals (resp )
291
278
}
292
279
293
280
var err error
294
281
295
- // If TempFolder is false then move the files back to the original location.
296
282
if ! resp .X .TempFolder {
283
+ // If TempFolder is false then move the files back to the original location.
297
284
resp .NewFiles , err = x .MoveFiles (resp .Output , resp .X .SearchPath , false )
298
- } else if len (x .GetFileList (resp .X .SearchPath )) == 0 {
285
+ }
286
+
287
+ if len (x .GetFileList (resp .X .SearchPath )) == 0 {
299
288
// If the original path is empty, delete it.
300
289
x .DeleteFiles (resp .X .SearchPath )
301
290
}
302
291
303
292
return err
304
293
}
294
+
295
+ func (x * Xtractr ) createLogFile (resp * Response ) {
296
+ tmpFile := filepath .Join (resp .Output , x .config .Suffix + "." + filepath .Base (resp .X .SearchPath )+ ".txt" )
297
+ resp .NewFiles = append (resp .NewFiles , tmpFile )
298
+
299
+ msg := []byte (fmt .Sprintf ("# %s - this file may be removed with the extracted data\n ---\n " +
300
+ "archives:%s\n extras:%v\n from_path:%s\n temp_path:%s\n relocated:%v\n time:%v\n files:\n - %v\n " ,
301
+ x .config .Suffix , resp .Archives , resp .Extras , resp .X .SearchPath , resp .Output , ! resp .X .TempFolder , time .Now (),
302
+ strings .Join (resp .NewFiles , "\n - " )))
303
+
304
+ if err := ioutil .WriteFile (tmpFile , msg , x .config .FileMode ); err != nil {
305
+ x .config .Printf ("Error: Creating Temporary Tracking File: %v" , err )
306
+ }
307
+ }
308
+
309
+ func (x * Xtractr ) deleteOriginals (resp * Response ) {
310
+ for _ , archives := range resp .Archives {
311
+ x .DeleteFiles (archives ... )
312
+ }
313
+ // these got extracted too
314
+ for _ , archives := range resp .Extras {
315
+ if len (archives ) != 0 {
316
+ x .DeleteFiles (archives ... )
317
+ }
318
+ }
319
+ }
320
+
321
+ func (x * Xtractr ) cleanTempFolder (resp * Response ) {
322
+ noSuffix := strings .TrimSuffix (strings .TrimRight (resp .Output , `/\` ), x .config .Suffix )
323
+ if _ , err := os .Stat (noSuffix ); err == nil {
324
+ return // it exists already?!
325
+ } else if _ , err := os .Stat (resp .Output ); err != nil {
326
+ return
327
+ }
328
+
329
+ if newFiles , err := x .MoveFiles (resp .Output , noSuffix , false ); err != nil {
330
+ x .config .Printf ("Error: Renaming Temporary Folder: %v" , err )
331
+ } else {
332
+ x .config .Debugf ("Renamed Temp Folder: %v -> %v" , resp .Output , noSuffix )
333
+ resp .Output = noSuffix
334
+ resp .NewFiles = newFiles
335
+ }
336
+
337
+ if len (x .GetFileList (resp .X .SearchPath )) == 0 {
338
+ // If the original path is empty, delete it.
339
+ x .DeleteFiles (resp .X .SearchPath )
340
+ }
341
+ }
0 commit comments