简体   繁体   English

Java:如何从资源复制文件夹并复制到临时目录?

[英]Java : How to copy folder with contents from resource and copy to temp directory?

Project structure:项目结构:

src
|
|--resource
    |
    |--PMD
        |-pmd-bin
            |-test.bat
        |-report
            |-report.xml
    |
    |--staticresource

Using maven-assembly plugin, I am including the resources in the jar file.使用maven-assembly插件,我将资源包含在 jar 文件中。

As PMD folder will be used by the applcaition, I would like to create a copy of the PMD folder in the temp directory, so that I can start reading the bat files and other files from that temp directory.由于应用程序将使用 PMD 文件夹,我想在临时目录中创建 PMD 文件夹的副本,以便我可以开始从该临时目录中读取 bat 文件和其他文件。

ISSUE问题

When the jar loads, it fails to read the PMD folder inside resource.当 jar 加载时,它无法读取资源内的 PMD 文件夹。

Tried:试过:

        InputStream pmdFolder = classLoader.getResourceAsStream("PMD");
        InputStreamReader isr = new InputStreamReader(pmdFolder, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        List<URL> collect = br.lines().map(l -> "PMD" + "/" + l)
                .map(r -> classLoader.getResource(r))
                .collect(toList());
        Path tempPMDFolder = null;
        Path pmd = Files.createTempDirectory("PMD");
        for (URL url : collect) {
            System.out.println(url.toString());
            createSameTempStructure(url, pmd);
        }

private static void createSameTempStructure(URL url, Path pmd) throws IOException {
    //tempPMDFolder.toFile().deleteOnExit();
    try(final InputStream is = url.openStream()) {
        File file = FileUtils.toFile(url);
        System.out.println("file -> "+file.getName());
        if(file.isDirectory()){
            Path tempPMDFolder = createTempPMDFolder(pmd, file.getName());
            System.out.println("tempPMDFolder -> "+tempPMDFolder.toString());
            FileUtils.copyDirectory(file, tempPMDFolder.toFile());
        } else {
            try (OutputStream outputStream = new FileOutputStream(file)) {
                IOUtils.copy(is, outputStream);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

Here it just creates the PMD folder in temp directory and nothing, inner files and folders are not copied.在这里,它只是在 temp 目录中创建 PMD 文件夹,没有任何内容,内部文件和文件夹不会被复制。 Any way we can achieve this?我们有什么办法可以做到这一点?

Here is what I came up with.这是我想出的。

Converted the folder to zip and put that zipped file in resources.将文件夹转换为zip并将该压缩文件放入资源中。 As inputstream can only read through a file.由于输入流只能读取文件。

InputStream pmdFolder = classLoader.getResourceAsStream("PMD.zip");
Path tempPMDDirectory = Files.createTempDirectory("PMD");

Then extracted the zip contents to the temp directory and then using that overall application.然后将 zip 内容提取到临时目录,然后使用整个应用程序。

if (pmdFolder != null) {
    try (ZipInputStream zipInputStream = new ZipInputStream(pmdFolder)) {
        // Extract the zip contents and keep in temp directory
        extract(zipInputStream, tempPMDDirectory.toFile());
    }
}
public static void extract(ZipInputStream zip, File target) throws IOException {
    try {
        ZipEntry entry;

        while ((entry = zip.getNextEntry()) != null) {
            File file = new File(target, entry.getName());

            if (!file.toPath().normalize().startsWith(target.toPath())) {
                throw new IOException("Bad zip entry");
            }

            if (entry.isDirectory()) {
                file.mkdirs();
                continue;
            }

            byte[] buffer = new byte[BUFFER_SIZE];
            file.getParentFile().mkdirs();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            int count;

            while ((count = zip.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }

            out.close();
        }
    } finally {
        zip.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM