美文网首页
springboot学习-读取压缩文件中的所有shp文件的路径

springboot学习-读取压缩文件中的所有shp文件的路径

作者: MrSwilder | 来源:发表于2019-11-28 10:36 被阅读0次

    1.解压缩文件

     /**
         *
         * @Description: TODO(找到文件并读取解压到指定目录)
         * @param 设定文件
         * @return void 返回类型
         * @throws
         */
        public void unZipFile(ZipEntry ze, InputStream read,
                              String saveRootDirectory) throws FileNotFoundException, IOException {
            // 如果只读取图片,自行判断就OK.
            String fileName = ze.getName();
            // 判断文件是否符合要求或者是指定的某一类型
    //      if (fileName.equals("WebRoot/WEB-INF/web.xml")) {
            // 指定要解压出来的文件格式(这些格式可抽取放置在集合或String数组通过参数传递进来,方法更通用)
            File file = new File(saveRootDirectory + fileName);
            if (!file.exists()) {
                File rootDirectoryFile = new File(file.getParent());
                // 创建目录
                if (!rootDirectoryFile.exists()) {
                    boolean ifSuccess = rootDirectoryFile.mkdirs();
                    if (ifSuccess) {
                        System.out.println("文件夹创建成功!");
                    } else {
                        System.out.println("文件创建失败!");
                    }
                }
                // 创建文件
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 写入文件
            BufferedOutputStream write = new BufferedOutputStream(
                    new FileOutputStream(file));
            int cha = 0;
            while ((cha = read.read()) != -1) {
                write.write(cha);
            }
            // 要注意IO流关闭的先后顺序
            write.flush();
            write.close();
            read.close();
            // }
    //      }
        }
    

    2.读取解压缩后文件夹中的shp文件

    /**
         *
         * @Description: TODO(读取Zip信息,获得zip中所有的目录文件信息)
         * @param设定文件
         * @return void 返回类型
         * @throws
         */
        public List<String> zipFileRead(String file, String saveRootDirectory) {
            List<String> listFiles=new ArrayList<>();
            try {
                // 获得zip信息
                ZipFile zipFile = new ZipFile(file, Charset.forName("GBK"));
                System.out.println(zipFile.getName());
                @SuppressWarnings("unchecked")
                Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.entries();
                while (enu.hasMoreElements()) {
                    ZipEntry zipElement = (ZipEntry) enu.nextElement();
                    InputStream read = zipFile.getInputStream(zipElement);
                    String fileName = zipElement.getName();
                    if (fileName != null && fileName.indexOf(".") != -1) {// 是否为文件
                        unZipFile(zipElement, read, saveRootDirectory);
                        if(zipElement.getName().contains(".shp")&&(!zipElement.getName().contains(".xml"))){
                            listFiles.add(zipElement.getName());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return listFiles;
        }
    

    3.调用

      @Test
        public void test() throws FileNotFoundException,IOException {
            String filePath = "E://青海省草原类型图_xa80.zip";
            String saveRootDirectory="E://test/";
            List<String> listFiles=zipFileRead(filePath,saveRootDirectory);
        }
    

    相关文章

      网友评论

          本文标题:springboot学习-读取压缩文件中的所有shp文件的路径

          本文链接:https://www.haomeiwen.com/subject/xvbvwctx.html