cancel
Showing results for 
Search instead for 
Did you mean: 

zip multiple text files

mandar
Explorer
0 Kudos

Hello,

My requirement is to zip multiple text files and zip file should have same name as input file.I have gone through many blogs and have tried implmenting different solutions like payloadzipbean, custom adapter module and OS command, but i did not manage to achieve the result. Could you please help.

former_member190293
Active Contributor
0 Kudos

Hi Praveen!

Recently I've tried to zip my source file using PayloadZipBean. I faced the strange issue: my file inside zip archive was named untitled.xml (original name was OrderData.xml). I've checked bean source code and found out this method:

private static String getFileName(XMBPayload pld, String filekey)
  {
    String filename = null;
    if (filekey == null) {
      filename = pld.getPayloadName();
    } else if (filekey.equals("contentType")) {
      filename = ContentTypeDecoder.getParameter("name", pld.getContentType());
    } else if (filekey.equals("contentDescription")) {
      filename = pld.getContentAttribute("Content-Description");
    } else if (filekey.equalsIgnoreCase("contentDisposition")) {
      filename = ContentTypeDecoder.getParameter("filename", pld.getContentAttribute("Content-Disposition"));
    } else {
      filename = pld.getPayloadName();
    }
    return "untitled" + ContentTypeDecoder.getFileExtension(pld.getContentType());
  }

As you can see, method always returns "untitled" + file extension from ContentType. And further returned value is used as ZipEntry name.

String filename = getFileName(pld, filekey);
      String zfilename = filename + ".zip";
      BinaryBufferOutputStream binout = new BinaryBufferOutputStream();
      ZipOutputStream zout = new ZipOutputStream(binout);
      ZipEntry entry = new ZipEntry(filename);
      zout.putNextEntry(entry);
      zout.write(pldbin.getBytes());
      String zipStatus = "Zip: zipped payload";

Don't you know if there is any way to zip file using PayloadZipBean module preserving original file name?

Regards, Evgeniy.

former_member182412
Active Contributor
0 Kudos

Hi Evgeniy,

I have mentioned all the steps in below blog.

Zip File Using PayloadZipBean Preserving Original File Name Inside The Zip

Regards,

Praveen.

Accepted Solutions (0)

Answers (7)

Answers (7)

mandar
Explorer
0 Kudos

Hello Praveen,

Sorry for late reply. Before trying out your solution, could you please first look at sender and receiver file channel configuration.

Also i am using dummy SIs and namespace for ICO, no ESR objects created.

former_member182412
Active Contributor
0 Kudos

Hi Mandar,

If you configure the sender file channel with additional file option.

Configure receiver channel like below.

Use below java mapping to change the extension from .txt to .zip.

import org.apache.commons.io.IOUtils;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class DynamicFileNameJavaMap extends AbstractTransformation {
	@Override
	public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
			throws StreamTransformationException {
		try {
			DynamicConfiguration dynConfig = transformationInput.getDynamicConfiguration();
			DynamicConfigurationKey FILE_NAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");
			dynConfig.put(FILE_NAME, dynConfig.get(FILE_NAME).replace(".txt", ".zip"));
			IOUtils.copy(transformationInput.getInputPayload().getInputStream(), transformationOutput.getOutputPayload()
					.getOutputStream());
		} catch (Exception e) {
			throw new StreamTransformationException(e.getMessage());
		}
	}
}

Regards,

Praveen.

mandar
Explorer
0 Kudos

Hello Evgeniy,

Yes, i have marked my other files under additional files. I think both files are main payload, as message payload has content of both files.

former_member182412
Active Contributor
0 Kudos

Can you provide screen shot of your sender channel and message monitoring screen shot which shows payloads.

former_member190293
Active Contributor
0 Kudos

Hi Mandar!

Do you set your files besides the first one as additional files?

You should have the first file as main payload and the rest ones as attachments.

Regards, Evgeniy.

mandar
Explorer
0 Kudos

Hello Evgeniy,

Thanks for the reply.

I have tried that already. But it is zipping only one file and not all.

Regards,

Mandar

mandar
Explorer
0 Kudos

Hello Praveen,

Indeed, I need to zip all files into one zip file. For example,

file names are:

000123_ABC.txt

000123_ABCXYZ.txt

Zip file name: 000123_ABC.zip. However getting the correct zip filename is not an issue for me, as i am able to achive it through DynamicConfiguration Class and then using substring function to retrive the required filename in adapter module code.

Also, I have seen content of both files into XI message payload in message monitoring. Still after execution of adapter module, zip file on target directory has only first file.

former_member190293
Active Contributor
0 Kudos

Hi Mandar!

Do you use PayloadZIPBean module in your configuration?

If so, do you use zip.mode = zipALL in module configuration?

Regards, Evgeniy.

mandar
Explorer
0 Kudos

Hello Praveen,

Thanks for your reply.

I have done exactly same. Only difference is i have changed the content type (hardcoded values like .txt and .zip) in adapter module code and then zip the file using zipoutputstream. However then it only zip the single file. My requirement is to zip multiple text files.

former_member182412
Active Contributor
0 Kudos

Hi Mandar,

Can you explain your requirement clearly.

  • Do you need to pick up all files from source directory as single XI message?
  • Do you need to zip all the files into one zip file? if yes you have multiple files what will be the zip file name??

Regards,

Praveen.