首先新建一些zip包,里面包含幾個(gè)json文件,如下圖所示:
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)與策劃設(shè)計(jì),瑪多網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:瑪多等地區(qū)?,敹嘧鼍W(wǎng)站價(jià)格咨詢: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對應(yīng)的實(shí)體類package com.zhaotq.service.io;
import java.util.Date;
//json對應(yīng)的實(shí)體類
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包內(nèi)容
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);//轉(zhuǎn)移符添加,在這里沒用
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);
}
}
// 以某關(guān)鍵字讀取某路徑下的壓縮包的方法,最終返回一個(gè)map,map中的每一項(xiàng)是一個(gè)文件或者文件夾
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();
//循環(huán)讀取壓縮包里面的文件
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)) {
//讀取每個(gè)文件的字節(jié),并放進(jìn)數(shù)組
bytes = new byte[(int) ze.getSize()];
bs.read(bytes, 0, (int) ze.getSize());
//將文件轉(zhuǎn)成流
InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
BufferedReader br = new BufferedReader(new InputStreamReader(byteArrayInputStream));
//讀取文件里面的內(nèi)容
String line;
while ((line = br.readLine()) != null) {
orginJson.append(line);
}
//關(guān)閉流
br.close();
String name=new String(ze.getName().replace(".json",""));
jsonMap.put(name,orginJson.toString());
}
}
zin.closeEntry();
inputStream.close();
return jsonMap;
}
}
實(shí)際運(yùn)行結(jié)果如下:
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站欄目:ZipInputStream讀取zip包中的指定文件小案例-java-創(chuàng)新互聯(lián)
當(dāng)前URL:http://jinyejixie.com/article44/jgcee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、動態(tài)網(wǎng)站、微信小程序、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容