【Java IO】从url在线下载zip包并解析Excel,解压到本地

512 阅读1分钟
  1. 建立http连接,获取BufferedInputStream输入流
 public static BufferedInputStream getInputStreamByUrl(String urlStr) {
        try {
            // 统一资源
            URL url = new URL(urlStr);
            // 连接类的父类,抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            //设置超时
            httpURLConnection.setConnectTimeout(1000 * 5);
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
            httpURLConnection.connect();
            // 文件大小
            int fileLength = httpURLConnection.getContentLength();
            // 建立链接从请求中获取数据
            return new BufferedInputStream(httpURLConnection.getInputStream());
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
  1. 将BufferedInputStream流转成ZipInputStream,此处需要设置编码为“GBK”
    public static ZipInputStream getZipInputStreamByUrl(String urlStr) {
        BufferedInputStream inputStream = getInputStreamByUrl(urlStr);
        Charset gbk = Charset.forName("gbk");
        return new ZipInputStream(inputStream, gbk);
    }
  1. 读取ZipInputStream流中的Excel文件,并将其他文件解压到本地路径,此处需要引入poi-ooxml依赖去解析Excel成XSSFWorkbook对象
public static  List<XSSFWorkbook> workbook findExcelInZip(String url, String localUnZipPath) {
        ZipInputStream zin = FileUtil.getZipInputStreamByUrl(url);
        File pathFile = new File(localUnZipPath);
        if (!pathFile.exists()) {
            log.info("临时文件夹不存在,创建目录[{}]", localUnZipPath);
            pathFile.mkdirs();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        List<XSSFWorkbook> workbook = new ArrayList<>();
        try {
            //读取一个目录
            ZipEntry nextEntry = zin.getNextEntry();
            //不为空进入循环
            while (nextEntry != null) {
                String name = nextEntry.getName();
                File file = new File(localUnZipPath + name);
                //如果是目录,创建目录
                if (name.endsWith("/")) {
                    file.mkdir();
                } else {
                    //文件则写入具体的路径中
                    String zipFileName = nextEntry.getName();
                    if (zipFileName.contains(".") && (zipFileName.endsWith("xlsx"))) {
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = zin.read(buffer)) > -1) {
                            baos.write(buffer, 0, len);
                        }
                        baos.flush();
                        @Cleanup ByteArrayInputStream stream1 = new ByteArrayInputStream(baos.toByteArray()); //excel 流
                        workbook.add(new XSSFWorkbook(stream1));
                    } else {
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                        int n;
                        byte[] bytes = new byte[1024];
                        while ((n = zin.read(bytes)) != -1) {
                            bufferedOutputStream.write(bytes, 0, n);
                        }
                        //关闭流
                        bufferedOutputStream.close();
                        fileOutputStream.close();
                    }
 
                }
                //关闭当前目录
                zin.closeEntry();
                //读取下一个目录,作为循环条件
                nextEntry = zin.getNextEntry();
            }
            zin.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return workbook;
    }