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

java如何解壓zip壓縮包

 更新時(shí)間:2025年07月03日 16:39:18   作者:yuhuofei2021  
這篇文章主要介紹了java如何解壓zip壓縮包問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java解壓zip壓縮包

坐在旁邊的小伙伴問我怎么用 java 將服務(wù)器上的壓縮文件解壓出來,我索性給他寫了個(gè) demo ,也順手記錄一下。

實(shí)例代碼

package com.yuhuofei.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @Description
 * @ClassName UnzipUtils
 * @Author yuhuofei
 * @Date 2022/8/10 21:03
 * @Version 1.0
 */
public class UnzipUtils {
    /**
     * 解壓zip壓縮文件到指定目錄
     *
     * @param zipPath zip壓縮文件絕對路徑
     * @param descDir 指定的解壓目錄
     */
    public static void unzipFile(String zipPath, String descDir) throws IOException {
        try {
            File zipFile = new File(zipPath);
            if (!zipFile.exists()) {
                throw new IOException("要解壓的壓縮文件不存在");
            }
            File pathFile = new File(descDir);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            InputStream input = new FileInputStream(zipPath);
            unzipWithStream(input, descDir);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    /**
     * 解壓
     *
     * @param inputStream
     * @param descDir
     */
    public static void unzipWithStream(InputStream inputStream, String descDir) {
        if (!descDir.endsWith(File.separator)) {
            descDir = descDir + File.separator;
        }
        try (ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("GBK"))) {
            ZipEntry zipEntry;
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                String zipEntryNameStr = zipEntry.getName();
                String zipEntryName = zipEntryNameStr;
                if (zipEntryNameStr.contains("/")) {
                    String str1 = zipEntryNameStr.substring(0, zipEntryNameStr.indexOf("/"));
                    zipEntryName = zipEntryNameStr.substring(str1.length() + 1);
                }
                String outPath = (descDir + zipEntryName).replace("\\\\", "/");
                File outFile = new File(outPath.substring(0, outPath.lastIndexOf('/')));
                if (!outFile.exists()) {
                    outFile.mkdirs();
                }
                if (new File(outPath).isDirectory()) {
                    continue;
                }
                writeFile(outPath, zipInputStream);
                zipInputStream.closeEntry();
            }
            System.out.println("======解壓成功=======");
        } catch (IOException e) {
            System.out.println("壓縮包處理異常,異常信息{}" + e);
        }
    }

    //將流寫到文件中
    public static void writeFile(String filePath, ZipInputStream zipInputStream) {
        try (OutputStream outputStream = new FileOutputStream(filePath)) {
            byte[] bytes = new byte[4096];
            int len;
            while ((len = zipInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        } catch (IOException ex) {
            System.out.println("解壓文件時(shí),寫出到文件出錯(cuò)");
        }
    }
	
	//測試方法
    public static void main(String[] args) throws IOException {

        String zipPath = "D:/test/測試文件.zip";
        String descDir = "D:/test/解壓/";

        unzipFile(zipPath, descDir);
    }
}

結(jié)果如下

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

赤峰市| 罗甸县| 正阳县| 浙江省| 富蕴县| 南陵县| 景德镇市| 凤台县| 南安市| 乌兰察布市| 平遥县| 龙游县| 体育| 泽普县| 广汉市| 宿州市| 巴里| 内黄县| 葫芦岛市| 临朐县| 南宫市| 桂东县| 深水埗区| 伊金霍洛旗| 清原| 厦门市| 蓝山县| 含山县| 丹江口市| 武川县| 肇东市| 随州市| 安徽省| 高青县| 蒲城县| 迁西县| 托里县| 广西| 江城| 上虞市| 和林格尔县|