Skip to content

Commit 0c597af

Browse files
committed
Have zipper use standard-compliant path separators
Closes #75
1 parent fce2329 commit 0c597af

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/io/zip/StudentFileAwareZipper.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
public 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

Comments
 (0)