Package org.eclipse.cbi.common.util
Class Zips
- java.lang.Object
-
- org.eclipse.cbi.common.util.Zips
-
public class Zips extends java.lang.Object
Utility class to work with Zip files (Path
based).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Zips.NoPreserveRootPathMapper
private static class
Zips.PackerFileVisitor
private static interface
Zips.PathMapper
private static class
Zips.PreserveRootPathMapper
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
BACKSLASH_ESCAPE_REPLACEMENT
private static java.util.regex.Pattern
BACKSLASH_PATTERN
private static int
PERM_MASK
private static java.lang.String
ZIP_ENTRY_NAME_SEPARATOR
-
Constructor Summary
Constructors Constructor Description Zips()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.nio.file.Path
checkPathExists(java.nio.file.Path source, java.lang.String msg)
private static org.apache.commons.compress.archivers.zip.ZipArchiveEntry
createArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.lang.String entryName)
private static java.lang.String
entryNameFrom(java.nio.file.Path path, boolean isDirectoryw)
private static void
fixPosixPermissions(org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path entryPath)
private static java.io.BufferedOutputStream
newBufferedOutputStream(java.nio.file.Path path)
private static int
packEntries(java.nio.file.Path source, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, boolean preserveRoot, java.util.Set<java.nio.file.Path> pathToExcludes)
static int
packJar(java.nio.file.Path source, java.nio.file.Path targetJar, boolean preserveRoot)
Zip the givensource
file or folder in thetargetJar
Jar file.private static java.util.Set<java.nio.file.Path>
packManifestIfAny(java.nio.file.Path source, org.apache.commons.compress.archivers.jar.JarArchiveOutputStream jos)
Looks for META-INF/MANIFEST.MF file in the given source folder and add them as entries to the JarOutputStream.static int
packZip(java.nio.file.Path source, java.nio.file.Path targetZip, boolean preserveRoot)
Zip the givensource
file or folder in thetargetZip
Zip file.private static void
putDirectoryEntry(java.nio.file.Path dir, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath)
private static void
putFileEntry(java.nio.file.Path file, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath)
private static void
setPermissions(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, java.nio.file.Path entryPath)
(package private) static int
unpack(org.apache.commons.compress.archivers.tar.TarArchiveInputStream zis, java.nio.file.Path outputDir)
private static int
unpack(org.apache.commons.compress.archivers.zip.ZipFile zipFile, java.nio.file.Path outputDir, java.util.function.BiConsumer<org.apache.commons.compress.archivers.zip.ZipArchiveEntry,java.nio.file.Path> entryFixer)
static int
unpackJar(java.nio.file.Path source, java.nio.file.Path outputDir)
Unzip the givensource
Jar file in theoutputDir
.static int
unpackTarGz(java.nio.file.Path sourcePath, java.nio.file.Path outputDir)
static int
unpackZip(java.nio.file.Path source, java.nio.file.Path outputDir)
Unzip the givensource
Zip file in theoutputDir
.private static java.nio.file.Path
unpackZipEntry(org.apache.commons.compress.archivers.zip.ZipFile zipFile, org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path outputDir)
-
-
-
Field Detail
-
PERM_MASK
private static final int PERM_MASK
- See Also:
- Constant Field Values
-
ZIP_ENTRY_NAME_SEPARATOR
private static final java.lang.String ZIP_ENTRY_NAME_SEPARATOR
- See Also:
- Constant Field Values
-
BACKSLASH_ESCAPE_REPLACEMENT
private static final java.lang.String BACKSLASH_ESCAPE_REPLACEMENT
- See Also:
- Constant Field Values
-
BACKSLASH_PATTERN
private static final java.util.regex.Pattern BACKSLASH_PATTERN
-
-
Method Detail
-
unpackZip
public static int unpackZip(java.nio.file.Path source, java.nio.file.Path outputDir) throws java.io.IOException
Unzip the givensource
Zip file in theoutputDir
.- Parameters:
source
- the file to unzip.outputDir
- the output directory where the Zip will be unpacked.- Returns:
- the number of unpacked entries
- Throws:
java.io.IOException
-
fixPosixPermissions
private static void fixPosixPermissions(org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path entryPath)
-
unpack
private static int unpack(org.apache.commons.compress.archivers.zip.ZipFile zipFile, java.nio.file.Path outputDir, java.util.function.BiConsumer<org.apache.commons.compress.archivers.zip.ZipArchiveEntry,java.nio.file.Path> entryFixer) throws java.io.IOException, java.util.zip.ZipException
- Throws:
java.io.IOException
java.util.zip.ZipException
-
unpackZipEntry
private static java.nio.file.Path unpackZipEntry(org.apache.commons.compress.archivers.zip.ZipFile zipFile, org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path outputDir) throws java.io.IOException, java.util.zip.ZipException
- Throws:
java.io.IOException
java.util.zip.ZipException
-
unpackJar
public static int unpackJar(java.nio.file.Path source, java.nio.file.Path outputDir) throws java.io.IOException
Unzip the givensource
Jar file in theoutputDir
.- Parameters:
source
- the file to unzip.outputDir
- the output directory where the Jar will be unpacked. It does not have to exist beforehand.- Returns:
- the number of unpacked entries
- Throws:
java.io.IOException
-
unpackTarGz
public static int unpackTarGz(java.nio.file.Path sourcePath, java.nio.file.Path outputDir) throws java.io.IOException
- Throws:
java.io.IOException
-
unpack
static int unpack(org.apache.commons.compress.archivers.tar.TarArchiveInputStream zis, java.nio.file.Path outputDir) throws java.io.IOException
- Throws:
java.io.IOException
-
setPermissions
private static void setPermissions(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, java.nio.file.Path entryPath) throws java.io.IOException
- Throws:
java.io.IOException
-
packZip
public static int packZip(java.nio.file.Path source, java.nio.file.Path targetZip, boolean preserveRoot) throws java.io.IOException
Zip the givensource
file or folder in thetargetZip
Zip file. IfpreserveRoot
is set to true, the output Zip will contain the folder and its contents, only its contents otherwise.- Parameters:
source
- the folder to zip.targetZip
- the Zip file to create or overwrite.preserveRoot
- whether thesource
folder should be kept in the target Zip.- Returns:
- the number of packed entries
- Throws:
java.io.IOException
-
packJar
public static int packJar(java.nio.file.Path source, java.nio.file.Path targetJar, boolean preserveRoot) throws java.io.IOException
Zip the givensource
file or folder in thetargetJar
Jar file. IfpreserveRoot
is set to true, the output Zip will contain the folder and its contents, only its contents otherwise.- Parameters:
source
- the folder to zip.targetJar
- the Jar file to create or overwrite.preserveRoot
- whether thesource
folder should be kept in the target Jar.- Returns:
- the number of packed entries
- Throws:
java.io.IOException
-
packManifestIfAny
private static java.util.Set<java.nio.file.Path> packManifestIfAny(java.nio.file.Path source, org.apache.commons.compress.archivers.jar.JarArchiveOutputStream jos) throws java.io.IOException
Looks for META-INF/MANIFEST.MF file in the given source folder and add them as entries to the JarOutputStream.- Parameters:
source
- the folder to jar. If not a directory, returns 0 and do nothingpreserveRoot
- whether the root folderjos
- the jar output stream to write- Returns:
- set of paths to the META-INF and MANIFEST.MF, empty set otherwise.
- Throws:
java.io.IOException
-
checkPathExists
private static java.nio.file.Path checkPathExists(java.nio.file.Path source, java.lang.String msg)
-
newBufferedOutputStream
private static java.io.BufferedOutputStream newBufferedOutputStream(java.nio.file.Path path) throws java.io.IOException
- Throws:
java.io.IOException
-
packEntries
private static int packEntries(java.nio.file.Path source, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, boolean preserveRoot, java.util.Set<java.nio.file.Path> pathToExcludes) throws java.io.IOException
- Throws:
java.io.IOException
-
entryNameFrom
private static java.lang.String entryNameFrom(java.nio.file.Path path, boolean isDirectoryw)
-
putFileEntry
private static void putFileEntry(java.nio.file.Path file, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath) throws java.io.IOException
- Throws:
java.io.IOException
-
putDirectoryEntry
private static void putDirectoryEntry(java.nio.file.Path dir, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath) throws java.io.IOException
- Throws:
java.io.IOException
-
createArchiveEntry
private static org.apache.commons.compress.archivers.zip.ZipArchiveEntry createArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.lang.String entryName)
-
-