Skip to content

Commit

Permalink
Have zipper use standard-compliant path separators
Browse files Browse the repository at this point in the history
  • Loading branch information
cxcorp committed Jan 3, 2017
1 parent fce2329 commit ee75d66
Showing 1 changed file with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -109,13 +110,8 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path

log.trace("Writing {} to zip", currentPath);

String name = projectPath.getParent().relativize(currentPath).toString();

if (Files.isDirectory(currentPath)) {
log.trace("{} is a directory", currentPath);
// Must be "/", can not be replaces with File.separator
name += "/";
}
Path relativePath = projectPath.getParent().relativize(currentPath);
String name = relativePathToZipCompliantName(relativePath, Files.isDirectory(currentPath));

ZipArchiveEntry entry = new ZipArchiveEntry(name);
zipStream.putArchiveEntry(entry);
Expand All @@ -129,4 +125,31 @@ private void writeToZip(Path currentPath, ZipArchiveOutputStream zipStream, Path
log.trace("Closing entry");
zipStream.closeArchiveEntry();
}

private static String relativePathToZipCompliantName(Path path, boolean isDirectory) {
log.trace("Generating zip-compliant filename from Path \"{}\", isDirectory: {}", path, isDirectory);
// zip standard says "/" is to be used as the path separator.
final char separator = '/';

StringBuilder sb = new StringBuilder();
for (Path part : path) {
sb.append(part);
sb.append(separator);
}

if (sb.length() == 0) {
log.trace("Path didn't have any name elements");
return path.toString();
}

if (!isDirectory) {
// ZipArchiveEntry assumes the entry represents a directory if and only
// if the name ends with a forward slash "/". Remove the last "/" because
// this wasn't a directory.
log.trace("Path wasn't a directory, removing trailing slash");
sb.deleteCharAt(sb.length() - 1);
}

return sb.toString();
}
}

0 comments on commit ee75d66

Please sign in to comment.