diff options
-rw-r--r-- | bundles/org.eclipse.equinox.p2.core/src/org/eclipse/equinox/internal/p2/core/helpers/FileUtils.java | 76 |
1 files changed, 70 insertions, 6 deletions
diff --git a/bundles/org.eclipse.equinox.p2.core/src/org/eclipse/equinox/internal/p2/core/helpers/FileUtils.java b/bundles/org.eclipse.equinox.p2.core/src/org/eclipse/equinox/internal/p2/core/helpers/FileUtils.java index ff3b5ef18..5e48244e1 100644 --- a/bundles/org.eclipse.equinox.p2.core/src/org/eclipse/equinox/internal/p2/core/helpers/FileUtils.java +++ b/bundles/org.eclipse.equinox.p2.core/src/org/eclipse/equinox/internal/p2/core/helpers/FileUtils.java @@ -196,6 +196,23 @@ public class FileUtils { } } + /** + * Creates a zip archive at the given destination that contains all of the given inclusions + * except for the given exclusions. Inclusions and exclusions can be phrased as files or folders. + * Including a folder implies that all files and folders under the folder + * should be considered for inclusion. Excluding a folder implies that all files and folders + * under that folder will be excluded. Inclusions with paths deeper than an exclusion folder + * are filtered out and do not end up in the resultant archive. + * <p> + * All entries in the archive are computed using the given path computer. the path computer + * is reset between every explicit entry in the inclusions list. + * </p> + * @param inclusions the set of files and folders to be considered for inclusion in the result + * @param exclusions the set of files and folders to be excluded from the result. May be <code>null</code>. + * @param destinationArchive the location of the resultant archive + * @param pathComputer the path computer used to create the path of the files in the result + * @throws IOException if there is an IO issue during this operation. + */ public static void zip(File[] inclusions, File[] exclusions, File destinationArchive, IPathComputer pathComputer) throws IOException { ZipOutputStream output = new ZipOutputStream(new FileOutputStream(destinationArchive)); HashSet exclusionSet = exclusions == null ? new HashSet() : new HashSet(Arrays.asList(exclusions)); @@ -224,12 +241,6 @@ public class FileUtils { zipFile(output, source, pathComputer); } - public static interface IPathComputer { - public IPath computePath(File source); - - public void reset(); - } - /* * Zip the contents of the given directory into the zip file represented by * the given zip stream. Prepend the given prefix to the file paths. @@ -282,6 +293,34 @@ public class FileUtils { } } + /** + * Path computers are used to transform a given File path into a path suitable for use + * as the to identify that file in an archive file or copy. + */ + public static interface IPathComputer { + /** + * Returns the path representing the given file. Often this trims or otherwise + * transforms the segments of the source file path. + * @param source the file path to be transformed + * @return the transformed path + */ + public IPath computePath(File source); + + /** + * Resets this path computer. Path computers can accumulate state or other information + * for use in computing subsequent paths. Resetting a computer causes it to flush that + * state and start afresh. The exact semantics of resetting depends on the nature of the + * computer itself. + */ + public void reset(); + } + + /** + * Creates a path computer that trims all paths according to the given root path. + * Paths that have no matching segments are returned unchanged. + * @param root the base path to use for trimming + * @return a path computer that trims according to the given root + */ public static IPathComputer createRootPathComputer(final File root) { return new IPathComputer() { public IPath computePath(File source) { @@ -296,6 +335,26 @@ public class FileUtils { }; } + /** + * Creates a path computer that is a cross between the root and parent computers. + * When this computer is reset, the first path seen is considered a new root. That path + * is trimmed by the given number of segments and then used as in the same way as the + * root path computer. Every time this computer is reset, a new root is computed. + * <p> + * This is useful when handling several sets of disjoint files but for each set you want + * to have a common root. Rather than having to compute the roots ahead of time and + * then manage their relationships, you can simply reset the computer between groups. + * </p><p> + * For example, say you have the a list of folders { /a/b/c/eclipse/plugins/, /x/y/eclipse/features/} + * and want to end up with a zip containing plugins and features folders. Using a dynamic + * path computer and keeping 1 segment allows this to be done simply by resetting the computer + * between elements of the top level list. + * </p> + * @param segmentsToKeep the number of segments of encountered paths to keep + * relative to the dynamically computed roots. + * @return a path computer that trims but keeps the given number of segments relative + * to the dynamically computed roots. + */ public static IPathComputer createDynamicPathComputer(final int segmentsToKeep) { return new IPathComputer() { IPathComputer computer = null; @@ -315,6 +374,11 @@ public class FileUtils { }; } + /** + * Creates a path computer that retains the given number of trailing segments. + * @param segmentsToKeep number of segments to keep + * @return a path computer that retains the given number of trailing segments. + */ public static IPathComputer createParentPrefixComputer(final int segmentsToKeep) { return new IPathComputer() { public IPath computePath(File source) { |