最近准备做个日志分析工具,现网的日志运维人员打成了gz压缩包放在某个ftp上,顺便就研究一下java如何读取gzip,瞎写了个例子,批量读取gzip文件中的日志,然后汇总在一起。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class UnZip {
public static String visitGZip(File targzFile) throws IOException {
FileInputStream fileIn = null;
BufferedInputStream bufIn = null;
GZIPInputStream gzipIn = null;
try {
fileIn = new FileInputStream(targzFile);
bufIn = new BufferedInputStream(fileIn);
gzipIn = new GZIPInputStream(bufIn); // first unzip the input file
return new String(IOUtils.toByteArray(gzipIn));
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
try {
File of = new File("D:\\日志\\cn");
StringBuffer sb = new StringBuffer();
for (File f : of.listFiles()) {
if (f.isDirectory()) {
for (File ff : f.listFiles()) {
sb.append(visitGZip(ff)).append("\n");
}
}
else {
System.out.println(f.getName());
}
}
FileUtils.writeByteArrayToFile(new File("d:\\rs.txt"), sb.toString().getBytes());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}