欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中的GZIP压缩学习

程序员文章站 2024-02-29 09:54:46
...
今天由于公司的需要对java中的GZIP压缩功能学习,主要实现代码如下:

public static int write(String fileName,String gizpName) {
String charset = "UTF-8" ;
try {
FileInputStream fileInputStream = new FileInputStream("d:/a.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream,charset));
GZIPOutputStream gzipOutPutStream = new GZIPOutputStream(new FileOutputStream("d:/b.gzip"));
String a;
while((a=bufferedReader.readLine())!=null){
gzipOutPutStream.write(a.getBytes());
}
gzipOutPutStream.close();
bufferedReader.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}

public static int read(){
FileInputStream fileInputStream;
try {
String charset = "UTF-8" ;
fileInputStream = new FileInputStream("d:/b.gzip");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream,charset));
FileOutputStream fout = new FileOutputStream("d:/test2.txt");
String a;
while((a=bufferedReader.readLine())!=null){
System.out.println(a);
fout.write(a.getBytes());
}
fout.close();
bufferedReader.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1;
}

目前存在的问题为,写入到gz文件中的信息是没有问题的,但是读取的时候却会出现乱码的问题,需要进一步研究。
相关标签: java gzip