GZIPOutputStream压缩文件
程序员文章站
2022-05-30 21:46:41
...
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class ZipFile {
public static void main(String[] args) throws IOException{
// zip(); //压缩文件
unzip(); //解压缩文件
}
static void zip()throws IOException{
FileOutputStream fos = new FileOutputStream("D:\\test.cer");
GZIPOutputStream gos = new GZIPOutputStream(fos);
// ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i=0; i<1000;i++) {
String str=i+" city,street" + i+"\n";
gos.write(str.getBytes());
}
gos.close();
fos.close();
System.out.println("Write completed!");
}
static void unzip()throws IOException{
FileInputStream fis = new FileInputStream("D:\\test.cer");
GZIPInputStream gis = new GZIPInputStream(fis);
BufferedReader br=new BufferedReader(new InputStreamReader(gis));
String line=null;
do{
line=br.readLine();
System.out.println(line);
}while(line!=null);
gis.close();
fis.close();
System.out.println("Write completed!");
}
}
上一篇: 软引用SoftReference的使用
下一篇: HttpsURLConnection