自动下载网页上的zip文件并自动解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    /**
     * 给定一个zip的url,实现zip的下载.下载到目的文件夹
     * id为用户给定的id,方便维护
     * <p>
     * 返回zip文件的最终文件路径
     */
    public static String downloadZIP(String[] UrlAndZipName, String fileDisDir) throws Exception {
        String strUrl = UrlAndZipName[0];
        String fileName = UrlAndZipName[1];
 
        URL url = new URL(strUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
        conn.connect();
        InputStream inStream = conn.getInputStream();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buf)) != -1) {
            outStream.write(buf, 0, len);
        }
        inStream.close();
        outStream.close();
 
        String zippath = fileDisDir + File.separator + fileName + ".zip";
        File file = new File(zippath);
        FileOutputStream op = new FileOutputStream(file);
        op.write(outStream.toByteArray());
        op.close();
 
        return zippath;
    }
 
 
    /**
     * 解压文件到指定目录
     * 解压后的文件名,和之前一致
     * <p>
     * 返回最终解压的文件夹路径
     */
    public static String unZipFiles( String zipFilePath, String descDir) throws IOException {
        File zipFile=new File(zipFilePath);
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));//解决中文文件夹乱码
        String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));    //此处的\\针对windows,在linux用 / ,推荐改成File.sepator
 
        File pathFile = new File(descDir+ File.separator+name);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
 
        for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir +File.separator+ name +File.separator+ zipEntryName).replaceAll("\\*", "/");
 
            // 判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if (!file.exists()) {
                file.mkdirs();
            }
            // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if (new File(outPath).isDirectory()) {
                continue;
            }
            // 输出文件路径信息
//          System.out.println(outPath);
 
            FileOutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            in.close();
            out.close();
        }
        System.out.println("******************解压完毕********************");
        return (descDir+ File.separator+name);
    }

  

posted @   _Meditation  阅读(1230)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示