最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Java原生壓縮組件不支持中文文件名亂碼的問(wèn)題

 更新時(shí)間:2017年03月03日 10:21:30   作者:楚驤  
本篇文章主要介紹了解決Java原生壓縮組件不支持中文文件名亂碼的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

最近發(fā)現(xiàn)Java原生的Zip壓縮組件在壓縮過(guò)程中,不支持文件名的中文編碼,會(huì)在壓縮過(guò)程中把中文文件名變成亂碼。Apache的ant包中的壓縮組件修復(fù)了這個(gè)問(wèn)題,如果你在使用壓縮功能時(shí)需要支持中文文件名,建議你直接使用Apache的壓縮組件來(lái)實(shí)現(xiàn)這個(gè)功能。

具體使用方法:

1.在你的pom文件中增加對(duì)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.壓縮的方法實(shí)現(xiàn),大家注意,本組件支持設(shè)置編碼(setEncoding("GBK")方法),解決了中文編碼的問(wèn)題:

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);
    }
  }

  /** 壓縮一個(gè)目錄 */
  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() + "/");
    }
  }

  /** 壓縮一個(gè)文件 */
  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.解壓縮的實(shí)現(xiàn):

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("\\*", "/");;
        //判斷路徑是否存在,不存在則創(chuàng)建文件路徑
        File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
        if(!file.exists()){
          file.mkdirs();
        }
        //判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓
        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();
        }
      }
    }
  }

以上代碼經(jīng)過(guò)測(cè)試,可以直接使用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實(shí)現(xiàn)計(jì)算器功能

    java實(shí)現(xiàn)計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • SpringMVC注解@CrossOrigin跨域問(wèn)題詳解

    SpringMVC注解@CrossOrigin跨域問(wèn)題詳解

    這篇文章主要介紹了SpringMVC注解@CrossOrigin跨域問(wèn)題詳解,跨域是瀏覽同源策略的造成,是瀏覽器對(duì)JavaScript施加的安全限制CORS是一種可以解決跨域問(wèn)題的技術(shù),需要的朋友可以參考下
    2023-11-11
  • SpringMVC項(xiàng)目訪問(wèn)controller時(shí)候報(bào)404的解決

    SpringMVC項(xiàng)目訪問(wèn)controller時(shí)候報(bào)404的解決

    這篇文章主要介紹了SpringMVC項(xiàng)目訪問(wèn)controller時(shí)候報(bào)404的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • mybatis in查詢傳入String方式

    mybatis in查詢傳入String方式

    這篇文章主要介紹了mybatis in查詢傳入String方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • JVM完全解讀之Metaspace解密源碼分析

    JVM完全解讀之Metaspace解密源碼分析

    通過(guò)這篇文章,你將可以了解到,為什么會(huì)有metaspace?metaspace的組成,metaspace的VM參數(shù),jstat里我們應(yīng)該關(guān)注metaspace的哪些值,有需要的朋友可以借鑒參考下
    2022-01-01
  • idea如何自動(dòng)生成serialVersionUID

    idea如何自動(dòng)生成serialVersionUID

    這篇文章主要介紹了idea如何自動(dòng)生成serialVersionUID,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java中Mono類的一些基本方法和示例代碼

    Java中Mono類的一些基本方法和示例代碼

    在Java編程中,我們經(jīng)常會(huì)遇到需要?jiǎng)?chuàng)建單例(singleton)對(duì)象的情況,單例模式是一種常見(jiàn)的設(shè)計(jì)模式,它保證一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問(wèn)點(diǎn),這篇文章主要給大家介紹了關(guān)于Java中Mono類的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • Spring Boot/Angular整合Keycloak實(shí)現(xiàn)單點(diǎn)登錄功能

    Spring Boot/Angular整合Keycloak實(shí)現(xiàn)單點(diǎn)登錄功能

    Keycloak新的發(fā)行版命名為Quarkus,專為GraalVM和OpenJDK HotSpot量身定制的一個(gè)Kurbernetes Native Java框架,計(jì)劃2019年底正式發(fā)布。這篇文章主要介紹了Spring Boot/Angular整合Keycloak實(shí)現(xiàn)單點(diǎn)登錄,需要的朋友可以參考下
    2019-10-10
  • MyBatis自定義SQL攔截器示例詳解

    MyBatis自定義SQL攔截器示例詳解

    Mybatis支持對(duì)Executor、StatementHandler、PameterHandler和ResultSetHandler 接口進(jìn)行攔截,也就是說(shuō)會(huì)對(duì)這4種對(duì)象進(jìn)行代理,下面這篇文章主要給大家介紹了關(guān)于MyBatis自定義SQL攔截器的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • Spring Boot實(shí)現(xiàn)簡(jiǎn)單的增刪改查

    Spring Boot實(shí)現(xiàn)簡(jiǎn)單的增刪改查

    這篇文章主要介紹了Spring Boot如何實(shí)現(xiàn)簡(jiǎn)單的增刪改查,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論

顺平县| 星座| 顺昌县| 诸暨市| 珲春市| 大竹县| 双流县| 洛宁县| 安福县| 定兴县| 遵化市| 秀山| 竹北市| 安宁市| 土默特右旗| 汝南县| 东平县| 顺平县| 玉环县| 齐河县| 突泉县| 大姚县| 枞阳县| 洪江市| 锡林郭勒盟| 内乡县| 乌苏市| 长兴县| 漳平市| 建宁县| 墨脱县| 朝阳市| 运城市| 武陟县| 吴旗县| 怀柔区| 涡阳县| 巴彦淖尔市| 唐河县| 阿拉善左旗| 嘉祥县|