/** * computes a file path from a root folder and a zip archive path. * @param rootFolder the root folder * @param path the archive path * @return the File */ private static File computeFile(@NonNull File rootFolder, @NonNull String path) { // remove the lib/ part of the path and convert. path = FileUtils.toSystemDependentPath(path.substring(4)); return new File(rootFolder, path); } }
/** * Computes the relative of a file or directory with respect to a directory. * For example, if the file's absolute path is {@code /a/b/c} and the directory * is {@code /a}, this method returns {@code b/c}. * * @param file the path that may not correspond to any existing path in the filesystem * @param dir the directory to compute the path relative to * @return the relative path from {@code dir} to {@code file}; if {@code file} is a directory * the path comes appended with the file separator (see documentation on {@code relativize} * on java's {@code URI} class) */ @NonNull public static String relativePossiblyNonExistingPath(@NonNull File file, @NonNull File dir) { String path = dir.toURI().relativize(file.toURI()).getPath(); return toSystemDependentPath(path); }
/** * Reads a zip file and adds all files in the file in a new relative set. * * @param zip the zip file to read, must be a valid, existing zip file * @return the file set * @throws IOException failed to read the zip file */ @NonNull public static ImmutableSet<RelativeFile> fromZip(@NonNull File zip) throws IOException { Preconditions.checkArgument(zip.isFile(), "!zip.isFile()"); Set<RelativeFile> files = Sets.newHashSet(); try (ZFile zipReader = new ZFile(zip)) { for (StoredEntry entry : zipReader.entries()) { if (entry.getType() == StoredEntryType.FILE) { File file = new File(zip, FileUtils.toSystemDependentPath( entry.getCentralDirectoryHeader().getName())); files.add(new RelativeFile(zip, file)); } } } return ImmutableSet.copyOf(files); } }
new File( zip, FileUtils.toSystemDependentPath( entry.getCentralDirectoryHeader().getName())); builder.put(new RelativeFile(zip, file), FileStatus.REMOVED); String path = entry.getCentralDirectoryHeader().getName(); if (entry.getType() == StoredEntryType.FILE) { File file = new File(zip, FileUtils.toSystemDependentPath(path)); RelativeFile newRelative = new RelativeFile(zip, file); File file = new File(zip, FileUtils.toSystemDependentPath(path)); RelativeFile oldRelative = new RelativeFile(zip, file);