gz是Linux和OSX中常见的压缩文件格式,下面是用java压缩和解压缩gz包的例子
public class GZIPcompress { public static void FileCompress(String file, String outgz) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedOutputStream bs = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outgz))); int c;
while ((c = br.read()) != -1) {
bs.write(c);
}
br.close();
bs.close();
} public static String FileUnCompress(String filegz) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(filegz))));
String s;
StringBuffer sb = new StringBuffer();
while ((s = bf.readLine()) != null) {
sb.append(s);
}
bf.close();
return sb.toString();
} public static void main(String[] args) throws IOException {
String fileOut = "test.gz";
String in = "test.txt"; FileCompress(in, fileOut);
String out = FileUnCompress(fileOut); System.out.println(out);
} }