Github user zentol commented on a diff in the pull request: https://github.com/apache/flink/pull/6147#discussion_r195054416 --- Diff: flink-runtime/src/main/java/org/apache/flink/runtime/filecache/FileCache.java --- @@ -342,4 +320,54 @@ public void run() { } } } + + public static Path compressDirectory(Path directory) throws IOException { + FileSystem fs = directory.getFileSystem(); + java.nio.file.Path tmp = Files.createTempFile("flink-distributed-cache", ".zip"); + try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmp.toFile()))) { + addToZip(directory, fs, directory.getParent(), out); + } + return new Path(tmp.toUri()); + } + + private static void addToZip(Path fileOrDirectory, FileSystem fs, Path rootDir, ZipOutputStream out) throws IOException { + String relativePath = fileOrDirectory.getPath().replace(rootDir.getPath() + '/', ""); + if (fs.getFileStatus(fileOrDirectory).isDir()) { + out.putNextEntry(new ZipEntry(relativePath + '/')); + for (FileStatus containedFile : fs.listStatus(fileOrDirectory)) { + addToZip(containedFile.getPath(), fs, rootDir, out); + } + } else { + ZipEntry entry = new ZipEntry(relativePath); + out.putNextEntry(entry); + + try (FSDataInputStream in = fs.open(fileOrDirectory)) { + IOUtils.copyBytes(in, out, false); + } + out.closeEntry(); + } + } + + @VisibleForTesting + static Path expandDirectory(File file, File targetDirectory, boolean isExecutable) throws IOException { + java.nio.file.Path rootDir = null; + try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) { + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + java.nio.file.Path relativePath = Paths.get(entry.getName()); + rootDir = relativePath.getName(0); + + java.nio.file.Path newFile = targetDirectory.toPath().resolve(relativePath); + if (entry.isDirectory()) { + Files.createDirectories(newFile); + } else { + Files.copy(zis, newFile); + //noinspection ResultOfMethodCallIgnored + newFile.toFile().setExecutable(isExecutable); --- End diff -- yes, this is very much a hack. We need a way to attach meta-data for every file (i.e. an executable flag); maybe a `.metadata` file in the zip that contains an entry for each file.
---