`
peng_jian_ming
  • 浏览: 255223 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Java压缩与解压缩文件

    博客分类:
  • Java
阅读更多
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[]){
 
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics