cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI - How to Unzip Multiple Files in CPI

jose_augastine3
Active Participant

Hi,

I am receiving a zip file which contains 1 xml file and 1 pdf file.

So standard Decoder Zip Decompression wont work since it has more than 1 file

Any Groovy/Java script available to handle this

Thanks in advance

Accepted Solutions (0)

Answers (2)

Answers (2)

r_herrmann
Active Contributor
0 Kudos

Hi Jose,

you can use a Groovy script to unzip the files. I used this scripts in the past:

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.zip.ZipInputStream
import java.util.zip.ZipEntry


Message processData(Message message) {
	
	
	//Read your zip file to byte array
	def pkgBytes = ...
	
	//Get the ZIP listing
	def zipEntries = extractZipEntries(pkgBytes)
	
	//do what ever you want with the entries
	//e.g. pass entry name to readEntryFromZipAsString(...)
	
    return message
}


private extractZipEntries(byte[] content) throws IOException {
    def entries = [];
    ZipInputStream zi = null;
    try {
        zi = new ZipInputStream(new ByteArrayInputStream(content));
        ZipEntry zipEntry = null;
        while ((zipEntry = zi.getNextEntry()) != null) {
            entries.add(zipEntry);
        }
    } finally {
        if (zi != null) {
            zi.close();
        }
    }
    return entries;
}


private readEntryFromZipAsString(byte[] content, def fileName){
	StringBuilder s = new StringBuilder()
	byte[] buffer = new byte[1024]
	int read = 0
    ZipInputStream zi = null
    try {
        zi = new ZipInputStream(new ByteArrayInputStream(content))
        ZipEntry zipEntry = null
        while ((zipEntry = zi.getNextEntry()) != null) {
            if (zipEntry.name == fileName){
            	while ((read = zi.read(buffer, 0, 1024)) >= 0) {
		        	s.append(new String(buffer, 0, read))
		    	}
            }
        }
    } finally {
        if (zi != null) {
            zi.close()
        }
    }
    return s.toString()
}
jose_augastine3
Active Participant
0 Kudos

Hi Raffael,

I was able to unzip file. Not able to read the same. Could you please help ?

I am getting xml and pdf file in zip file, which is unzipped.

def byte[] bytes = message.getBody(byte[].class)

def zipEntries = extractZipEntries(bytes)

Now how to read both files ?

How to pass entry name to readEntryFromZipAsString(...) ?

Thanks in advance,

r_herrmann
Active Contributor
0 Kudos

Hi Jose,

The variable zipEntries from your example contains a list of zip entries. A ZipEntry is an metadata object which contains useful information regarding the file which is saved inside a zip.

To read the actual file, pass the filename to the readEntryFromZipAsString function like:

(Example shows how to read the first file. Attention this should work for the XML file. For the PDF you may adjust the read function, because the PDF should be read as byte[] and not as String)

def myfileContent = readEntryFromZipAsString(bytes, zipEntries[0].getName())
0 Kudos

Hi Jose,

There is a similar requirement where script is required and its attached in the answer.

https://answers.sap.com/questions/391156/error-while-unzipping-files-using-groovy-script.html

hope this helps !

thanks and regards,

Praveen T