重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先新建一些zip包,里面包含几个json文件,如下图所示:
成都创新互联是一家专注于网站设计制作、网站建设与策划设计,玛多网站建设哪家好?成都创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:玛多等地区。玛多做网站价格咨询:13518219792这里以读取name1.json为例:
maven依赖com.fasterxml.jackson.core jackson-core2.14.0 com.fasterxml.jackson.core jackson-databind2.12.7.1 com.fasterxml.jackson.core jackson-annotations2.14.0
json对应的实体类package com.zhaotq.service.io;
import java.util.Date;
//json对应的实体类
public class TestJsonDemoDto {
private String message;
private String condition;
private String com;
private String status;
private String state;
private String time;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public String getCom() {
return com;
}
public void setCom(String com) {
this.com = com;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "TestJsonDemoDto{" +
"message='" + message + '\'' +
", condition='" + condition + '\'' +
", com='" + com + '\'' +
", status='" + status + '\'' +
", state='" + state + '\'' +
", time='" + time + '\'' +
'}';
}
}
主方法package com.zhaotq.service.io;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import jdk.nashorn.internal.parser.JSONParser;
import jdk.nashorn.internal.runtime.JSONFunctions;
import java.io.*;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
//测试读取zip包内容
public class ZipFileDemo {
public static void main(String[] args) {
HashMap hashMap = null;
try {
hashMap = ZipFileDemo.readData("/Users/zhaotianqiang/Documents/01-coding/study-spring/src/main/resources/test.zip",".json");
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("=======json读取=======");
String str = (String) hashMap.get("name1");
System.out.println(str);
String json = JSONParser.quote(str);//转移符添加,在这里没用
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
try {
TestJsonDemoDto testJsonDemoDto = mapper.readValue(str, TestJsonDemoDto.class);
System.out.println(testJsonDemoDto.toString());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
// 以某关键字读取某路径下的压缩包的方法,最终返回一个map,map中的每一项是一个文件或者文件夹
public static HashMap readData(String path,String fileKeyWord) throws IOException {
//得到输入流
InputStream inputStream = new FileInputStream(path);
ZipInputStream zin = new ZipInputStream(inputStream);
BufferedInputStream bs = new BufferedInputStream(zin);
byte[] bytes = null;
ZipEntry ze;
HashMap jsonMap= new HashMap();
//循环读取压缩包里面的文件
int a = 1;
while ((ze = zin.getNextEntry()) != null) {
System.out.println("第"+a++ +"层是:"+ze.getName());
System.out.println("是否是目录:"+ze.isDirectory());
StringBuilder orginJson = new StringBuilder();
if (ze.toString().endsWith(fileKeyWord)) {
//读取每个文件的字节,并放进数组
bytes = new byte[(int) ze.getSize()];
bs.read(bytes, 0, (int) ze.getSize());
//将文件转成流
InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
BufferedReader br = new BufferedReader(new InputStreamReader(byteArrayInputStream));
//读取文件里面的内容
String line;
while ((line = br.readLine()) != null) {
orginJson.append(line);
}
//关闭流
br.close();
String name=new String(ze.getName().replace(".json",""));
jsonMap.put(name,orginJson.toString());
}
}
zin.closeEntry();
inputStream.close();
return jsonMap;
}
}
实际运行结果如下:
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧