How to go from spring mvc multipartfile into zipinputstream

前端 未结 1 1190
情书的邮戳
情书的邮戳 2021-01-16 01:11

I have a Spring MVC controller that accepts a MultipartFile, which will be a zip file. The problem is I can\'t seem to go from that to a ZipInputStream or ZipFile, so that I

1条回答
  •  别那么骄傲
    2021-01-16 02:06

    I would for starters rewrite some of the code instead of doing things in memory with additional byte[].

    You are using Spring's Resource class so why not simply use the getInputStream() method to construct the MockMultipartFile as you want to upload that file.

    Resource template = wac.getResource("classpath:lc_content/content.zip");
    MockMultipartFile firstFile = new MockMultipartFile(
            "file", "content.zip", MediaType.APPLICATION_OCTET_STREAM_VALUE, template.getInputStream());
    

    The same for your upload processing code the ZipInputStream can also be constructed on another InputStream which is also provided by the MultipartFile interface.

    protected void processFileZipEntry(MultipartFile file, String signature) throws IOException {
    
        LOG.debug("Processing archive with size={}.", file.getSize());
        ZipInputStream zis = new ZipInputStream(file.getInputStream());
    

    Wouldn't be the first time that jugling around with byte[] gives a problem. I also vaguely recall some issues with ZipInputStream which lead us to use ZipFile but for this you will first have to store the file in a temp directoy using the transferTo method on MultipartFile.

    File tempFile = File.createTempFile("upload", null);
    file.transferTo(tempFile);
    ZipFile zipFile = new ZipFle(tempFile);
    // Proces Zip
    tempFile.delete();
    

    0 讨论(0)
提交回复
热议问题