[英]How to read the files in the zip file?
please help. 请帮忙。 I want to read files from the zip file.
我想从zip文件中读取文件。 My zip file comes as a MultipartFile.
我的zip文件是MultipartFile。 Then I was taking its input file using ZipInputStream, however, it gives error of not founding the file.
然后我使用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();
An entry in a zip archive is not a file. zip存档中的条目不是文件。 It's just a sequence of compressed bytes in the zip.
这只是zip中压缩字节的序列。
Do not use FileInputStream at all. 根本不使用FileInputStream。 Just read zip entry data from your ZipInputStream:
只需从您的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.