直接上代码:
org.apache.commons
commons-compress
1.4.1
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.log4j.Logger;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Rorschach
* @title: TestAll
* @description: TODO
* @date 2019-05-229:40
*/
public class CompressUtil {
private String zipFileName; // 目的地Zip文件
private String sourceFileName; //源文件(带压缩的文件或文件夹)
private static Logger logger = Logger.getLogger(CompressUtil.class.getName());
public CompressUtil() {
}
public CompressUtil(String zipFileName, String sourceFileName) {
this.zipFileName = zipFileName;
this.sourceFileName = sourceFileName;
}
/**
* @description: 遍历文件夹,对归档好的文件夹进行压缩
* @author Rorschach
* @date 2019-05-23 15:55
*/
public void fullTar(String tempDir) throws Exception {
String path = getPath(tempDir);
File allFile = new File(path);
File[] files = allFile.listFiles();
String resultName = "";
for (File file : files) {
resultName = path + "\\" + file.getName() + ".tar";
new CompressUtil(resultName, path + "\\" + file.getName() + "\\").tar();
gzipCompression(resultName, resultName + ".gz");
}
}
/**
* @description: 获取包含了归档好的文件的路径
* @author Rorschach
* @date 2019-05-23 15:54
*/
public String getPath(String path) {
File allFile = new File(path);
File[] files = allFile.listFiles();
String dirPath = "";
for (int i = 0; i < files.length; i++) {
logger.info(files[i].getName());
if (files[i].isDirectory() && files[i].getName().startsWith("RADA_ST_DOR_L2_CAP")) {
logger.info("true");
dirPath = files[i].getParent();
break;
} else {
String rs = getPath(files[i].getPath());
if (rs != null && !"".equals(rs)) {
return rs;
}
}
}
if (dirPath.length() != 0) {
return dirPath;
} else {
return "没有找到目录";
}
}
/**
* @description: zip压缩
* @author Rorschach
* @date 2019-05-23 15:54
*/
public void zip() throws Exception {
//File zipFile = new File(zipFileName);
logger.info("压缩中...");
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
//创建zip输出流
out = new ZipOutputStream(new FileOutputStream(zipFileName));
File sourceFile = new File(sourceFileName);
//调用函数
compress(out, sourceFile, sourceFile.getName());
} catch (Exception e1) {
logger.info("压缩异常!!!!!!!!!请联系系统管理员");
e1.printStackTrace();
} finally {
if (bos != null)
bos.close();
if (out != null)
out.close();
}
System.out.println("压缩完成");
}
/**
* @description: tar压缩
* @author Rorschach
* @date 2019-05-23 15:53
*/
public void tar() throws Exception {
//File zipFile = new File(zipFileName);
logger.info("tar压缩中...");
TarArchiveOutputStream out = null;
BufferedOutputStream bos = null;
try {
//创建tar输出流
out = new TarArchiveOutputStream(new FileOutputStream(zipFileName), 512000);
File sourceFile = new File(sourceFileName);
//调用函数
tarCompress(out, sourceFile, sourceFile.getName());
} catch (Exception e1) {
logger.info("压缩异常!!!!!!!!!请联系系统管理员");
e1.printStackTrace();
} finally {
if (out != null)
out.close();
if (bos != null)
bos.close();
}
System.out.println("压缩完成");
}
/**
* @description: zip压缩
* @author Rorschach
* @date 2019-05-23 15:53
*/
public void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
//如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
{
System.out.println(base + "/");
out.putNextEntry(new ZipEntry(base + "/"));
} else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
{
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], base + "/" + flist[i].getName());
}
}
} else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入tar文件中
{
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos);
int tag;
System.out.println(base);
//将源文件写入到zip文件中,若原本是zip文件,则压缩成压缩比率更高的7Z文件
while ((tag = bis.read()) != -1) {
out.write(tag);
}
fos.close();
out.flush();
bis.close();
}
}
/**
* @description:打包成Tar
* @author Rorschach
* @date 2019-05-23 15:52
*/
public void tarCompress(TarArchiveOutputStream out, File sourceFile, String base) throws Exception {
InputStream inputStream;
//如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
{
System.out.println(base + "/");
out.putArchiveEntry(new TarArchiveEntry(base + "/"));
} else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
{
for (int i = 0; i < flist.length; i++) {
tarCompress(out, flist[i], base + "/" + flist[i].getName());
}
}
} else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
{
out.putArchiveEntry(new TarArchiveEntry(sourceFile, sourceFile.getName()));//打包的时候只是包含文件,不带路径
inputStream = new FileInputStream(sourceFile);
IOUtils.copy(inputStream, out);
out.closeArchiveEntry();
}
}
/**
* @description: 把tar压缩成gz文件
* @author Rorschach
* @date 2019-05-23 15:52
*/
public static boolean gzipCompression(String filePath, String resultFilePath) throws IOException {
logger.info(" gzipCompression -> Compression start!");
InputStream fin = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
GzipCompressorOutputStream gcos = null;
try {
fin = Files.newInputStream(Paths.get(filePath));
bis = new BufferedInputStream(fin);
fos = new FileOutputStream(resultFilePath);
bos = new BufferedOutputStream(fos);
gcos = new GzipCompressorOutputStream(bos);
byte[] buffer = new byte[1024];
int read = -1;
while ((read = bis.read(buffer)) != -1) {
gcos.write(buffer, 0, read);
}
} finally {
new File(filePath).delete();
if (gcos != null)
gcos.close();
if (bos != null)
bos.close();
if (fos != null)
fos.close();
if (bis != null)
bis.close();
if (fin != null)
fin.close();
}
logger.info(" gzipCompression -> Compression end!");
return true;
}
}