解决Java原生压缩组件不支持中文文件名乱码的问题

所属分类: 软件编程 / java 阅读数: 19
收藏 0 赞 0 分享

最近发现Java原生的Zip压缩组件在压缩过程中,不支持文件名的中文编码,会在压缩过程中把中文文件名变成乱码。Apache的ant包中的压缩组件修复了这个问题,如果你在使用压缩功能时需要支持中文文件名,建议你直接使用Apache的压缩组件来实现这个功能。

具体使用方法:

1.在你的pom文件中增加对Apache的ant工具包的dependency:

    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.9.3</version>
    </dependency>

并在头部引用用到的类:

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

2.压缩的方法实现,大家注意,本组件支持设置编码(setEncoding("GBK")方法),解决了中文编码的问题:

public static void compress(String srcPath , String dstPath) throws IOException{
    File srcFile = new File(srcPath);
    File dstFile = new File(dstPath);
    if (!srcFile.exists()) {
      throw new FileNotFoundException(srcPath + "does not exists");
    }

    FileOutputStream out = null;
    ZipOutputStream zipOut = null;
    try {
      out = new FileOutputStream(dstFile);
      zipOut = new ZipOutputStream(new BufferedOutputStream(out));
      zipOut.setEncoding("GBK");
      String baseDir = "";
      compress(srcFile, zipOut, baseDir);
    }
    catch (Throwable ex){
      throw new RuntimeException(ex);
    }
    finally {
      if(null != zipOut){
        zipOut.close();
        out = null;
      }

      if(null != out){
        out.close();
      }
    }
  }


private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
    if (file.isDirectory()) {
      compressDirectory(file, zipOut, baseDir);
    } else {
      compressFile(file, zipOut, baseDir);
    }
  }

  /** 压缩一个目录 */
  private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{
    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      compress(files[i], zipOut, baseDir + dir.getName() + "/");
    }
  }

  /** 压缩一个文件 */
  private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
    if (!file.exists()){
      return;
    }

    BufferedInputStream bis = null;
    try {
      bis = new BufferedInputStream(new FileInputStream(file));
      ZipEntry entry = new ZipEntry(baseDir + file.getName());
      zipOut.putNextEntry(entry);
      int count;
      byte data[] = new byte[BUFFER];
      while ((count = bis.read(data, 0, BUFFER)) != -1) {
        zipOut.write(data, 0, count);
      }

    }finally {
      if(null != bis){
        bis.close();
      }
    }
  }

3.解压缩的实现:

public static void decompress(String zipFile , String dstPath)throws IOException{
    File pathFile = new File(dstPath);
    if(!pathFile.exists()){
      pathFile.mkdirs();
    }
    ZipFile zip = new ZipFile(zipFile);
    for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){
      ZipEntry entry = (ZipEntry)entries.nextElement();
      String zipEntryName = entry.getName();
      InputStream in = null;
      OutputStream out = null;
      try{
        in = zip.getInputStream(entry);
        String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");;
        //判断路径是否存在,不存在则创建文件路径
        File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
        if(!file.exists()){
          file.mkdirs();
        }
        //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
        if(new File(outPath).isDirectory()){
          continue;
        }

        out = new FileOutputStream(outPath);
        byte[] buf1 = new byte[1024];
        int len;
        while((len=in.read(buf1))>0){
          out.write(buf1,0,len);
        }
      }
      finally {
        if(null != in){
          in.close();
        }

        if(null != out){
          out.close();
        }
      }
    }
  }

以上代码经过测试,可以直接使用。

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

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多