[英]How to read the files in the zip file?
请帮忙。 我想从zip文件中读取文件。 我的zip文件是MultipartFile。 然后我使用ZipInputStream获取其输入文件,但是,它给出了未找到该文件的错误。
public String importProject(MultipartFile file) throws IOException, ParseException {
//Reading the input of zip file,
ZipInputStream zin = new ZipInputStream(file.getInputStream());
ZipEntry ze;
FileInputStream excel = null;
ArrayList<AnimationSvg> animationSvgs = new ArrayList<>();
while ((ze = zin.getNextEntry()) != null) {
if(ze.getName().contains(".xlsx")){
excel = new FileInputStream(ze.getName());
}
else if(ze.getName().contains(".svg")){
FileInputStream svg = new FileInputStream(ze.getName());
AnimationSvg animationSvg = new AnimationSvg();
animationSvg.setName(ze.getName());
StringBuilder svgContent = new StringBuilder();
int i;
while((i = svg.read())!=-1) {
svgContent.append(String.valueOf((char) i));
}
animationSvg.setSvgContent(String.valueOf(svgContent));
animationSvgs.add(animationSvg);
}
zin.closeEntry();
}
zin.close();
zip存档中的条目不是文件。 这只是zip中压缩字节的序列。
根本不使用FileInputStream。 只需从您的ZipInputStream读取zip条目数据:
Path spreadsheetsDir = Files.createTempDirectory(null);
Path excel = null;
while ((ze = zin.getNextEntry()) != null) {
String name = ze.getName();
if (name.endsWith(".xlsx")) {
excel = spreadsheetsDir.resolve(name));
Files.copy(zin, excel);
} else if (name.endsWith(".svg")) {
AnimationSvg animationSvg = new AnimationSvg();
animationSvg.setName(name);
animationSvg.setSvgContent(
new String(zin.readAllBytes(), StandardCharsets.UTF_8));
animationSvgs.add(animationSvg);
}
zin.closeEntry();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.