在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java生成压缩文件

2017-2-7 13:39| 发布者: zhangjf| 查看: 446| 评论: 0

摘要: 在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码: import java.io.BufferedOutputStream; import java.io.File; i ...

在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码:

  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import org.apache.tools.zip.ZipEntry;
  6. import org.apache.tools.zip.ZipOutputStream;
  7. /**
  8. * @project: Test
  9. * @author chenssy
  10. * @date 2013-7-28
  11. * @Description: 文件压缩工具类
  12. * 将指定文件/文件夹压缩成zip、rar压缩文件
  13. */
  14. public class CompressedFileUtil {
  15. /**
  16. * 默认构造函数
  17. */
  18. public CompressedFileUtil(){
  19. }
  20. /**
  21. * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
  22. * @param resourePath 源文件/文件夹
  23. * @param targetPath 目的压缩文件保存路径
  24. * @return void
  25. * @throws Exception
  26. */
  27. public void compressedFile(String resourcesPath,String targetPath) throws Exception{
  28. File resourcesFile = new File(resourcesPath); //源文件
  29. File targetFile = new File(targetPath); //目的
  30. //如果目的路径不存在,则新建
  31. if(!targetFile.exists()){
  32. targetFile.mkdirs();
  33. }
  34. String targetName = resourcesFile.getName()+".zip"; //目的压缩文件名
  35. FileOutputStream outputStream = new FileOutputStream(targetPath+"\"+targetName);
  36. ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
  37. createCompressedFile(out, resourcesFile, "");
  38. out.close();
  39. }
  40. /**
  41. * @desc 生成压缩文件。
  42. * 如果是文件夹,则使用递归,进行文件遍历、压缩
  43. * 如果是文件,直接压缩
  44. * @param out 输出流
  45. * @param file 目标文件
  46. * @return void
  47. * @throws Exception
  48. */
  49. public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
  50. //如果当前的是文件夹,则进行进一步处理
  51. if(file.isDirectory()){
  52. //得到文件列表信息
  53. File[] files = file.listFiles();
  54. //将文件夹添加到下一级打包目录
  55. out.putNextEntry(new ZipEntry(dir+"/"));
  56. dir = dir.length() == 0 ? "" : dir +"/";
  57. //循环将文件夹中的文件打包
  58. for(int i = 0 ; i < files.length ; i++){
  59. createCompressedFile(out, files[i], dir + files[i].getName()); //递归处理
  60. }
  61. }
  62. else{ //当前的是文件,打包处理
  63. //文件输入流
  64. FileInputStream fis = new FileInputStream(file);
  65. out.putNextEntry(new ZipEntry(dir));
  66. //进行写操作
  67. int j = 0;
  68. byte[] buffer = new byte[1024];
  69. while((j = fis.read(buffer)) > 0){
  70. out.write(buffer,0,j);
  71. }
  72. //关闭输入流
  73. fis.close();
  74. }
  75. }
  76. public static void main(String[] args){
  77. CompressedFileUtil compressedFileUtil = new CompressedFileUtil();
  78. try {
  79. compressedFileUtil.compressedFile("G:\zip", "F:\zip");
  80. System.out.println("压缩文件已经生成...");
  81. } catch (Exception e) {
  82. System.out.println("压缩文件生成失败...");
  83. e.printStackTrace();
  84. }
  85. }
  86. }
复制代码

运行程序结果如下:

压缩之前的文件目录结构:



提示:如果是使用java.util下的java.util.zip进行打包处理,可能会出现中文乱码问题,这是因为java的zip方法不支持编码格式的更改,我们可以使用ant.java下的zip工具类来进行打包处理。所以需要将ant.jar导入项目的lib目录下。

来自: http://blog.csdn.net/chenssy/article/details/9622171

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 01:50

Copyright 2015-2025 djqfx

返回顶部