Java 导入zip,并进行解压

时间:2025-02-13 10:21:51

场景一:上传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 = (buffer, 0, 8192)) != -1) {
                (buffer, 0, bytesRead);
            }
            ();
            ();
            ();
        } catch (Exception e) {
            ();
        }
    }
    @ResponseBody
    @RequestMapping(value = "updPlScpjcjd", method = )
    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 (("") || () <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            try {
                ins = ();
                zipFile = new File(());
                inputStreamToFile(ins, zipFile);
                ZipFile zip = new ZipFile(zipFile, ("GBK"));
                int ypxhrs = 0;
                for (Enumeration enumeration = (); (); ) {
                    ZipEntry entry = (ZipEntry) ();
                    ypxhrs++;
                    // 创建一个数值格式化对象
                    NumberFormat numberFormat = ();
                    // 设置精确到小数点后2位
                    (2);
                    String result = ((float) ypxhrs / (float) () * 100);
                    ((), webSocketUrl, result); //用户id,路径,消息
                    if (()) {
                        continue;
                    }
                    //文件的
                    String filename = ();
                    (filename);
                    int lastIndex = (".");
                    (lastIndex);
                    String xh = (0, lastIndex);
                    (xh);
                    //将zipEntry转成输入流
                    InputStream inputStream = (entry);
                    //将inputstream转换成mulipartFile
                    MultipartFile multipartFile = new MockMultipartFile(filename, filename, "text/plain", inputStream);
                    //文件名就是学号
                    page.setP_xh(xh);
                    //调用上传接口
                    (request, response, page, multipartFile, map);
                }
            } catch (IOException e) {
                ();
                (500);
            } finally {
                if (ins != null) {
                    ();
                }
            }
        }
        return re;
    }

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

    public static void putNextEntry(InputStream inputStream,String name,ZipOutputStream zipOut) throws IOException {
        //读取相关的文件
        InputStream input = inputStream;//
        (new ZipEntry(name));
        // 设置注释
        //("hello");
        int temp = 0;
        //读取相关的文件
        while((temp = ()) != -1){
            //写入输出流中
            (temp);
        }
        //关闭流
        ();
    }
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = (buffer, 0, 8192)) != -1) {
                (buffer, 0, bytesRead);
            }
            ();
            ();
            ();
        } catch (Exception e) {
            ();
        }
    }
    @ResponseBody
    @RequestMapping(value = "updateScPybByNkd", method = )
    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 (("") || () <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            try {
                ins = ();
                zipFile = new File(());
                inputStreamToFile(ins, zipFile);
                ZipFile zip = new ZipFile(zipFile, ("GBK"));
                int ypxhrs = 0;
                for (Enumeration enumeration = (); (); ) {
                    ZipEntry entry = (ZipEntry) ();
                    ypxhrs++;
                    // 创建一个数值格式化对象
                    NumberFormat numberFormat = ();
                    // 设置精确到小数点后2位
                    (2);
                    String result = ((float) ypxhrs / (float) () * 100);
                    ((), webSocketUrl, result); //用户id,路径,消息
                    if (()) {
                        String directoryName = ();
                        //设置输出流
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        ZipOutputStream zipOut = new ZipOutputStream(out);
                        for (Enumeration enumeration2 = (); (); ) {
                            ZipEntry entry2 = (ZipEntry) ();
                            String fileName = ();
                            if (!() && (directoryName)) {
                                //將文件添加到包中
                                putNextEntry((entry2),fileName,zipOut);
                            }
                        }
                        ();
                        String fileName = ("/","")+".zip";
                        MultipartFile multipartFile = new MockMultipartFile( fileName, fileName, "text/plain", new ByteArrayInputStream(()));

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

相关文章