19
19
public final class StudentFileAwareZipper implements Zipper {
20
20
21
21
private static final Logger log = LoggerFactory .getLogger (StudentFileAwareZipper .class );
22
+ // The zip standard mandates the forward slash "/" to be used as path separator
23
+ private static final char ZIP_SEPARATOR = '/' ;
24
+
22
25
private StudentFilePolicy filePolicy ;
23
26
24
27
public StudentFileAwareZipper () {}
@@ -109,13 +112,8 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path
109
112
110
113
log .trace ("Writing {} to zip" , currentPath );
111
114
112
- String name = projectPath .getParent ().relativize (currentPath ).toString ();
113
-
114
- if (Files .isDirectory (currentPath )) {
115
- log .trace ("{} is a directory" , currentPath );
116
- // Must be "/", can not be replaces with File.separator
117
- name += "/" ;
118
- }
115
+ Path relativePath = projectPath .getParent ().relativize (currentPath );
116
+ String name = relativePathToZipCompliantName (relativePath , Files .isDirectory (currentPath ));
119
117
120
118
ZipArchiveEntry entry = new ZipArchiveEntry (name );
121
119
zipStream .putArchiveEntry (entry );
@@ -129,4 +127,25 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path
129
127
log .trace ("Closing entry" );
130
128
zipStream .closeArchiveEntry ();
131
129
}
130
+
131
+ private static String relativePathToZipCompliantName (Path path , boolean isDirectory ) {
132
+ log .trace ("Generating zip-compliant filename from Path \" {}\" , isDirectory: {}" ,
133
+ path , isDirectory );
134
+
135
+ StringBuilder sb = new StringBuilder ();
136
+ for (Path part : path ) {
137
+ sb .append (part );
138
+ sb .append (ZIP_SEPARATOR );
139
+ }
140
+
141
+ if (!isDirectory ) {
142
+ // ZipArchiveEntry assumes the entry represents a directory if and only
143
+ // if the name ends with a forward slash "/". Remove the trailing slash
144
+ // because this wasn't a directory.
145
+ log .trace ("Path wasn't a directory, removing trailing slash" );
146
+ sb .deleteCharAt (sb .length () - 1 );
147
+ }
148
+
149
+ return sb .toString ();
150
+ }
132
151
}
0 commit comments