Reputation: 1384
I have the following file structure:
C:/test/example1/file1.txt
C:/test/file2.txt
C:/test/file3.txt
How can I archive those files and directories so that the resulting zip looks like that:
Archive.zip
example
file1.txt
file2.txt
file3.txt
Like WinRar would do when selecting multiple files and folders to compress.
Code:
Path baseDir = Paths.get(content.toString());
Path zipPath = Paths.get(mainJarDir.toString());
Map<String, ?> env = Collections.singletonMap("create", "true");
URI uri = URI.create("jar:" + zipPath.toUri());
try {
final FileSystem zipfs = FileSystems.newFileSystem(uri, env);
}
catch (Exception e) {
e.printStackTrace();
try {
MoreFiles.copyRecursive(baseDir, zipfs.get("/"), RecursionMode.FAIL_FAST, StandardCopyOption.REPLACE_EXISTING);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 0
Views: 122
Reputation: 121710
This is done using one of my packages:
final Path baseDir = Paths.get("c:\\test");
final Path zipPath = Paths.get("path/to/zipfile");
final Map<String, ?> env = Collections.singletonMap("create", "true");
final URI uri = URI.create("jar:" + zipPath.toUri());
try (
final FileSystem zipfs = FileSystems.newFileSystem(uri, env);
) {
// in java7-fs-more
MoreFiles.copyRecursive(baseDir, zipfs.getPath("/"), RecursiveMode.FAIL_FAST);
}
Upvotes: 1