1919public final class StudentFileAwareZipper implements Zipper {
2020
2121 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+
2225 private StudentFilePolicy filePolicy ;
2326
2427 public StudentFileAwareZipper () {}
@@ -109,13 +112,8 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path
109112
110113 log .trace ("Writing {} to zip" , currentPath );
111114
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 ));
119117
120118 ZipArchiveEntry entry = new ZipArchiveEntry (name );
121119 zipStream .putArchiveEntry (entry );
@@ -129,4 +127,25 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path
129127 log .trace ("Closing entry" );
130128 zipStream .closeArchiveEntry ();
131129 }
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+ }
132151}
0 commit comments