0

下面是代码,最终结果是一个没有 .tar.gz 扩展名的常规文件,有人可以帮忙吗?谢谢你。

String reportId = estatConfig.getId().getReportId(); String targetOutPutReportDirForTarAndGzip = homeDir + estatConfig.getLocationOutPdf()+ StatementConstants.OUTPUT_PREFIX_DIR.value()+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMyyy")) + EStatementConstants.OUTPUT_POSTFIX_DIR.value() + reportId; 字符串 outputPdf = homeDir + File.separatorChar + estatConfig.getLocationOutPdf() + File.separatorChar + reportId + EStatementConstants.EXTENSION_PDF.value(); 文件 outputPdfFile = new File(outputPdf); if (outputPdfFile.exists()) { try { Files.move(Paths.get(outputPdf), Paths.get(targetOutPutReportDirForTarAndGzip), REPLACE_EXISTING);

            } catch (IOException e) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                logger.info("Exception during moving of pdf file to target gzip dir => " + e.fillInStackTrace());
            }
            Path targetOutPutReportDirForTarAndGzipPath = Paths.get(targetOutPutReportDirForTarAndGzip);
            // get folder name as zip file name
            String tarFileName = targetOutPutReportDirForTarAndGzipPath.getFileName().toString()
                    + EStatementConstants.TAR_AND_GZIP_EXTENSION.value();
            try (OutputStream fOut = Files.newOutputStream(Paths.get(tarFileName));
                    BufferedOutputStream buffOut = new BufferedOutputStream(fOut);
                    GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(buffOut);
                    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut)) {
                Files.walkFileTree(targetOutPutReportDirForTarAndGzipPath, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                        // only copy files, no symbolic links
                        if (attributes.isSymbolicLink()) {
                            return FileVisitResult.CONTINUE;
                        }
                        // get filename
                        Path targetFile = targetOutPutReportDirForTarAndGzipPath.relativize(file);
                        try {
                            TarArchiveEntry tarEntry = new TarArchiveEntry(file.toFile(), targetFile.toString());
                            tOut.putArchiveEntry(tarEntry);
                            Files.copy(file, tOut);
                            tOut.closeArchiveEntry();
                            logger.info("tarred and gzipped: " + file + "successfully");
                        } catch (IOException e) {
                            StringWriter sw = new StringWriter();
                            PrintWriter pw = new PrintWriter(sw);
                            e.printStackTrace(pw);
                            logger.info("Unable to tar and gzip: " + file + "=> " + e.fillInStackTrace());
                        }
                        return FileVisitResult.CONTINUE;
                    }
        }
    }
}

以上代码来自https://mkyong.com/java/how-to-create-tar-gz-in-java/

4

1 回答 1

0

问题已解决,它似乎从下面的行更改

String tarFileName = targetOutPutReportDirForTarAndGzipPath.getFileName().toString()
                + EStatementConstants.TAR_AND_GZIP_EXTENSION.value();

String tarFileName = targetOutPutReportDirForTarAndGzipPath
                + EStatementConstants.TAR_AND_GZIP_EXTENSION.value();
于 2020-11-10T07:37:05.030 回答