1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| @Value("${tempPath}") protected String tempPath;
@Value("${charset}") protected String charset;
public String unZipFileToTempPath(MultipartFile upFile) { String returnPath = null; String fileName = upFile.getOriginalFilename(); String prefix = fileName.substring(0, fileName.lastIndexOf(".")); if (!fileName.toLowerCase().endsWith(".zip")) { throw new MyException("仅支持zip压缩文件"); } OutputStream out = null; InputStream in = null; ZipInputStream zipInputStream = null; try { File file = new File(tempPath + "/" + fileName); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); log.info("the tempFile Path Is there : {}", file.getAbsolutePath()); } upFile.transferTo(file); ZipFile zipFile = new ZipFile(file, Charset.forName(charset)); zipInputStream = new ZipInputStream(new FileInputStream(file)); ZipEntry entry; File tempFile = new File(tempPath + "/" + prefix + "/"); if (!tempFile.exists()) { tempFile.mkdirs(); } while (((entry=zipInputStream.getNextEntry())!=null)&& !entry.isDirectory()) { String entryName = entry.getName(); if (entryName.toLowerCase().endsWith(".shp")) { returnPath = tempPath + "/" + prefix + "/" + entryName; } out = new FileOutputStream(tempPath + "/" + prefix + "/" + entryName); in = zipFile.getInputStream(entry); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0) { out.write(buf1,0,len); } in.close(); out.close(); } zipInputStream.close(); zipFile.close(); if (file.exists()) { if (file.delete()) { log.info("压缩文件{}清理成功", fileName); } } } catch (Exception e) { throw new MyException("解压缩文件时出现异常", e); } return returnPath; }
|