Search code examples
javaamazon-web-servicesamazon-s3zipzipinputstream

ZipInputStream Stream Closed error on nextEtnry/closeEntry


I'm trying to upload 1 zip file and extracting it in a AWS S3 bucket. I get Stream Closed error:

java.io.IOException: Stream closed
at java.base/java.util.zip.ZipInputStream.ensureOpen(Unknown Source)
at java.base/java.util.zip.ZipInputStream.getNextEntry(Unknown Source)
at com.abc.Handler.extractAndProcessZipFile(Handler.java:115)

This is my code snippet:

    try {
                ZipInputStream zis = new ZipInputStream(
                        S3ObjectUtils.getS3Object(S3_INBOUND, FILE_PATH + metadata.getObjectKey(), this.amazonS3Client)
                                .getObjectContent());
                ZipEntry entry;
    
                while ((entry = zis.getNextEntry()) != null) { //Stream closed Error here when I comment closeEntry()
                    String fileName = entry.getName();
                    
                    String srcKey = metadata.getObjectKey().replace('+', ' ');
                    srcKey = URLDecoder.decode(FILE_PATH + fileMetadata.getObjectKey(), "UTF-8");
                    
                    // Covert to plain text
                    String mimeType = "text/plain";
                                    
                    ObjectMetadata meta = new ObjectMetadata();
                    meta.setContentLength(entry.getSize());
                    meta.setContentType(mimeType);
                    
                    this.amazonS3Client.putObject(S3_IN, FilenameUtils.getFullPath(srcKey) + fileName, zis, meta);
                    zis.closeEntry(); // Stream Closed error here
                }
                zis.close();
} catch (IOException e) {
            e.printStackTrace();
        }

Solution

  • Fixed by removing zis.closeEntry and changed while to if