Java压缩与解压缩文件2

package com.yc.ycportal.ge.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPfile {
private boolean flag = true;
//定义一个接口,通过结构来调用该类的方法
public static   GZIPfile getInterface(){
   return new GZIPfile();
}
//创建一个方法,对文件进行解压缩
public boolean openFile(String InfileName,String OutfileName){
   /**
    * @InfileName 传入方法的文件名称及文件所在路径的具体值
    * @OutfileName 对文件解压缩成功后,要将文件保存的具体位置和名称
    * @return 返回类型为boolean,标识文件是否正常操作完成
    */
   try {
    GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(InfileName));
    FileOutputStream out = new FileOutputStream(OutfileName);
    byte[] bt = new byte[1024];
    int length = 0;
    while((length=gzip.read(bt))>0){
     out.write(bt, 0, length);
    }
   } catch (Exception e) {
    this.flag = false;
    System.out.println(e.getMessage());
   }
   return flag;
}
//创建一个方法对读取到的文件进行压缩处理
public boolean compFile(String InfileName,String OutfileName){
   /**
    * @InfileName 读入文件的具体名称和地址的值,对文件的数据进行压缩处理
    * @OutfileName 读出的文件要存放的具体的地址和文件名称,处理后的压缩文件
    * @return 返回一个boolean值表明程序是否能够争取的处理
    */
   try {
    GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(OutfileName));
    FileInputStream in = new FileInputStream(InfileName);
    byte[] bt = new byte[1024];
    int length = 0;
    while((length=in.read(bt))>0){
     gzip.write(bt, 0, length);
    }
   } catch (Exception e) {
    flag = false;
    System.out.println(e.getMessage());
   }
   return flag;
}
public static void main(String args[]){
  
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值