package com.runbatch.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GzipUtil {
private static final Logger logger = LoggerFactory.getLogger(GzipUtil.class);
/**
* 解压tar.gz文件
*
* @param targzFile
* 需要解压的文件路径
* @param outputDir
* 解压后的文件存放的文件夹
*
* @return 解压结果
*/
public static boolean unzipGzipFile(String targzFile, String outputDir) {
FileInputStream fileInputStream = null;
ArchiveInputStream archiveInputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
fileInputStream = new FileInputStream(targzFile);
GZIPInputStream gzipInputStream = new GZIPInputStream(new BufferedInputStream(fileInputStream));
archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream);
bufferedInputStream = new BufferedInputStream(archiveInputStream);
TarArchiveEntry tarArchiveEntry = (TarArchiveEntry) archiveInputStream.getNextEntry();
while (tarArchiveEntry != null) {
String name = tarArchiveEntry.getName();
String[] names = name.split("/");
String fileName = outputDir;
for (String str : names) {
fileName = fileName + File.separator + str;
}
File file = mkFile(fileName);
logger.info("解压出的文件为:{}", file.toString());
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
int b;
while ((b = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(b);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
tarArchiveEntry = (TarArchiveEntry) archiveInputStream.getNextEntry();
}
return true;
} catch (Exception e) {
logger.error("error", e);
} finally {
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
} catch (IOException e) {
logger.error("error", e);
}
}
return false;
}
private static File mkFile(String fileName) {
File f = new File(fileName);
try {
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
f.createNewFile();
} catch (IOException e) {
logger.error("error", e);
}
return f;
}
/**
* 校验数据文件 .dat
*
* @param dataFileDir
* 数据文件所在的文件夹路径
* @param dataFileOk
* 就绪文件名
* @return 数据文件列表
*/
public static List<String> checkDataFile(List<String> dataFileOks) {
BufferedReader bufferedReader = null;
List<String> dataFileList = new ArrayList<String>();
try {
for (String dataFileOk : dataFileOks) {
File dataOkfile = new File(dataFileOk);
FileInputStream fileInputStream = new FileInputStream(dataOkfile);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
String datFileName = bufferedReader.readLine();
String datFileSize = bufferedReader.readLine();
String datFileLine = bufferedReader.readLine();
String datFileTag = bufferedReader.readLine();
logger.info("数据就绪文件内容:数据文件名:{}\n 数据文件大小:{}\n 数据文件行数:{}\n 数据文件类型标识:{}", datFileName, datFileSize,
datFileLine, datFileTag);
if (bufferedReader != null) {
bufferedReader.close();
}
String datFilePath = dataOkfile.getParent() + File.separator + datFileName;
File datFile = new File(datFilePath);
if (!datFile.exists()) {
throw new FileNotFoundException(datFilePath + "文件不存在!");
}
if (!Long.toString(datFile.length()).equals(datFileSize.trim())) {
throw new Exception(datFilePath + "文件大小错误!");
}
if (!datFileTag.equals("I") || !datFileName.endsWith("INC.dat")) {
throw new Exception(datFilePath + "不是增量文件或文件名错误!");
}
dataFileList.add(datFilePath);
}
} catch (Exception e) {
logger.error("error", e);
return null;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
logger.error("error", e);
}
}
}
return dataFileList;
}
/**
* 校验压缩文件 .tar.gz
*
* @param zipFileOkPath
* 包就绪文件的路径
* @param zipFileOk
* 包就绪文件名
* @return true-校验通过,false-校验失败
*/
public static boolean checkGzipFile(String zipFileOkPath, String zipFileOk) {
BufferedReader bufferedReader = null;
boolean isZipFileComplete = false;
try {
FileInputStream fileInputStream = new FileInputStream(zipFileOkPath + zipFileOk);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
String gzipFileName = bufferedReader.readLine();
String gzipFileSize = bufferedReader.readLine();
logger.info("包就绪文件内容:包文件名:{}\n包文件大小:{}", gzipFileName, gzipFileSize);
String gzipFilePath = zipFileOkPath + gzipFileName;
File gzipFile = new File(gzipFilePath);
if (!gzipFile.exists()) {
throw new FileNotFoundException(gzipFilePath + "文件不存在!");
}
if (!Long.toString(gzipFile.length()).equals(gzipFileSize.trim())) {
throw new Exception(gzipFilePath + "文件大小错误!");
}
isZipFileComplete = true;
} catch (Exception e) {
logger.error("error", e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
logger.error("error", e);
}
}
}
return isZipFileComplete;
}
/**
*
* @Title: pack
* @Description: 将一组文件打成tar包
* @param sources
* 要打包的原文件数组
* @param target
* 打包后的文件
* @return File 返回打包后的文件
*/
public static File pack(File[] sources, File target) {
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
logger.error("将一组文件打成tar包的时候生成文件输出流时发生异常{}", e1);
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
FileInputStream in = null;
for (File file : sources) {
try {
in = new FileInputStream(file);
TarArchiveEntry entry = new TarArchiveEntry(file.getName());
entry.setSize(file.length());
os.putArchiveEntry(entry);
IOUtils.copy(in, os);
} catch (FileNotFoundException e) {
logger.error("将一组文件打成tar包时发生FileNotFoundException异常{}", e);
} catch (IOException e) {
logger.error("将一组文件打成tar包时发生IOException异常{}", e);
} finally {
if (os != null) {
try {
os.closeArchiveEntry();
} catch (IOException e) {
logger.error("closeArchiveEntry时发生IOException异常{}", e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
logger.error("关闭FileInputStream时发生IOException异常{}", e);
}
}
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
logger.error("关闭FileOutputStream时发生IOException异常{}", e);
}
}
return target;
}
/**
*
* @Title: compress
* @Description: 将文件用gzip压缩
* @param source
* 需要压缩的文件
* @return File 返回压缩后的文件
*/
public static File compress(File source) {
File target = new File(source.getAbsolutePath() + ".gz");
if (target.exists()) {
target.delete();
}
FileInputStream in = null;
GZIPOutputStream out = null;
try {
target.createNewFile();
in = new FileInputStream(source);
out = new GZIPOutputStream(new FileOutputStream(target));
byte[] array = new byte[1024];
int number = -1;
while ((number = in.read(array, 0, array.length)) != -1) {
out.write(array, 0, number);
}
} catch (FileNotFoundException e) {
logger.error("将一组文件打成gz包时发生FileNotFoundException异常{}", e);
return null;
} catch (IOException e) {
logger.error("将一组文件打成gz包时发生IOException异常{}", e);
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
logger.error("将一组文件打成gz包关闭输入流时发生IOException异常{}", e);
return null;
}
}
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
logger.error("将一组文件打成gz包关闭输出流时发生IOException异常{}", e);
return null;
}
}
}
return target;
}
}