import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class MainClass
{
/**
* 将kuka.txt文件通过GZIPOutputStream压缩为test.gz
*/
public void compression()
{
try
{
FileInputStream in=new FileInputStream("c:/kuka.txt");
GZIPOutputStream out=new GZIPOutputStream(new FileOutputStream("c:/test.gz"));
System.out.println("c:/kuka.txt---->c:/tets.gz");
int c;
while ((c=in.read())!=-1)
{
out.write(c); //写压缩文件
}
in.close();
out.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 通过GZIPInputStream解压"test.gz"文件,将文件内容显示出来,并恢复为lovekuka.txt
*/
public void decompression()
{
try
{
//首先读取压缩包中的内容,并显示出来
BufferedReader reader=new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("c:/test.gz"))));
String s;
while ((s=reader.readLine())!=null)
{
System.out.println(s); //读每一行,并显示出来
}
reader.close();
System.out.println("c:/test.gz--->c:/lovekuka.txt");
GZIPInputStream stream=new GZIPInputStream(new FileInputStream("c:/test.gz"));
FileOutputStream out=new FileOutputStream("c:/lovekuka.txt");
int c;
while((c=stream.read())!=-1)
{
out.write(c);
}
stream.close();
out.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}