java zip解压缩

news/2024/7/6 1:59:21 标签: zip, ocp, java

java 实现解压zip

javascript">// An highlighted block
<!--		ZIP/RAR压缩文件解压-->
		<!-- https://mvnrepository.com/artifact/ant/ant -->
		<dependency>
			<groupId>ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.6.5</version>
		</dependency>

package org.hwdz.common.util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author : Cui mc
 * @date : 2021-5-25 15:17:20
 * @param  //ZIP/RAR压缩文件解压
 */
public class DecompressingUtils {

    public static void zipUncompress(String inputFile,String uncompress) throws Exception {
        File srcFile = new File(inputFile);
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            throw new Exception(srcFile.getPath() + "所指文件不存在");
        }
        String destDirPath = inputFile.replace(".zip", "");
        //创建压缩文件对象
        ZipFile zipFile = new ZipFile(srcFile);
        //开始解压
        Enumeration<?> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            // 如果是文件夹,就创建个文件夹
            if (entry.isDirectory()) {
                srcFile.mkdirs();
            } else {
                // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                File targetFile = new File(uncompress + "/" + entry.getName());
                // 保证这个文件的父文件夹必须要存在
                if (!targetFile.getParentFile().exists()) {
                    targetFile.getParentFile().mkdirs();
                }
                targetFile.createNewFile();
                // 将压缩文件内容写入到这个文件中
                InputStream is = zipFile.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(targetFile);
                int len;
                byte[] buf = new byte[1024];
                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                // 关流顺序,先打开的后关闭
                fos.close();
                is.close();
            }
        }
    }

    public static void readFiles(String inputFile) throws Exception {
        File srcFile = new File(inputFile);
        if (srcFile.isDirectory()) {
            File next[] = srcFile.listFiles();
            for (int i = 0; i < next.length; i++) {
                System.out.println(next[i].getName());
                if(!next[i].isDirectory()){
                    BufferedReader br = new BufferedReader(new FileReader(next[i]));
                    List<String> arr1 = new ArrayList<>();
                    String contentLine ;
                    while ((contentLine = br.readLine()) != null) {
                        JSONObject js = JSONObject.parseObject(contentLine);
                        arr1.add(contentLine);
                    }
                }

            }
        }
    }

    public static void main(String[] args) {
        try {
            String path = "F:\\etc\\dist(1).zip";
            zipUncompress(path,"F:\\etc\\test");
            readFiles(path.replace(".zip", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


http://www.niftyadmin.cn/n/1229748.html

相关文章

编写可测试的程序

转载时请注明出处和作者联系方式 文章出处&#xff1a;http://www.limodev.cn/blog 作者联系方式&#xff1a;李先静 <xianjimli at hotmail dot com> 说到自动测试&#xff0c;不少人都会想到单元测试框架(如cppunit/junit)&#xff0c;或者gui测试工具(winrunner)。我…

java 多线程使用

下面展示一些 内联代码片。 private static ExecutorService ex Executors.newCachedThreadPool();//异步消息 线程池ex.submit(new Runnable() {Overridepublic void run() {//里面是逻辑}});//2//插入日志new Thread(() -> {里面是逻辑}).start();

sscanf函数的高级用法

转载时请注明出处和作者联系方式 文章出处&#xff1a;http://www.limodev.cn/blog 作者联系方式&#xff1a;李先静 <xianjimli at hotmail dot com> 大家都知道sscanf是一个很好用的函数&#xff0c;利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简…

Java封装OkHttp3工具类

Java封装OkHttp3工具类代码如下代码如下 //引入maven <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.10.0</version> </dependency> <dependency><groupId>co…

mysql 24小时分组查询

下面展示一些 内联代码片。 一天24 小时分组查询SELECT a.hour hour, ifnull(b.count, 0) count FROM ( SELECT 0 hour UNION ALL SELECT 1 hour UNION ALL SELECT 2 hour UNION ALL SELECT 3 hour UNION ALL SELECT 4 hour UNION ALL SELECT 5 hour UNION ALL SELECT 6 hour …

IT餐馆—第十八回 祭奠

周一中午吃过午饭&#xff0c;雨辰发现老杜MSN正在线上&#xff0c;就打了个招呼&#xff0c;老杜也挺闲&#xff0c;哥俩儿就聊了起来。侃了一会&#xff0c;就把话题转到了一则新闻上&#xff1a; 员工因丢失iPhone被调查跳楼自杀 雨辰是前些时候在CSDN上看到这则新闻的&…

Java整合打印机打印

Java整合打印机打印直接上代码直接上代码 package com.yx.face.test;import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.SneakyThrows;import javax.print.*; import javax.print.attribute.DocAttributeSet; import javax.print.attribute…

【 多线程调用接口处理结果】

多线程调用接口处理结果直接上代码// An highlighted block //结果接受 List<Future<Map<String, String>>> futureList new ArrayList<>(); //定义线程池ExecutorService threadPool Executors.newFixedThreadPool(requestUrlList.size());try {for…