文章參考:https://bbs.csdn.net/topics/60289970
https://blog.csdn.net/qq_35893120/article/details/79311629?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
需要:為一個zip 的下載下傳連結,下載下傳為一個 byte[ ]。從這個 byte[]中直接擷取想要的圖檔。
/**
* 對 下載下傳zip檔案 的 byte[] 中提取 圖檔的 byte[]
* @author xubenqing
* @param zipByte zip檔案 的 byte[]
* @return 圖檔的 byte[]
*/
private byte[] getImgByet(byte[] zipByte) {
InputStream input = null;
ZipInputStream zin = null;
try {
input = new ByteArrayInputStream(zipByte);
zin = new ZipInputStream(input);
ZipEntry zipEntry;
while((zipEntry = zin.getNextEntry())!= null){
if(zipEntry.isDirectory()){
//do nothing
}else {
String name = zipEntry.getName();
//1、将圖檔提取出來
if(name.endsWith(".jpg") || name.endsWith(".png") ){
suffix = FileUtils.getImgSuffix(name);
return getData(zin);
}
}
}
} catch (Exception e) {
}finally {
if(input != null) {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
if(zin != null) {
try {
zin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
return null;
}
private byte[] getData(InflaterInputStream zis) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
byte[] buf = null;
int length = 0;
while ((length = zis.read(temp, 0, 1024)) != -1) {
bout.write(temp, 0, length);
}
buf = bout.toByteArray();
bout.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}