ZipUtils.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.huimv.manage.util;
  2. import java.io.*;
  3. import java.util.List;
  4. import java.util.zip.ZipEntry;
  5. import java.util.zip.ZipOutputStream;
  6. /**
  7. * @Project : huimv.shiwan
  8. * @Package : com.huimv.biosafety.uface.controller
  9. * @Description : TODO
  10. * @Version : 1.0
  11. * @Author : ZhuoNing
  12. * @Create : 2020-12-25
  13. **/
  14. public class ZipUtils {
  15. public static void downloadZip(OutputStream outputStream, File file){
  16. BufferedInputStream bufferedInputStream = null;
  17. ZipOutputStream zipOutputStream = null;
  18. try {
  19. zipOutputStream = new ZipOutputStream(outputStream);
  20. // for (File file : fileList) {
  21. ZipEntry zipEntry = new ZipEntry(file.getName());
  22. zipOutputStream.putNextEntry(zipEntry);
  23. byte[] buf = new byte[1024];
  24. int len;
  25. FileInputStream in = new FileInputStream(file);
  26. while ((len = in.read(buf)) != -1) {
  27. zipOutputStream.write(buf, 0, len);
  28. zipOutputStream.flush();
  29. }
  30. // }
  31. zipOutputStream.flush();
  32. zipOutputStream.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. } finally {
  36. // 关闭流
  37. try {
  38. if (bufferedInputStream != null) {
  39. bufferedInputStream.close();
  40. }
  41. if (zipOutputStream != null ) {
  42. zipOutputStream.close();
  43. }
  44. if (outputStream != null) {
  45. outputStream.close();
  46. }
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }