1
1
package com .genexus .compression ;
2
2
3
+ import com .genexus .GXBaseCollection ;
4
+ import com .genexus .SdtMessages_Message ;
3
5
import org .apache .logging .log4j .Logger ;
4
6
5
- import java .io .File ;
7
+ import java .io .IOException ;
8
+ import java .nio .file .Files ;
9
+ import java .nio .file .Path ;
10
+ import java .nio .file .Paths ;
6
11
import java .util .ArrayList ;
7
- import java .util .List ;
8
- import java .util .Vector ;
12
+ import java .util .stream .Stream ;
9
13
10
14
public class Compression {
11
15
private static final Logger log = org .apache .logging .log4j .LogManager .getLogger (Compression .class );
12
16
13
17
private String path ;
14
18
private String format ;
15
- private List <File > filesToCompress ;
19
+ private GXBaseCollection <SdtMessages_Message >[] messages ;
20
+ private ArrayList <String > filesToCompress ;
16
21
17
22
public Compression () {}
18
23
19
- public Compression (String path , String format ) {
24
+ public Compression (String path , String format , GXBaseCollection < SdtMessages_Message >[] messages ) {
20
25
this .path = path ;
21
26
this .format = format ;
22
- this .filesToCompress = new ArrayList <>();
27
+ this .messages = messages ;
28
+ filesToCompress = new ArrayList <>();
23
29
}
24
30
25
31
public void setPath (String path ) {
@@ -31,48 +37,26 @@ public void setFormat(String format) {
31
37
}
32
38
33
39
public void addFile (String filePath ) {
34
- File file = new File (filePath );
35
- if (file .exists ()) {
36
- filesToCompress .add (file );
37
- } else {
38
- log .error ("File does not exist: {}" , file .getAbsolutePath ());
39
- }
40
+ filesToCompress .add (filePath );
40
41
}
41
42
42
43
public void addFolder (String folderPath ) {
43
- File folder = new File (folderPath );
44
- if (folder .exists () && folder .isDirectory ()) {
45
- File [] files = folder .listFiles ();
46
- if (files != null ) {
47
- for (File file : files ) {
48
- if (file .isDirectory ()) {
49
- addFolder (file .getAbsolutePath ());
50
- } else {
51
- addFile (file .getAbsolutePath ());
52
- }
53
- }
54
- }
55
- } else {
56
- log .error ("Folder does not exist or is not a directory: {}" , folder .getAbsolutePath ());
44
+ Path path = Paths .get (folderPath );
45
+ try (Stream <Path > stream = Files .walk (path )) {
46
+ stream .filter (Files ::isRegularFile )
47
+ .forEach (p -> addFile (p .toAbsolutePath ().toString ()));
48
+ } catch (IOException e ) {
49
+ log .error ("Failed to process directory: {}" , folderPath , e );
57
50
}
58
51
}
59
52
60
- public int save () {
61
- if (filesToCompress .isEmpty ()) {
62
- log .error ("No files have been added for compression." );
63
- return -4 ;
64
- }
65
- Vector <String > paths = new Vector <>();
66
- for (File file : filesToCompress ) {
67
- paths .add (file .getPath ());
68
- }
69
- return GXCompressor .compressFiles (paths , path , format );
53
+ public Boolean save () {
54
+ return GXCompressor .compressFiles (filesToCompress , path , format , messages );
70
55
}
71
56
72
-
73
57
public void clear () {
74
- this . path = "" ;
75
- this . format = "" ;
76
- this . filesToCompress = new ArrayList <>();
58
+ path = "" ;
59
+ format = "" ;
60
+ filesToCompress = new ArrayList <>();
77
61
}
78
- }
62
+ }
0 commit comments