传入压缩包文件路径,我的参数是因为处理业务需求。
public Map<String, String> unZipFiles(File zipFile, String descDir, String dataType,String markType) throws IOException {
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
// 解决zip文件中有中文目录或者中文文件
ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
Map<String, String> map = new HashMap<>();
for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) { // 排除目录,因为可能把目录解析了导致上传文件多了一个,后台解压失败。
// 文件名重新赋值
String picName = UUID.randomUUID().toString();
String zipEntryName = entry.getName();
int lastIndexOf = zipEntryName.lastIndexOf(".");
// 获取文件的后缀名
String suffix = zipEntryName.substring(lastIndexOf);
InputStream in = zip.getInputStream(entry);
if (suffix.equals(".json")) {
// 处理json文件逻辑
InputStreamReader inputStreamReader2=new InputStreamReader(in);
String charsetName = inputStreamReader2.getEncoding();
if(charsetName.equals("UTF8")){
charsetName="UTF-8";
}
InputStreamReader inputStreamReader=new InputStreamReader(in,charsetName);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String line;
StringBuffer sb = new StringBuffer();
while ((line=bufferedReader.readLine())!=null){
sb.append(line);
}
bufferedReader.close();
inputStreamReader2.close();
inputStreamReader.close();
in.close();
JSONObject jsonObject = JSONObject.parseObject(sb.toString());
String shapes = jsonObject.getString("shapes");
String imageHeight = jsonObject.getString("imageHeight");
String imageWidth = jsonObject.getString("imageWidth");
List<ShapesInfo> list = JSONArray.parseArray(jsonObject.getString("shapes"), ShapesInfo.class);
JSONObject jsonObject1 = new JSONObject();
JSONObject json = new JSONObject();
HashSet<String> set = new HashSet<>();
for (ShapesInfo shapesInfo : list) {
String label = shapesInfo.getLabel();
List<List<Double>> points = shapesInfo.getPoints();
//判断是不是图像分割类型,如果是则返回点位信息
if (markType.equals("3")){
json.put("label", label);
json.put("points", points);
set.add(json.toString());
}else {
String x1 = points.get(0).get(0).toString();
BigDecimal x11 = new BigDecimal(x1);
String y1 = points.get(0).get(1).toString();
BigDecimal y11 = new BigDecimal(y1);
String x2 = points.get(1).get(0).toString();
BigDecimal x22 = new BigDecimal(x2);
String y2 = points.get(1).get(1).toString();
BigDecimal y22 = new BigDecimal(y2);
// 比较x1和x2,取较小值
BigDecimal minX = x11.min(x22);
// 比较y1和y2,取较小值
BigDecimal minY = y11.min(y22);
BigDecimal width = x11.subtract(x22, MathContext.DECIMAL32).abs();
BigDecimal height = y11.subtract(y22, MathContext.DECIMAL32).abs();
int x = minX.intValue();
int y = minY.intValue();
int w = width.intValue();
int h = height.intValue();
jsonObject1.put("x", x);
jsonObject1.put("y", y);
jsonObject1.put("width", w);
jsonObject1.put("height", h);
json.put("label", label);
json.put("coordinates", jsonObject1);
set.add(json.toString());
}
}
map.put(entry.getName(),set.toString());
} else {
String outPath = (descDir + picName + suffix).replaceAll("\\*", "/");
map.put(entry.getName(), picName + suffix);
if (new File(outPath).isDirectory()) {
continue;
}
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
out.close();
}
in.close();
}
}
return map;
}