欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何基于java實現(xiàn)解壓ZIP TAR等文件

 更新時間:2020年07月28日 15:41:15   作者:張志勇-  
這篇文章主要介紹了如何基于java實現(xiàn)解壓ZIP TAR等文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

  java實現(xiàn)對常用.ZIP , .TAR, .TAR.BZ2, .BZ2 ,.TAR.GZ ,.GZ格式文件的解壓。

  首先需要引入maven依賴,這里使用的是Apache的壓縮工具包common-compress,改工具包支持解壓、壓縮,此代碼中我列舉出一個zip的壓縮示例,其他格式的只需切換改格式對應(yīng)的流即可。

對于RAR格式文件的解壓,目前該工具包還不支持,希望大家做過的可以多多交流。

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-compress</artifactId>
  <version>1.19</version></dependency>
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;

/**
 * @author :zhangzhiyong
 * @description:  java實現(xiàn)常見文件格式的解壓與壓縮
 *         支持.ZIP .TAR .TAR.BZ2 .BZ2 .TAR.GZ .GZ
 *         其他格式compress包也支持,在此基礎(chǔ)上開發(fā)即可
 *         另外壓縮文件只寫了ZIP壓縮的方法zipCompression,其他的格式類似,換成對應(yīng)的文件流即可。
 *   暫不支持RAR壓縮格式,RAR可以用junrar的工具包,但是有缺陷:
 *   其一:如果壓縮文件中有中文名字的文件夾,解壓以后文件夾名字是亂碼,但是不影響文件夾里面的文件;
 *   其二:最新 WinRar 壓縮產(chǎn)生的 .rar 文件可能會無法解壓。
 *   缺陷原因:rar 有版權(quán),有些東西沒有公開,對解壓有一些限制,即使其他解壓包也可能有問題,但是建議嘗試。
 * @date :2020/7/1 20:44
 */
public class CompressionFileUtil {
  /**
   * @param filePath 需要解壓的zip文件的完成路徑。
   * @param unzipPath 解壓過后生成文件的存放路徑
   * @description: 對zip文件進行解壓。
   * @return: boolean
   * @author: ZZY
   * @time: 2020/7/2 14:47
   */
  public static boolean zipUnCompress(String filePath, String unzipPath) throws IOException {

    System.out.println("開始解壓ZIP..........");
    FileInputStream fis = null;
    ZipArchiveInputStream zis = null;
    try {
      File file = new File(filePath);
      fis = new FileInputStream(file);
      zis = new ZipArchiveInputStream(fis);
      ZipArchiveEntry nze = null;
      while ((nze = zis.getNextZipEntry()) != null) {
        FileOutputStream os = null;
        BufferedOutputStream bos = null;
        try {
          System.out.println("正在解壓....." + nze.getName());
          //自動添加File.separator文件路徑的分隔符,根據(jù)系統(tǒng)判斷是\\還是/
          String dir = unzipPath + File.separator + nze.getName(); //解壓全路徑
          System.out.println("dir---" + dir);
          File file1 = null;
          if (nze.isDirectory()) {
            file1 = new File(dir);
            file1.mkdirs();
          } else {
            file1 = new File(dir);
            os = new FileOutputStream(file1);
            bos = new BufferedOutputStream(os);
             /*byte [] bt = new byte[1024];
             int len = 0;
             while((len = zis.read(bt,0,1024)) != -1){
               bos.write(bt,0,len);
             }*/
            IOUtils.copy(zis, bos); //作用與上面注釋代碼一樣
          }
          System.out.println("解壓完成......");
        } catch (FileNotFoundException e) {
          e.printStackTrace();
          return false;
        } finally {
          if (bos != null) {
            bos.close();
          }
          if (os != null) {
            os.close();
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      if (zis != null) {
        zis.close();
      }
      if (fis != null) {
        fis.close();
      }
    }
    return true;
  }

  /**
   * @param filesPathArray 多個文件的絕對路徑,是一個數(shù)組。
   * @param zipFilePath  生成的壓縮文件的位置,包括生成的文件名,如D:\zip\test.zip
   * @description: 將多個文件壓縮成ZIP壓縮包。
   * @return: boolean
   * @author: ZZY
   * @time: 2020/7/2 14:42
   */
  public static boolean zipCompression(String[] filesPathArray, String zipFilePath) throws Exception {
    System.out.println("開始壓縮ZIP文件");
    ZipArchiveOutputStream zos = null;
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(new File(zipFilePath));
      zos = new ZipArchiveOutputStream(fos);
      for (String filePath : filesPathArray) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
          File file = new File(filePath);
          // 第二個參數(shù)如果是文件全路徑名,那么壓縮時也會將路徑文件夾也縮進去;
          // 我們只壓縮目標文件,而不壓縮該文件所處位置的相關(guān)文件夾,所以這里我們用file.getName()
          System.out.println("開始壓縮..." + file.getName());
          ZipArchiveEntry zae = new ZipArchiveEntry(file, file.getName());
          zos.putArchiveEntry(zae);
          fis = new FileInputStream(file);
          bis = new BufferedInputStream(fis);
          int count;
          byte[] bt = new byte[1024];
          while ((count = bis.read(bt, 0, 1024)) != -1) {
            zos.write(bt, 0, count);
          }
        } finally {
          zos.closeArchiveEntry();
          if (bis != null)
            bis.close();
          if (fis != null)
            fis.close();
        }
      }
    } finally {
      if (zos != null)
        zos.close();
      if (fos != null)
        fos.close();
    }
    System.out.println("壓縮完成......");
    return true;
  }

  /**
   * @param inputStream 每種TAR文件用不同的輸入流,unCompress方法中已注明
   * @param unTarPath  TAR文件解壓后的存放路徑
   * @description: 解壓TAR類文件,包括.TAR .TAR.BZ2 .TAR.GZ
   * @return: void
   * @author: ZZY
   * @time: 2020/7/2 17:42
   */
  public static void unTar(InputStream inputStream, String unTarPath) throws IOException {

    FileInputStream fis = null;
    TarArchiveInputStream tis = null;
    try {
      tis = new TarArchiveInputStream(inputStream);
      TarArchiveEntry nte = null;
      System.out.println("開始解壓......");
      while ((nte = tis.getNextTarEntry()) != null) {
        String dir = unTarPath + File.separator + nte.getName();
        System.out.println("正在解壓......" + dir);
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
          if (nte.isDirectory()) {
            File file1 = new File(dir);
            file1.mkdirs();
          } else {
            File file2 = new File(dir);
            fos = new FileOutputStream(file2);
            bos = new BufferedOutputStream(fos);
            IOUtils.copy(tis, bos);
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (bos != null) {
            bos.close();
          }
          if (fos != null) {
            fos.close();
          }
        }
      }
      System.out.println("解壓完成......");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (tis != null) {
        tis.close();
      }
      if (fis != null) {
        fis.close();
      }
    }
  }

  public static boolean unCompress(String filePath,String unCompressPath) throws Exception {
    String fileType = filePath.toUpperCase();
    if(fileType.endsWith(".TAR")){
      System.out.println("解壓的.TAR包");
      //.TAR包用一般的FileInputStream流讀取
      unTar(new FileInputStream(filePath),unCompressPath);
    }
    else if(fileType.endsWith(".TAR.GZ")){
      System.out.println("解壓的.TAR.GZ包");
      //.TAR.GZ包要用GzipCompressorInputStream讀取
      unTar(new GzipCompressorInputStream(new FileInputStream(filePath)),unCompressPath);
    }
    else if(fileType.endsWith(".TAR.BZ2")){
      System.out.println("解壓的.TAR.BZ2包");
      unTar(new BZip2CompressorInputStream(new FileInputStream(filePath)),unCompressPath);
    }
    else if(fileType.endsWith(".ZIP")){
      System.out.println("解壓的.ZIP包");
      zipUnCompress(filePath,unCompressPath);
    }
    else{
      System.out.println("暫不支持該種格式文件的解壓");
    }
    return true;
  }

  public static void main(String[] args) throws Exception {

    unCompress("D:\\test\\zip\\nginx-1.18.0.rar","D:\\test\\zip");

  }

}

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

相關(guān)文章

  • SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法

    SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法

    本篇文章主要介紹了SpringMVC+ZTree實現(xiàn)樹形菜單權(quán)限配置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Nacos注冊中心的幾種調(diào)用方式詳解

    Nacos注冊中心的幾種調(diào)用方式詳解

    Spring Cloud Alibaba Nacos 作為近幾年最熱門的注冊中心和配置中心,也被國內(nèi)無數(shù)公司所使用,本文就來看下 Nacos 作為注冊中心時,調(diào)用它的接口有幾種方式
    2023-10-10
  • java配置文件取值的多種方式總結(jié)

    java配置文件取值的多種方式總結(jié)

    這篇文章主要為大家詳細介紹了java配置文件取值的多種方式,包括一般項目,國際化項目,springboot項目,文中的示例代碼講解詳細,需要的可以參考下
    2023-11-11
  • Maven使用方法詳及方式詳細介紹

    Maven使用方法詳及方式詳細介紹

    使用maven倉庫的話需要從網(wǎng)上下載maven的包,比如“apache-maven-3.5.4-bin.tar”,下載完成之后解壓,在解壓的文件夾中的conf目錄下的settings.xml文件夾下就可以配置maven遠程倉庫和本地倉庫的地址
    2022-11-11
  • Java之SpringBoot集成ActiveMQ消息中間件案例講解

    Java之SpringBoot集成ActiveMQ消息中間件案例講解

    這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • springboot為異步任務(wù)規(guī)劃自定義線程池的實現(xiàn)

    springboot為異步任務(wù)規(guī)劃自定義線程池的實現(xiàn)

    本文主要介紹了springboot為異步任務(wù)規(guī)劃自定義線程池,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題

    IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題

    IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 一文詳解如何更改電腦使用的JDK版本

    一文詳解如何更改電腦使用的JDK版本

    我們在日常學(xué)習(xí)或者工作中,難免會遇到需要使用不同的jdk版本進行開發(fā),這篇文章主要給大家介紹了關(guān)于如何更改電腦使用的JDK版本的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java生成條形碼code128(親測有效)

    Java生成條形碼code128(親測有效)

    這篇文章主要介紹了Java生成條形碼code128,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Hibernate的一對一,一對多/多對一關(guān)聯(lián)保存的實現(xiàn)

    Hibernate的一對一,一對多/多對一關(guān)聯(lián)保存的實現(xiàn)

    本文主要介紹了Hibernate的一對一,一對多/多對一關(guān)聯(lián)保存的實現(xiàn),文中通過示例代碼介紹的很詳細,感興趣的可以了解一下
    2021-09-09

最新評論