Java 如何读取zip文件中的文件?
请帮忙。我想从zip文件中读取文件。我的zip文件是一个多部分文件。然后我使用ZipInputStream获取它的输入文件,然而,它给出了不创建文件的错误Java 如何读取zip文件中的文件?,java,file,spring-boot,zip,Java,File,Spring Boot,Zip,请帮忙。我想从zip文件中读取文件。我的zip文件是一个多部分文件。然后我使用ZipInputStream获取它的输入文件,然而,它给出了不创建文件的错误 public String importProject(MultipartFile file) throws IOException, ParseException { //Reading the input of zip file, ZipInputStream zin = new ZipInputStream(fi
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();
publicstringimportproject(MultipartFile文件)抛出IOException、ParseException{
//读取zip文件的输入,
ZipInputStream zin=新的ZipInputStream(file.getInputStream());
紫丁香;
FileInputStream excel=null;
ArrayList animationSvgs=新的ArrayList();
while((ze=zin.getnextery())!=null){
如果(ze.getName()包含(“.xlsx”)){
excel=新文件输入流(ze.getName());
}
else if(ze.getName().contains(“.svg”)){
FileInputStream svg=新的FileInputStream(ze.getName());
AnimationSvg AnimationSvg=新的AnimationSvg();
animationSvg.setName(ze.getName());
StringBuilder svgContent=新StringBuilder();
int i;
而((i=svg.read())!=-1){
svgContent.append(String.valueOf((char)i));
}
animationSvg.setSvgContent(String.valueOf(svgContent));
添加(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();
}
zip存档中的条目不是文件。它只是zip中的一系列压缩字节。通过读取ZipInputStream读取每个条目。如果使用Java 9或更高版本,请不要创建新的FileInputStream。