Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Creating a zip of a particular rendition of assets in aem

Avatar

Adobe Champion

I have a list if inputStream as below which contains the stream of a particular rendition of an asset. assetResourceList is a global list<beanclass> object.

 

private void createZipFile( ) throws IOException {
 List<InputStream> inputStreamList = new ArrayList<InputStream>();
 for(Resource assetResource:assetResourceList)
 {
    final Asset asset = DamUtil.resolveToAsset(assetResource);
    InputStream in = asset.getRendition("cq5dam.thumbnail.48.48.png").getStream();
    inputStreamList.add(in);
 }
}

 

How can i convert this inputstream to a zip of images?

I used the below code for the same, but the zip is getting created with filetype and when i open the zip, it says file format not supported. Can someone help.

 

private void createZipFile() throws IOException {
List<InputStream> inputStreamList = new ArrayList<InputStream>();
for(Resource assetResource:assetResourceList)
{


final Asset asset = DamUtil.resolveToAsset(assetResource);
InputStream in = asset.getRendition(DAM_THUMBNAIL).getStream();
inputStreamList.add(in);
}



try (
ZipOutputStream zos = new ZipOutputStream(
new FileOutputStream("C:\\Project\\TestZip\\assets.zip"));
) {
int count =0;
for(InputStream in:inputStreamList){

ZipEntry ze = new ZipEntry("image"+count++);
zos.putNextEntry(ze);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = in.read(tmp)) != -1){
zos.write(tmp, 0, size);
}
zos.flush();
in.close();
}
// zos.close();
}

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 3

I was able to fix this with the below code.

 

try (
ZipOutputStream zos = new ZipOutputStream(
new FileOutputStream("C:\\Project\\TestZip\\assets.zip"));
) {
int count =0;
for(InputStream in:inputStreamList){

ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedImage jImage = ImageIO.read(in);
ImageIO.write(jImage, "png", os);
byte[] tmp = os.toByteArray();

ZipEntry ze = new ZipEntry("image"+ count++ +".png");
zos.putNextEntry(ze);
//byte[] tmp = new byte[4*1024];
//int size = 0;
//while((size = in.read(tmp)) != -1){
zos.write(tmp);
//}
zos.flush();
in.close();
os.close();
}
// zos.close();

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @P_V_Nair , try this out -

// Get the resource resolver from the current request or service
ResourceResolver resourceResolver = request.getResourceResolver();

// Get the session from the resource resolver
Session session = resourceResolver.adaptTo(Session.class);

// Get the asset manager from the session
AssetManager assetManager = session.getWorkspace().getVersionManager().getAssetManager();

// Get the resource of the image from the DAM path
Resource resource = resourceResolver.getResource("/content/dam/my-image.jpg");

// Adapt the resource to an asset
Asset asset = resource.adaptTo(Asset.class);

// Get the file name of the asset from the metadata
String fileName = asset.getMetadataValue("dc:title");

// Create a temporary file to store the downloaded image
File tempFile = File.createTempFile(fileName, ".jpg");

// Copy the asset content to the temporary file
Files.copy(asset.getOriginal().getStream(), tempFile.toPath());

// Create a zip file to store the zipped image
File zipFile = new File(fileName + ".zip");

// Create a buffered output stream to write to the zip file
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipFile));

// Create a zip output stream to write the zip entries
ZipOutputStream zos = new ZipOutputStream(bos);

// Create a zip entry for the image file
ZipEntry zipEntry = new ZipEntry(fileName + ".jpg");

// Put the zip entry to the zip output stream
zos.putNextEntry(zipEntry);

// Copy the temporary file content to the zip output stream
Files.copy(tempFile.toPath(), zos);

// Close the zip entry
zos.closeEntry();

// Close the zip output stream
zos.close();

// Delete the temporary file
tempFile.delete();

 

 

Avatar

Correct answer by
Level 3

I was able to fix this with the below code.

 

try (
ZipOutputStream zos = new ZipOutputStream(
new FileOutputStream("C:\\Project\\TestZip\\assets.zip"));
) {
int count =0;
for(InputStream in:inputStreamList){

ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedImage jImage = ImageIO.read(in);
ImageIO.write(jImage, "png", os);
byte[] tmp = os.toByteArray();

ZipEntry ze = new ZipEntry("image"+ count++ +".png");
zos.putNextEntry(ze);
//byte[] tmp = new byte[4*1024];
//int size = 0;
//while((size = in.read(tmp)) != -1){
zos.write(tmp);
//}
zos.flush();
in.close();
os.close();
}
// zos.close();