用GZIP进行文件压缩与解压

使用GZIPInputStream与GZIPOutputStream进行单个文件的压缩与解压

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Test {
	public static void main(String[] args) {
		try {
			//使用GZIP进行压缩及解压
			BufferedReader in = new BufferedReader(new FileReader("d:/ui.txt"));
			BufferedOutputStream bos = new BufferedOutputStream(
					new GZIPOutputStream(new FileOutputStream("d:/a.gz")));
			int c;
			while ((c = in.read()) != -1) {
				bos.write(c);
			}
			in.close();
			bos.close();

			
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new GZIPInputStream(new FileInputStream("d:/a.gz"))));
			String s1,s="";
			
			while((s1=br.readLine())!=null){
				s += s1;
			}
			System.out.println(s);
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 

 

你可能感兴趣的:(java,C++,c,UI,C#)