Is it possible to take a file or files, create a zip version of that file and then create an inputstream version of the new zip without storing the whole zip in memory or on a file system?
I am ideally looking for an input->output->input stream where the data is only ever buffered or piped.
The reason, I am trying to take some objects stored in an aws bucket and zip them using an aws lambda function, but lambda has memory limits and can't always store a full zip.
I have been reading about piped input/outputstreams and still don't fully understand how (if at all) my goal is achievable.
private final String BUCKET = "aws-bucket-name";
private final int BUFFER = 2048;
public zip( String[] keys ) {
AmazonS3 s3Client = new AmazonS3Client( new EnvironmentVariableCredentialsProvider() );
for( String inFilename : keys ) {
String outFilename = "output/" + inFilename;
S3Object object = s3Client.getObject( new GetObjectRequest( BUCKET, inFilename ) );
InputStream objectData = object.getObjectContent();
ObjectMetadata omd = new ObjectMetadata();
BufferedInputStream origin = new BufferedInputStream( objectData, BUFFER );
/* need to take above file(s) create a zip stream to them use as input stream for putObject method
* FileOutputStream dest = new FileOutputStream( outFilename );
* ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream( dest ) );
* byte data[] = new byte[ BUFFER ];
* ZipEntry entry = new ZipEntry( output );
* out.putNextEntry( entry );
* int count;
* while ( ( count = origin.read( data, 0, BUFFER ) ) != -1 )
* {
* out.write( data, 0, count );
* }
*/
s3Client.putObject( BUCKET, outFilename, origin, omd );
// close streams
}
}