当前位置: 代码网 > it编程>编程语言>Java > java如何解压zip压缩包

java如何解压zip压缩包

2025年07月03日 Java 我要评论
java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,我索性给他写了个 demo ,也顺手记录一下。实例代码package com.yuhuofei.utils;

java解压zip压缩包

坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,我索性给他写了个 demo ,也顺手记录一下。

实例代码

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("解压文件时,写出到文件出错");
        }
    }
	
	//测试方法
    public static void main(string[] args) throws ioexception {

        string zippath = "d:/test/测试文件.zip";
        string descdir = "d:/test/解压/";

        unzipfile(zippath, descdir);
    }
}

结果如下

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com