@Test public void compressToTar() { String srcPath = "E:\\my_document\\BeanLaboratory\\BootLabo\\target\\classes\\back_test\\ReDx001107912"; String desPath = "E:\\my_document\\BeanLaboratory\\BootLabo\\target\\classes\\back_test\\ReDx001107912\\test.tar.gz"; File srcFile = new File(srcPath); if (srcFile.exists()) { FileCommonUtil.compressToTar(srcFile, desPath); } }
public static boolean compressToTar(File inputFile, String outFilePath) { if (inputFile == null || !inputFile.isDirectory()) { logger.error("inputFile is null or not directory"); return false; } File outputFile = new File(outFilePath); if (outputFile.exists()) { boolean deleteExist = outputFile.delete(); if (!deleteExist) { logger.error("deleteExist failed, outFilePath: [{}]", outFilePath); } } File outputFileParent = new File(outputFile.getParent()); if (!outputFileParent.exists()) { logger.error("outFilePath parent dir not exist"); return false; } File[] sourceFiles = inputFile.listFiles(); if (sourceFiles == null || sourceFiles.length == 0) { logger.error("sourceFiles is null or empty"); return false; } try (FileOutputStream fileOutputStream = new FileOutputStream(outFilePath); CompressorOutputStream gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fileOutputStream); TarArchiveOutputStream os = new TarArchiveOutputStream(gzippedOut)) { os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : sourceFiles) { String p = file.getParentFile().getParent(); String pn = file.getAbsolutePath().substring(p.length() + 1); ArchiveEntry archiveEntry = new TarArchiveEntry(file, pn); os.putArchiveEntry(archiveEntry); IOUtils.copy(new FileInputStream(file), os); os.closeArchiveEntry(); } } catch (Exception e) { logger.error("compressToTar Exception: [{}]", e); return false; } return true; }
FileCommonUtil { public static boolean compressToTar(File inputFile, String outFilePath) { if (inputFile == null || !inputFile.isDirectory()) { logger.error("inputFile is null or not directory"); return false; } File outputFile = new File(outFilePath); if (outputFile.exists()) { boolean deleteExist = outputFile.delete(); if (!deleteExist) { logger.error("deleteExist failed, outFilePath: [{}]", outFilePath); } } File outputFileParent = new File(outputFile.getParent()); if (!outputFileParent.exists()) { logger.error("outFilePath parent dir not exist"); return false; } File[] sourceFiles = inputFile.listFiles(); if (sourceFiles == null || sourceFiles.length == 0) { logger.error("sourceFiles is null or empty"); return false; } try (FileOutputStream fileOutputStream = new FileOutputStream(outFilePath); CompressorOutputStream gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fileOutputStream); TarArchiveOutputStream os = new TarArchiveOutputStream(gzippedOut)) { os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : sourceFiles) { String p = file.getParentFile().getParent(); String pn = file.getAbsolutePath().substring(p.length() + 1); ArchiveEntry archiveEntry = new TarArchiveEntry(file, pn); os.putArchiveEntry(archiveEntry); IOUtils.copy(new FileInputStream(file), os); os.closeArchiveEntry(); } } catch (Exception e) { logger.error("compressToTar Exception: [{}]", e); return false; } return true; } }
FileCommonUtil { public static boolean compressToTar(File inputFile, String outFilePath) { if (inputFile == null || !inputFile.isDirectory()) { logger.error("inputFile is null or not directory"); return false; } File outputFile = new File(outFilePath); if (outputFile.exists()) { boolean deleteExist = outputFile.delete(); if (!deleteExist) { logger.error("deleteExist failed, outFilePath: [{}]", outFilePath); } } File outputFileParent = new File(outputFile.getParent()); if (!outputFileParent.exists()) { logger.error("outFilePath parent dir not exist"); return false; } File[] sourceFiles = inputFile.listFiles(); if (sourceFiles == null || sourceFiles.length == 0) { logger.error("sourceFiles is null or empty"); return false; } try (FileOutputStream fileOutputStream = new FileOutputStream(outFilePath); CompressorOutputStream gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fileOutputStream); TarArchiveOutputStream os = new TarArchiveOutputStream(gzippedOut)) { os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : sourceFiles) { String p = file.getParentFile().getParent(); String pn = file.getAbsolutePath().substring(p.length() + 1); ArchiveEntry archiveEntry = new TarArchiveEntry(file, pn); os.putArchiveEntry(archiveEntry); IOUtils.copy(new FileInputStream(file), os); os.closeArchiveEntry(); } } catch (Exception e) { logger.error("compressToTar Exception: [{}]", e); return false; } return true; }  }
FileCommonUtil { public static boolean compressToTar(File inputFile, String outFilePath) { if (inputFile == null || !inputFile.isDirectory()) { logger.error("inputFile is null or not directory"); return false; } File outputFile = new File(outFilePath); if (outputFile.exists()) { boolean deleteExist = outputFile.delete(); if (!deleteExist) { logger.error("deleteExist failed, outFilePath: [{}]", outFilePath); } } File outputFileParent = new File(outputFile.getParent()); if (!outputFileParent.exists()) { logger.error("outFilePath parent dir not exist"); return false; } File[] sourceFiles = inputFile.listFiles(); if (sourceFiles == null || sourceFiles.length == 0) { logger.error("sourceFiles is null or empty"); return false; } try (FileOutputStream fileOutputStream = new FileOutputStream(outFilePath); CompressorOutputStream gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fileOutputStream); TarArchiveOutputStream os = new TarArchiveOutputStream(gzippedOut)) { os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : sourceFiles) { String p = file.getParentFile().getParent(); String pn = file.getAbsolutePath().substring(p.length() + 1); ArchiveEntry archiveEntry = new TarArchiveEntry(file, pn); os.putArchiveEntry(archiveEntry); IOUtils.copy(new FileInputStream(file), os); os.closeArchiveEntry(); } } catch (Exception e) { logger.error("compressToTar Exception: [{}]", e); return false; } return true; }  static boolean isFileSizeLegal(int length, Double maxSize); static String generateMappingPath(String rootPath, String hash, String nameSuffix); static byte[] encryptImgXor(byte[] fileBytes, int xor); static String getContent(File file); static boolean resizeImage(String srcPath, String destPath, int newWidth, int newHeight, boolean forceSize); static boolean changeSize(String srcPath, String destPath, int newWidth, int newHeight); static boolean compressToTar(File inputFile, String outFilePath); }
FileCommonUtil { public static boolean compressToTar(File inputFile, String outFilePath) { if (inputFile == null || !inputFile.isDirectory()) { logger.error("inputFile is null or not directory"); return false; } File outputFile = new File(outFilePath); if (outputFile.exists()) { boolean deleteExist = outputFile.delete(); if (!deleteExist) { logger.error("deleteExist failed, outFilePath: [{}]", outFilePath); } } File outputFileParent = new File(outputFile.getParent()); if (!outputFileParent.exists()) { logger.error("outFilePath parent dir not exist"); return false; } File[] sourceFiles = inputFile.listFiles(); if (sourceFiles == null || sourceFiles.length == 0) { logger.error("sourceFiles is null or empty"); return false; } try (FileOutputStream fileOutputStream = new FileOutputStream(outFilePath); CompressorOutputStream gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, fileOutputStream); TarArchiveOutputStream os = new TarArchiveOutputStream(gzippedOut)) { os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : sourceFiles) { String p = file.getParentFile().getParent(); String pn = file.getAbsolutePath().substring(p.length() + 1); ArchiveEntry archiveEntry = new TarArchiveEntry(file, pn); os.putArchiveEntry(archiveEntry); IOUtils.copy(new FileInputStream(file), os); os.closeArchiveEntry(); } } catch (Exception e) { logger.error("compressToTar Exception: [{}]", e); return false; } return true; }  static boolean isFileSizeLegal(int length, Double maxSize); static String generateMappingPath(String rootPath, String hash, String nameSuffix); static byte[] encryptImgXor(byte[] fileBytes, int xor); static String getContent(File file); static boolean resizeImage(String srcPath, String destPath, int newWidth, int newHeight, boolean forceSize); static boolean changeSize(String srcPath, String destPath, int newWidth, int newHeight); static boolean compressToTar(File inputFile, String outFilePath);  }