public static void zipSource()

in src/main/java/ZipSourceCallable.java [86:134]


    public static void zipSource(FilePath workspace, final String directory, final ZipOutputStream out, final String prefixToTrim) throws InvalidInputException, IOException, InterruptedException {
        if (!Paths.get(directory).startsWith(Paths.get(prefixToTrim))) {
            throw new InvalidInputException(zipSourceError + "prefixToTrim: " + prefixToTrim + ", directory: "+ directory);
        }

        FilePath dir = new FilePath(workspace, directory);
        List<FilePath> dirFiles = dir.list();
        if (dirFiles == null) {
            throw new InvalidInputException("Empty or invalid source directory: " + directory);
        }
        byte[] buffer = new byte[1024];
        int bytesRead;

        if (dirFiles.isEmpty()) {
            String path = trimPrefix(dir.getRemote(), prefixToTrim);
            if (!path.isEmpty()) {
                out.putNextEntry(new ZipEntry(path + "/"));
            }
        } else {
            for (int i = 0; i < dirFiles.size(); i++) {
                FilePath f = new FilePath(workspace, dirFiles.get(i).getRemote());
                if (f.isDirectory()) {
                    zipSource(workspace, f.getRemote() + File.separator, out, prefixToTrim);
                } else {
                    InputStream inputStream = f.read();
                    try {
                        String path = trimPrefix(f.getRemote(), prefixToTrim);

                        if(path.startsWith(File.separator)) {
                            path = path.substring(1, path.length());
                        }

                        // Zip files created on the windows file system will not unzip
                        // properly on unix systems. Without this change, no directory structure
                        // is built when unzipping.
                        path = path.replace(File.separator, "/");

                        ZipEntry entry = new ZipEntry(path);
                        out.putNextEntry(entry);
                        while ((bytesRead = inputStream.read(buffer)) != -1) {
                            out.write(buffer, 0, bytesRead);
                        }
                    } finally {
                        inputStream.close();
                    }
                }
            }
        }
    }