压缩文件
GZIPfile.java
}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[]){ } }
压缩文件夹
GZIPFolder.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class GZIPFolder { static final int BUFFER = 2048; /** * * <pre> * 方法名称: * 功能描述:压缩文件/文件夹 * 引用描述: * 参数:f文件 * 返回: * </pre> */ public boolean put(File f, ZipOutputStream out,String dir) { boolean result=false; if(f.isDirectory()){ File[] files=f.listFiles(); dir=(dir.length()== 0 ? "" : dir + "/" )+ f.getName(); for(int i=0;i<files.length;i++){ result=put(files[i],out,dir); //如果失败,返回失败信息 if(!result){ return result; } } }else{ byte[] data=new byte[BUFFER]; FileInputStream fi=null; BufferedInputStream temp=null; try { fi = new FileInputStream(f); temp=new BufferedInputStream (fi,BUFFER); dir =( dir.length() == 0 ? "" : dir + "/" )+ f.getName(); //System.out.print(dir); ZipEntry entry = new ZipEntry(dir); out.putNextEntry(entry); int count; while((count=temp.read(data, 0, BUFFER)) != -1){ out.write(data, 0, count); } out.flush(); result=true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { temp.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * * <pre> * 方法名称: * 功能描述:压缩文件/文件夹 * 引用描述: * 参数:fileName:文件路径 outfileName:输出路径 * 返回: * </pre> */ public boolean compression(String fileName,String OutfileName){ boolean result=false; File f=new File(fileName); ZipOutputStream zip = null; try { zip = new ZipOutputStream(new FileOutputStream(OutfileName)); result=put(f,zip,""); } catch (FileNotFoundException e) { e.printStackTrace(); } finally{ try { zip.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } public static void main(String[] args){ String fileName="F://chengmeng//jsTest//images"; String outFileName="F://chengmeng//jsTest.zip"; new GZIPFolder().compression(fileName,outFileName); File f=new File(fileName); System.out.println(f.getAbsolutePath()); } }