Java多文件以ZIP压缩包导出的实现方法

 更新时间:2018年07月14日 13:14:39   作者:白枫J  
这篇文章主要为大家详细介绍了Java多文件以ZIP压缩包导出的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java多文件以ZIP压缩包导出的具体代码,供大家参考,具体内容如下

1、使用java实现吧服务器的图片打包成一个zip格式的压缩包导出,多个文件打包导出。
2、代码如下:

**ImageByteUtil.java**

public class ImageByteUtil{
  private static float QUALITY = 0.6f;
  public static void compressZip(List<File> listfiles, OutputStream output,String encode, boolean compress,String alias){
  ZipOutputStream zipStream = null;
  try {
      zipStream = new ZipOutputStream(output);
      for (File file : listfiles){
        compressZip(file, zipStream, compress,alias+"_"+(listfiles.indexOf(file)+1));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try { 
        if (zipStream != null) { 
          zipStream.close(); 
        } 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }
    }
  }

private static void compressZip(File file, ZipOutputStream zipStream, 
      boolean compress,String alias) throws Exception{
    FileInputStream input = null;
    try {
      input = new FileInputStream(file); 
      //zip(input, zipStream, file.getName(), compress); 
      zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1), compress);
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try { 
        if(input != null) 
          input.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }
    }
  }

private static void zip(InputStream input, ZipOutputStream zipStream, 
      String zipEntryName, boolean compress) throws Exception{
      byte[] bytes = null; 
    BufferedInputStream bufferStream = null; 
    try { 
      if(input == null) 
        throw new Exception("获取压缩的数据项失败! 数据项名为:" + zipEntryName); 
      // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样 
      ZipEntry zipEntry = new ZipEntry("图片/"+zipEntryName);
      // 定位到该压缩条目位置,开始写入文件到压缩包中 
      zipStream.putNextEntry(zipEntry);
      if (compress) { 
        bytes = ImageByteUtil.compressOfQuality(input, 0); 
        zipStream.write(bytes, 0, bytes.length); 
      } else {
        bytes = new byte[1024 * 5];// 读写缓冲区 
        bufferStream = new BufferedInputStream(input);// 输入缓冲流 
        int read = 0; 
        while ((read = bufferStream.read(bytes)) != -1) {
          zipStream.write(bytes, 0, read); 
        } 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (null != bufferStream) 
          bufferStream.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    }
  }

  public static byte[] compressOfQuality(File file, float quality) throws Exception{
    byte[] bs = null; 
    InputStream input = null; 
    try { 
      input = new FileInputStream(file); 
      bs = compressOfQuality(input,quality);
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (input != null) 
          input.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return bs;
  }

  public static byte[] compressOfQuality(InputStream input, float quality) 
      throws Exception {
      ByteArrayOutputStream output = null; 
    try { 
      output = new ByteArrayOutputStream(); 
      if(quality == 0){ 
        Thumbnails.of(input).scale(1f).outputQuality(QUALITY) 
        .toOutputStream(output); 
      } else { 
        Thumbnails.of(input).scale(1f).outputQuality(quality).toOutputStream(output); 
      } 
      return output.toByteArray(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (output != null) 
          output.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return null;
  }
}

**Main.java**

public static void main(String[] args){
  //要导出的文件集合,添加自己需要导出的文件
  List<File> ListFiles = new ArrayList<>();
  //调用工具类,参数说明(需要导出的文件集,ByteArrayOutputStream对象,编码,是否压缩【true,false】,文件名称前缀)
  ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ");
  //指定导出格式
  ReturnContext.addParam("exportFileName","extFile.zip");
  ReturnContext.addParam("mimeType", "zip");
  return in;
}

3、此功能是根据在开发过程中项目需要实现的,测试可正常使用,可更改定制。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 关于synchronized、volatile、ReentrantLock的区别与对比

    关于synchronized、volatile、ReentrantLock的区别与对比

    这篇文章主要介绍了关于synchronized、volatile、ReentrantLock的区别与对比,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • spring BeanProcessor接口详解

    spring BeanProcessor接口详解

    这篇文章主要介绍了spring BeanProcessor接口的相关资料,帮助大家更好的理解和学习使用spring,感兴趣的朋友可以了解下
    2021-03-03
  • Java设计模式之单例模式实例分析

    Java设计模式之单例模式实例分析

    这篇文章主要介绍了Java设计模式之单例模式,以实例形式较为详细的分析了单例模式的概念、定义及简单实现技巧,需要的朋友可以参考下
    2015-11-11
  • AntDesign多环境配置启动过程详解

    AntDesign多环境配置启动过程详解

    这篇文章主要为大家介绍了AntDesign多环境配置启动过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • mybatis中使用not in与 in的写法说明

    mybatis中使用not in与 in的写法说明

    这篇文章主要介绍了mybatis中使用not in与 in的写法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java使用easyExcel实现导入功能

    Java使用easyExcel实现导入功能

    这篇文章介绍了Java使用easyExcel实现导入功能的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • 详解Java内存管理中的JVM垃圾回收

    详解Java内存管理中的JVM垃圾回收

    这篇文章给大家分享了关于Java内存管理中的JVM垃圾回收的相关知识点内容,有兴趣的朋友们可以学习参考下。
    2018-08-08
  • 关于线程池创建、执行、销毁的原理及分析

    关于线程池创建、执行、销毁的原理及分析

    这篇文章主要介绍了关于线程池创建、执行、销毁的原理及分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • Java实现直接插入排序和折半插入排序算法示例

    Java实现直接插入排序和折半插入排序算法示例

    这篇文章主要介绍了Java实现直接插入排序和折半插入排序算法示例,文中对算法的思想和时间复杂度都有简单的讲解,需要的朋友可以参考下
    2016-04-04
  • flink进阶富函数生命周期介绍

    flink进阶富函数生命周期介绍

    这篇文章主要为大家介绍了flink进阶富函数生命周期的举例介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03

最新评论