我有一个groovy脚本,需要2个zip文件,解压缩,然后处理它们。
我遇到的问题是,当我解压缩zip文件时,每个解压缩文件生成一个子文件夹然后解压缩文件的内容而不是内容本身。
示例:的
目前,当我解压缩
时会发生这种情况 content-1232.zip -> /content-1232/content/<all content here>
我想要的是
content-1232.zip -> /content-1232/<all_content>
或
放入新目录
content-1232.zip -> /new_directory/<all_content>
我尝试了这个但无济于事:
Path basePath = Paths.get(output.getAbsolutePath())
Path srcPath = Paths.get(pdfContent.getAbsolutePath())
Path targetPath = basePath
Files.move(srcPath, targetPath.resolve(basePath.relativize(srcPath)))
看起来很简单,但我没有运气。我怎么能在groovy中完成这个?任何帮助将不胜感激
修改
static void main(String[] args) {
logger.info("Beginning unpacking of content...")
def output = new File("/Users/Haddad/Desktop/workspace/c")
def pdfContent = new File("/Users/Haddad/Desktop/workspace/c/pdf-content")
// Look for test1 and test2 production content zip file, ensure there's only 1 and unzip
List appZip = FileUtil.discoverFiles(output, true, "test1-production-content-.*\\.zip")
List compliZip = FileUtil.discoverFiles(output,true,"test2-production-content-.*\\.zip")
assert appZip.size() == 1, "Did not find expected number of test1 content zip, actual size is: " + appZip.size()
assert compliZip.size() == 1, "Did not find expected number of test2 content zip, actual size is " + appZip.size()
def outputAppZip = new File(output.getAbsolutePath()+"/"+appZip.get(0).getName()+"-intermediate");
def outputCompliZip = new File(output.getAbsolutePath()+"/"+compliZip.get(0).getName()+"-intermediate");
ZipUtil.unzipToDisk(appZip.get(0), outputAppZip )
ZipUtil.unzipToDisk(compliZip.get(0), outputCompliZip )
// move it to pdf content
List applicationContent = FileUtil.discoverDirectories(output, "one-production-content-.*-intermediate", true)
assert applicationContent.size() == 1, "Did not find expected number of test1 contents " + applicationContent.size()
def success = FileUtil.copy(applicationContent.get(0), pdfContent)
assert success, "Could not copy tt content"
// move pdf content
List complianceContent = FileUtil.discoverDirectories(output, "two-production-content-.*-intermediate", true)
assert complianceContent.size() == 1, "Did not find expected number of test2 contents " + complianceContent.size()
success = FileUtil.copy(complianceContent.get(0), pdfContent)
assert success, "Could not copy pdf content"
// rename to not be unsupported
List unsupportedDirs = FileUtil.discoverDirectories(pdfContent, "_unsupported_.*", true)
for (File file : unsupportedDirs) {
file.renameTo(new File(file.getParentFile(), file.getName().replace("_unsupported_", "")))
}
logger.info("Completed!")
}
答案 0 :(得分:0)
这个怎么样?
<强>输入强>:
test.zip
--- test
------ test.txt
<强>输出强>:
<someFolder>
--- test.txt
<强>代码强>:
public class Main {
public static void main(String[] args) throws IOException {
Main main = new Main();
Path input = main.getInputPath(args);
Path output = main.getOutputPath(args);
main.extract(input, output);
main.flatten(output);
}
private final Path getInputPath(String[] args) {
if (args.length < 1) {
throw new IllegalArgumentException("Usage: Main <inputFile> [<outputDirectory>]");
}
Path input = Paths.get(args[0]);
if (!isRegularFile(input)) {
throw new IllegalArgumentException(String.format("%s is not a file.", input.toFile().getAbsolutePath()));
}
return input;
}
private final Path getOutputPath(String[] args) throws IOException {
return args.length < 2 ? Files.createTempDirectory(null) :
Files.createDirectories(Paths.get(args[1]));
}
private final void extract(Path input, Path output) throws IOException {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(input.toFile()))) {
for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
String fileName = ze.getName();
if (fileName.contains("__MACOSX")) {
continue;
}
File file = new File(output.toFile(), fileName);
if (ze.isDirectory()) {
Files.createDirectories(file.toPath());
continue;
}
System.out.println("Extracting: " + file.getAbsolutePath());
try (FileOutputStream fos = new FileOutputStream(file, true)) {
byte[] buffer = new byte[4096];
for (int len = zis.read(buffer); len > 0; len = zis.read(buffer)) {
fos.write(buffer, 0, len);
}
}
}
}
}
private final void flatten(Path output) throws IOException {
List<Path> children = Files.list(output)
.collect(toList());
if (children.size() == 1) {
Path path = children.get(0);
Files.list(path)
.forEach(f -> {
try {
System.out.printf("Moving %s to %s.\n", f.toFile().getAbsolutePath(),
output.toFile().getAbsolutePath());
Files.move(f, Paths.get(output.toFile().getAbsolutePath(), f.toFile().getName()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
Files.delete(path);
}
}
}