Java 导入zip,并进行解压

场景一:上传zip包,后台进行解压,提取里面的文件,进行上传。

    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @RequestMapping(value = "updPlScpjcjd", method = RequestMethod.POST)
    public ReturnT updPlScpjcjd(KtbgPage page
            , @RequestParam(value = "file", required = false) MultipartFile file
            , @AuthenticationPrincipal UserManagerEntity user
            , HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        Map map = new HashMap();
        ReturnT re = new ReturnT(map);
        String webSocketUrl = "/updPlScpjcjd-" + page.getP_pycc();
        File zipFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            try {
                ins = file.getInputStream();
                zipFile = new File(file.getOriginalFilename());
                inputStreamToFile(ins, zipFile);
                ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
                int ypxhrs = 0;
                for (Enumeration enumeration = zip.entries(); enumeration.hasMoreElements(); ) {
                    ZipEntry entry = (ZipEntry) enumeration.nextElement();
                    ypxhrs++;
                    // 创建一个数值格式化对象
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    // 设置精确到小数点后2位
                    numberFormat.setMaximumFractionDigits(2);
                    String result = numberFormat.format((float) ypxhrs / (float) zip.size() * 100);
                    messagingTemplate.convertAndSendToUser(user.getId(), webSocketUrl, result); //用户id,路径,消息
                    if (entry.isDirectory()) {
                        continue;
                    }
                    //文件的
                    String filename = entry.getName();
                    System.out.println(filename);
                    int lastIndex = filename.lastIndexOf(".");
                    System.out.println(lastIndex);
                    String xh = filename.substring(0, lastIndex);
                    System.out.println(xh);
                    //将zipEntry转成输入流
                    InputStream inputStream = zip.getInputStream(entry);
                    //将inputstream转换成mulipartFile
                    MultipartFile multipartFile = new MockMultipartFile(filename, filename, "text/plain", inputStream);
                    //文件名就是学号
                    page.setP_xh(xh);
                    //调用上传接口
                    ktbgService.updPlScpjcjd(request, response, page, multipartFile, map);
                }
            } catch (IOException e) {
                e.printStackTrace();
                re.setCode(500);
            } finally {
                if (ins != null) {
                    ins.close();
                }
            }
        }
        return re;
    }

场景二:上传zip,后台进行解压,提取zip包中“文件夹”以及该“文件夹”下的文件后,再进行压缩上传。

    public static void putNextEntry(InputStream inputStream,String name,ZipOutputStream zipOut) throws IOException {
        //读取相关的文件
        InputStream input = inputStream;//
        zipOut.putNextEntry(new ZipEntry(name));
        // 设置注释
        //zipOut.setComment("hello");
        int temp = 0;
        //读取相关的文件
        while((temp = input.read()) != -1){
            //写入输出流中
            zipOut.write(temp);
        }
        //关闭流
        input.close();
    }
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @RequestMapping(value = "updateScPybByNkd", method = RequestMethod.POST)
    public ReturnT updateScPybByNkd(LwdbPage page
            , @RequestParam(value = "file", required = false) MultipartFile file
            , @AuthenticationPrincipal UserManagerEntity user
            , HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        Map map = new HashMap();
        ReturnT re = new ReturnT(map);
        String webSocketUrl = "/updPlScpjcjd-" + page.getP_pycc();
        File zipFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            try {
                ins = file.getInputStream();
                zipFile = new File(file.getOriginalFilename());
                inputStreamToFile(ins, zipFile);
                ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
                int ypxhrs = 0;
                for (Enumeration enumeration = zip.entries(); enumeration.hasMoreElements(); ) {
                    ZipEntry entry = (ZipEntry) enumeration.nextElement();
                    ypxhrs++;
                    // 创建一个数值格式化对象
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    // 设置精确到小数点后2位
                    numberFormat.setMaximumFractionDigits(2);
                    String result = numberFormat.format((float) ypxhrs / (float) zip.size() * 100);
                    messagingTemplate.convertAndSendToUser(user.getId(), webSocketUrl, result); //用户id,路径,消息
                    if (entry.isDirectory()) {
                        String directoryName = entry.getName();
                        //设置输出流
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        ZipOutputStream zipOut = new ZipOutputStream(out);
                        for (Enumeration enumeration2 = zip.entries(); enumeration2.hasMoreElements(); ) {
                            ZipEntry entry2 = (ZipEntry) enumeration2.nextElement();
                            String fileName = entry2.getName();
                            if (!entry2.isDirectory() && fileName.contains(directoryName)) {
                                //將文件添加到包中
                                putNextEntry(zip.getInputStream(entry2),fileName,zipOut);
                            }
                        }
                        zipOut.close();
                        String fileName = directoryName.replaceAll("/","")+".zip";
                        MultipartFile multipartFile = new MockMultipartFile( fileName, fileName, "text/plain", new ByteArrayInputStream(out.toByteArray()));

                        //解析學號
                        int lastIndex = directoryName.lastIndexOf("_");
                        String xh = directoryName.substring(lastIndex+1, directoryName.length());
                        //文件名就是学号
                        page.setP_xh(xh.replaceAll("/",""));
                        //调用上传接口
                        lwdbService.updPyjlcjfjidByXh(request, response,multipartFile, page);
                        continue;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                re.setCode(500);
            } finally {
                if (ins != null) {
                    ins.close();
                }
            }
        }
        return re;
    }

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
如果你想提高开发爬虫的效率,如果你用selenium老是被网站检测到机器识别,如果你想实现js注入。请你立马用goniub。 开发工具在软件开发生命周期中扮演着至关重要的角色,它们旨在简化和加速从概念设计到产品部署的各个环节。以下是开发工具的主要作用: 代码编写与编辑: 提供集成开发环境(IDE),如Visual Studio、Eclipse、Android Studio和Sublime Text等,这些工具集成了文本编辑器,支持语法高亮、自动补全、代码片段管理和版本控制等功能,有助于开发者高效编写和维护代码。 项目管理: 支持项目创建、组织、构建自动化以及依赖管理,确保不同模块和组件之间的协调一致。 编译与构建: 包括编译器、构建工具(如Make、Gradle、Maven)等,用于将源代码转换为可执行文件或库,并进行资源打包、优化等处理。 调试与测试: 集成调试器允许开发者逐行执行代码,设置断点、查看变量值、跟踪调用堆栈等,帮助定位并修复代码中的错误。 测试框架和工具则协助开发者编写和运行单元测试、集成测试及性能测试,确保软件质量。 版本控制与协作: 通过集成Git、SVN等版本控制系统,支持团队成员间的代码共享、分支管理、合并请求和冲突解决。 可视化设计与原型制作: 对于UI/UX设计,有界面设计工具,如Sketch、Adobe XD,可以帮助设计师快速构建应用程序界面模型,并生成规范的设计稿供开发人员参考实现。 跨平台支持: 跨平台开发工具如Xamarin、React Native和Flutter,让开发者使用一种语言或框架编写可以在多个操作系统上运行的应用程序。 文档编写与API管理: 文档生成工具可以自动生成代码注释文档,便于团队内外理解和使用项目代码。 API管理工具则方便开发者创建、测试、发布和维护API接口。 持续集成与持续部署(CI/CD): Jenkins、Travis CI、GitHub Actions等工具负责自动化构建、测试和部署流程,提高交付效率和可靠性。 数据库管理与ORM工具: 数据库客户端工具用于连接、查询、更新数据库,ORM(对象关系映射)工具简化了数据操作和持久化层的开发工作。 总之,开发工具极大地提升了软件工程师的工作效率,保证了开发过程中的准确性与一致性,同时也促进了团队合作,使得软件开发更系统化、规范化和工业化。
Java解压zip文件并循环导入Excel数据可以通过以下步骤实现: 首先,需要使用JavaZipInputStream类来解压zip文件。可以使用FileInputStream类来读取zip文件,然后通过ZipInputStream类逐个读取zip中的条目。可以使用ZipEntry类获取条目的名称,并使用ZipInputStream类的getNextEntry()方法将输入流的位置移动到下一个条目。 然后,需要使用Java的Apache POI库来读取和操作Excel文件。可以使用Workbook类来打开和读取Excel文件,可以根据需要选择使用HSSFWorkbook(用于处理旧版本的Excel文件)或XSSFWorkbook(用于处理最新版本的Excel文件)。可以使用Sheet类来获取工作簿中的工作表,使用Row类来获取行,并使用Cell类来获取单元格。 在循环过程中,可以通过ZipEntry类判断解压缩的条目是否为Excel文件。如果是Excel文件,则可以根据需要使用Workbook类打开并读取该文件。然后,可以使用Sheet类获取工作簿中的工作表。通过循环遍历行和单元格,可以逐行逐列地读取和处理Excel数据。 最后,根据需要对读取的Excel数据进行处理和存储,可以将其保存到数据库中或导出到其他文件格式中。 需要注意的是,解压和读取大型zip文件和Excel文件可能会消耗较多的内存和时间。因此,建议在处理大型文件时使用适当的缓存和优化策略,以提高性能和效率。 总结来说,使用JavaZipInputStream类解压zip文件,并使用Apache POI库读取和处理Excel数据,能够很方便地实现解压zip文件并循环导入Excel数据的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D哈迪斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值