
automately.core.util.file.ZipUtil Maven / Gradle / Ivy
package automately.core.util.file;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.*;
/**
* The ZipUtil is a simple zip utility that is used by Automately Core. It provides
* basic zip and unzip functionality along with the ability to by default exclude files.
*/
public class ZipUtil {
/**
* Returns a zip file system
* @param zipFilename to construct the file system from
* @param create true if the zip file should be created
* @return a zip file system
* @throws IOException
*/
private static FileSystem createZipFileSystem(String zipFilename,
boolean create)
throws IOException {
// convert the filename to a URI
final Path path = Paths.get(zipFilename);
final URI uri = URI.create("jar:file:" + path.toUri().getPath());
final Map env = new HashMap<>();
if (create) {
env.put("create", "true");
}
return FileSystems.newFileSystem(uri, env);
}
public static void unzip(String zipFilename, String destDirname)
throws IOException{
final Path destDir = Paths.get(destDirname);
//if the destination doesn't exist, create it
if(Files.notExists(destDir)){
Files.createDirectories(destDir);
}
try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, false)){
final Path root = zipFileSystem.getPath("/");
Path[] files = Files.walk(root)
.filter(Files::isRegularFile).toArray(Path[]::new);
for(Path file : files){
Path dest = Paths.get(destDir.toString(), file.toString());
if(Files.notExists(dest.getParent())){
Files.createDirectories(dest.getParent());
}
Files.copy(file, dest, StandardCopyOption.REPLACE_EXISTING);
}
}
}
public static void create(String zipFilename, String directory) throws IOException {
create(zipFilename, directory, new Object[0]);
}
public static void create(String zipFilename, String directory , Object[] ignored)
throws IOException {
try (FileSystem zipFileSystem = createZipFileSystem(zipFilename, true)) {
final Path root = zipFileSystem.getPath("/");
final Path src = Paths.get(directory);
//add a file to the zip file system
if(!Files.isDirectory(src)){
throw new RuntimeException(directory + " is not a valid directory");
} else{
Path[] files = Files.walk(src)
.filter(Files::isRegularFile).toArray(Path[]::new);
for(Path file : files){
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy