使用Java代码压缩文件或文件夹
2014-04-17 14:35
771 查看
/**<P>文件注释:ReportUtils.java</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午12:06:31</p> * <p>类型:文件-ReportUtils.java</p> * <p>用途:该文件用于</p> * <p>备注:***</p> */ package report.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; /**<P>类型注释:</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午12:06:31</p> * <p>类型:ReportUtils</p> * <p>用途:该类型用于</p> * <p>备注:***</p> */ public class ReportUtils { /** * <P>函数注释:zip()</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午2:38:16</p> * <p>类型:方法</p> * <p>用途:压缩文件或者文件夹</p> * <p>备注:***</p> */ public static File zip(File srcFile){ File destFile = null; if(srcFile != null){ destFile = new File(srcFile.getParent()+"\\"+srcFile.getName().split("\\.")[0]+".zip"); if(srcFile.isDirectory()){ zipFolder(srcFile,destFile); }else{ zipFile(srcFile,destFile); } } return destFile; } /** * <P>函数注释:zipProcess()</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午2:38:36</p> * <p>类型:方法</p> * <p>用途:该方法用于压缩文件夹的递归处理</p> * <p>备注:***</p> */ public static void zipProcess(ZipOutputStream out, File file, String base) throws Exception { if (file.isDirectory()) { File[] fl = file.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zipProcess(out, fl[i], base + fl[i].getName()); } }else { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(file); int b; while ( (b = in.read()) != -1) { out.write(b); } in.close(); } } /** * <P>函数注释:zipFoler()</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午2:06:20</p> * <p>类型:方法</p> * <p>用途:该方法用于压缩文件夹</p> * <p>备注:***</p> */ public static void zipFolder(File srcFile, File destFile){ ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(destFile)); out.setLevel(9); out.setEncoding("GBK");// 指定编码为gbk,否则部署到linux下会出现乱码 zipProcess(out, srcFile, ""); }catch (Exception e) { e.printStackTrace(); }finally{ try { if(out != null){ out.close() ; } } catch (IOException e) { e.printStackTrace(); } } } /** * <P>函数注释:zipFile()</p> * <p>作者:何伟坡</p> * <p>时间:2014年4月17日-下午2:03:41</p> * <p>类型:方法</p> * <p>用途:该方法用于压缩文件</p> * <p>备注:***</p> */ public static void zipFile(File srcFile, File destFile){ ZipOutputStream out = null; FileInputStream in = null; try { out = new ZipOutputStream(new FileOutputStream(destFile)); out.setLevel(9); out.setEncoding("GBK");// 指定编码为gbk,否则部署到linux下会出现乱码 in = new FileInputStream(srcFile); out.putNextEntry(new ZipEntry(srcFile.getName())); int length; byte[] buffer = new byte[1024]; while ((length = in.read(buffer,0,1024)) != -1){ out.write(buffer, 0, length); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } if(out != null){ out.close() ; } } catch (IOException e) { e.printStackTrace(); } } } }
相关文章推荐
- Java中使用Ant压缩指定文件夹或文件
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用Java对文件或文件夹的压缩, 解压, 加密和解密
- 使用java代码在本地创建文件夹(多层目录),在本地磁盘创建文件目录
- Java代码压缩文件夹及多文件压缩---简单案例
- Java压缩文件工具类ZipUtil使用方法代码示例
- 使用java对文件或文件夹进行压缩和加密
- 使用Java zip包和apach 的ant包对文件、文件夹进行压缩,兼容中文
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用ant批量将corba的idl文件生成java代码
- Java实现将文件或者文件夹压缩成zip
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- Java实现多文件压缩和解压缩代码(未测试)
- 170814、Java使用gzip压缩文件、还原文件
- 使用ZipArchive压缩打包想要的文件夹或文件