簡體   English   中英

如何讀取zip文件中的文件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM