Java implements the packaging of multiple files into GZ packages

Angryshark_ one hundred and twenty-eight 2022-06-24 07:14:57 阅读数:380

javaimplementspackagingmultiplefiles

POM Reference reference

 <!-- pack -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.9</version>
</dependency>
/**
* The files are packed into tar.gz
*
* @param files File list
* @param targetPath Destination file path
* @return
*/
public boolean tarFiles(File[] files, String targetPath) {
FileOutputStream out = null;
try {
out = new FileOutputStream(targetPath);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return false;
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
for (File file : files) {
try {
log.info(MessageFormat.format(" Are packaged :{0}", file.getName()));
os.putArchiveEntry(new TarArchiveEntry(file));
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
copyright:author[Angryshark_ one hundred and twenty-eight],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240050386333.html