当前位置:   article > 正文

Apache commons-compress ZIP打包

Apache commons-compress ZIP打包
  1. ZipArchiveOutputStream zipOutput = null;
  2. try {
  3. String folderPath = "d:\\测试文件夹";
  4. File zipFile = new File("d:\\demo.zip");
  5. zipOutput = (ZipArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, new FileOutputStream(zipFile));
  6. zipOutput.setEncoding("UTF-8");
  7. zipOutput.setUseZip64(Zip64Mode.AsNeeded);
  8. File[] files = new File(folderPath).listFiles();
  9. for(File file : files){
  10. InputStream in = null;
  11. try {
  12. in = new FileInputStream(file);
  13. ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());//zipOutput.createArchiveEntry(logFile, logFile.getName());
  14. zipOutput.putArchiveEntry(entry);
  15. IOUtils.copy(in, zipOutput);
  16. zipOutput.closeArchiveEntry();
  17. }finally{
  18. if(in != null){
  19. try {
  20. in.close();
  21. } catch (Exception e) { }
  22. }
  23. }
  24. }
  25. zipOutput.finish();
  26. zipOutput.close();
  27. }catch(Exception e){
  28. if(zipOutput != null){
  29. try {
  30. zipOutput.close();
  31. } catch (Exception e1) { }
  32. }
  33. throw e;
  34. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/58552
推荐阅读
相关标签
  

闽ICP备14008679号