Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load zip file that resides in S3 bucket?

Tags:

java

amazon-s3

I have a situation where I need to open a zip file that resides in S3 bucket. So far my code is like below:

public ZipFile readZipFile(String name) throws Exception {
    GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
    S3Object obj = s3Client.getObject(req);
    S3ObjectInputStream is = obj.getObjectContent();

    /******************************
     * HOW TO DO
     ******************************/
    return null;
}

Previously I did try creating a temporary file object and with File.createTempFile function, but I always got trouble where I don't get the File object created. My previous attempt was like below:

public ZipFile readZipFile(String name) throws Exception {
    GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
    S3Object obj = s3Client.getObject(req);
    S3ObjectInputStream is = obj.getObjectContent();

    File temp = File.createTempFile(name, "");
    temp.setWritable(true);
    FileOutputStream fos = new FileOutputStream(temp);
    fos.write(IOUtils.toByteArray(is));
    fos.flush();
    return new ZipFile(temp);
}

Anybody ever got into this situation? Please advice me thanks :)

like image 894
Kunto Fullstack Avatar asked Oct 11 '25 09:10

Kunto Fullstack


1 Answers

If you want to use the zip file immediately without saving it to a temporary file first, you can use java.util.zip.ZipInputStream:

import java.util.zip.ZipInputStream;

S3ObjectInputStream is = obj.getObjectContent();
ZipInputStream zis = new ZipInputStream(is);

From there on you can read through the entries of the zip files, ignoring the ones that you don't need, and using the ones that you need:

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
    String name = entry.getName();
    if (iWantToProcessThisEntry(name)) {
        processFile(name, zis);
    }
    zis.closeEntry();
}

public void processFile(String name, InputStream in) throws IOException { /* ... */ }

You don't need to worry about storing temporary files that way.

like image 126
Erwin Bolwidt Avatar answered Oct 16 '25 06:10

Erwin Bolwidt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!