Java 上传zip文件把解压后的文件存到另外的文件夹

// zip文件上传
    public Map uploadFile(MultipartFile[] files) {
        Map outMap = new HashMap<String, Object>();
        try {
            // zip文件存放路径
            String path = localpath + "/document";
            File dir = new File(path);
            if (!dir.exists() && !dir.isDirectory()) {
                dir.mkdirs();
            }
            int fileLength = files.length;
            for (int i = 0; i < fileLength; i++) {
                MultipartFile upfile = files[i];
                String upFilename = upfile.getOriginalFilename();
                String suffix = upFilename.substring(upFilename.lastIndexOf("."));
                String filename = UUID.randomUUID().toString() + suffix;
                File targetFile = new File(path, filename);
                upfile.transferTo(targetFile);
                outMap.put("url", "/upload/document/" + filename);
                outMap.put("state", "success");
                zipTest(path + "/" + filename);
            }
        } catch (Exception e) {
            e.printStackTrace();
            outMap.put("state", "error");
        }
        return outMap;
    }
 public  void zipTest(String lpath) throws Exception {
        // 解压文件存放路径
        String newPath = localpath + "/zipFile";
        File file = new File(lpath);
        // 文件是否存在
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        ZipFile zipFile = null;
        ZipInputStream zin = null;
        FileInputStream fis = null;
        try {
            Charset gbk = Charset.forName("GBK");
            zipFile = new ZipFile(file, gbk);
            fis = new FileInputStream(file);
            zin = new ZipInputStream(fis, gbk);

            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                String path = ze.getName();
                System.out.println(path);
                if (!ze.isDirectory() ) {
                    InputStream inputStream = zipFile.getInputStream(ze);
                    File deskFile = new File(newPath,path);
                    FileOutputStream output = new FileOutputStream(deskFile);
                    IOUtils.copy(inputStream, output);
                    output.close();
                    inputStream.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zin != null) {
                    zin.closeEntry();
                    zin.close();
                }
                if (fis != null)
                    fis.close();
                if (zipFile != null)
                    zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值