提交文件MultipartFile后ZipEntry.size一直为-1

先看一下代码

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
public static void importModels (MultipartFile file) throws IOException, InvalidFormatException {
ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.defaultCharset());
BufferedInputStream bs = new BufferedInputStream(zipInputStream);
Map<String, Map> stringMapMap;
Map<String, String> modelJsons = new HashMap<>();
ZipEntry zipEntry;
String zipFileName;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (zipEntry.isDirectory()) { // do nothing

} else if (zipEntry.getName().endsWith("json")) {
String name = zipEntry.getName();

long size = zipEntry.getSize();
if (size == -1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
int bytes = zipInputStream.read();
if (bytes == -1) {
break;
}
baos.write(bytes);
}
baos.close();
System.out.println(String.format("Name:%s,Content:%s", name, new String(baos.toByteArray())));
} else { // ZipEntry的size正常
byte[] bytes = new byte[(int) zipEntry.getSize()];
zipInputStream.read(bytes, 0, (int) zipEntry.getSize());
System.out.println(String.format("Name:%s,Content:%s", name, new String(bytes)));
}
} else if (zipEntry.getName().endsWith("xlxs")) { //do other things
} else {
return;
}
}
return;
}

文件提交后台

是提交后天后文件并不是在本地,网上之前有提供zipfile后读取在我这边是不可能实现

1
2
3
4
5
6
7
8
while ((ze = zin.getNextEntry()) != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zin));
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}

上面的读取的是整个Zip文件,和要求不符

上传Zip文件不解压读取文件内容时ZipEntry的size为-1的问题