tupaiopiao 2021-07-07 08:53 采纳率: 80.3%
浏览 206
已结题

java导入zip文件

java导入zip文件的功能怎么做,现有的资料都是来自同一份模板,只给出了主代码,其他的配置都没有详说,求一个详细的教程

  • 写回答

1条回答 默认 最新

  • 小P聊技术 2021-07-07 08:57
    关注

    1.读取zip中文件名,返回文件名列表

        //读取zip文件内的文件,返回文件名称列表
        public static List<String> readZipFileName(String path){
            List<String> list = new ArrayList<>();
            try {
                ZipFile zipFile = new ZipFile(path);
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    list.add(entries.nextElement().getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return list;
        }
    

    2.读取zip中文件的内容

        //读取zip文件内的文件,返回文件内容列表
        public static List<String> readZipFile(String path){
            List<String> list = new ArrayList<>();
            List<List<String>> ddlList=null;
            try {
                ZipFile zipFile = new ZipFile(path);
                InputStream in = new BufferedInputStream(new FileInputStream(path));
                ZipInputStream zin = new ZipInputStream(in);
                ZipEntry ze;
                while ((ze = zin.getNextEntry()) != null) {
                    ddlList=new ArrayList<>();
                    if (ze.isDirectory()) {
                    } else {
                        System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
                        long size = ze.getSize();
                        if (size > 0) {
                            BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze),Charset.forName("gbk")));
                            String line;
                            while ((line = br.readLine()) != null) {
                                String[] index = line.split(",");
                                List<String> indexList = Arrays.asList(index);
                                ddlList.add(indexList);
                            }
                            br.close();
                        }
                    }
                    //处理ddlList,此时ddlList为每个文件的内容,while每循环一次则读取一个文件
                }
                zin.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //此处返回无用,懒得修改了
            return list;
        }
    
    

    3 上传zip文件

        //保存zip文件
        public String saveZipFile(MultipartFile file, String path) {
            String resultPath = "";
            ZipInputStream zipInputStream = null;
            FileOutputStream fs = null;
            try {
                resultPath = "D:" + path+"/" + file.getOriginalFilename();
                File zipFile = new File(resultPath);
                if (!zipFile.exists()) {
                    new File(zipFile.getParent()).mkdirs();
                    zipFile.createNewFile();
                }
                zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
                BufferedInputStream stream = new BufferedInputStream(file.getInputStream());
                fs = new FileOutputStream(zipFile);
                byte[] buffer = new byte[1024];
                int i = -1;
                while ((i = stream.read(buffer)) != -1) {
                    fs.write(buffer, 0, i);
                    fs.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    zipInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return resultPath;
        }
    
    

    4 获取zip中文件个数

     public int getZipFileCount(String zipFilePath) {
            ZipFile zf = null;
            int count = 0;
            try {
                zf = new ZipFile(zipFilePath);
                count = zf.size();     //返回zip文件中的条目数
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    zf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return count;
        }
    

    5 解压缩zip(直接解压内部文件,去掉父级目录)

    /**
         * 解压zip压缩包
         * @param path  要解压的文件路径
         * @param name  要解压的文件名
         * @param resultPath    解压后文件存放路径
         */
        public static final void unzip(String path,String name,String resultPath,String code){
            try {
                ZipFile zipFile = new ZipFile(path+"/"+name);
                InputStream in = new BufferedInputStream(new FileInputStream(path+"/"+name));
                ZipInputStream zin = new ZipInputStream(in);
                ZipEntry ze;
                while ((ze = zin.getNextEntry()) != null) {
                    System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
                    //创建文件
                    String newTextPath = resultPath+"/"+ze.getName();
                    long size = ze.getSize();
                    if (size > 0) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze), Charset.forName(code)));
                        String line;
                        while ((line = br.readLine()) != null) {
                            FileUtil.appendString(line+"\r\n", newTextPath, "UTF-8");
                        }
                        br.close();
                    }
                }
                zin.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月6日
  • 已采纳回答 8月29日

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答