function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohammed AzarudeenMohammed Azarudeen 

How do I Loop Through All the Images in Zip file in Controller

I have one static resource zip file, which has N number of Images. So I don't know the exact number of images are present in zip file.
I just want loop through all the images in zip file. How can I achieve this?. Any help is appreciable.
My VFP page is,

        
<apex:page Controller="repeatCon">
<body style="font-size:10pt; font-family:sans-serif">
        <table>
            <td style="font-family:'Segoe UI', Tahoma, sans-serif;">
                <apex:repeat value="{!imageList}" var="img">
                    <td><apex:image url="{!URLFOR($Resource.MinionsDemo,img)}" height="200" width="200" /><br/></td>
                </apex:repeat>
            </td>
        </table>
    </body>
</apex:page>

and my controller class is,
 
public class repeatCon {
    public List<string> getimageList(){
        List<String> imgstr = new List<String>();
        for(integer i = 1; i <= zipfileimagesize; i++){
            imgstr.add('Minion/'+i+'.jpg');
        }
    return imgstr;
    }
}
in the loop how do I give the size of images present in zip file
 
bob_buzzardbob_buzzard
You may be able to do this by pulling files from the zip as PageReferences. Andy Fawcett introduced this in his blog post : http://andyinthecloud.com/2012/11/04/handling-office-files-and-zip-files-in-apex-part-1/

The concept is :
 
PageReference somefileRef = 
   new PageReference('/resources/myzip/folder/file.js');
Blob contentAsBlob = somefileRef.getContent();
String contentAsText = contextAsBlob.toString();

However, getContent calls are counted as callouts so you may need multiple requests to figure out how many files you have. I suspect the only way that this will really scale will be a batch job that processes the file, figures out how many files are present and persists this in a custom object or setting.
 
Pedro I Dal ColPedro I Dal Col
Install the Zipex library. It's open source - you can get it here:  https://github.com/pdalcol/Zippex

Then you can use this code in your controller to read the name of the .jpg files inside your zip file:
public class repeatCon {
    public List<string> getimageList(){
    
        StaticResource staticRes = [SELECT Body FROM StaticResource WHERE Name = 'MinionsDemo'];
        Zippex minionsDemoZip = new Zippex(staticRes.Body);
        List<String> imgstr = new List<String>();
        for(String imgFileName : minionsDemoZip.getFileNames()){
            if(imgFileName.endsWith('.jpg')){
                imgstr.add(imgFileName);
            }
        }
        return imgstr;
    }
}