java的gz压缩

  • Post author:
  • Post category:java


场景:将某个文件夹内的txt文件都转为txt.gz文件

1、遍历文件夹内的txt文件

File folder=new File(folderPath)

for (File file : folder.listFiles())

2、用io流转化

输入流

File txtFile=new File(txtFilePath)

FileInputStream fis=new FileInputStream(txtFile);

BufferedInputStream bis=new BufferedInputStream(fis);

输出流

File txtGzFile=new File(txtGzFilePath);

FileOutputStream fos=new FileOutputStream(txtGzFile);

BufferedOutputStream bos=new BufferedOutputStream(fos);

GZIPOutputStream gzos = new GZIPOutputStream(bos);

int len=0;

byte[] buffer=new byte[1024];

while ((len = bis.read(buffer)) != -1) {


gzos.write(buffer, 0, len);

}

gzos.flush();

gzos.close();

bis.close();



版权声明:本文为hu_mingwei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。