Mule Zip文件并向FTP服务器发送压缩文件

我知道Mule非常支持使用该元素对数据进行gzip压缩。 但是客户端现在想要压缩压缩,因为文件必须作为zip压缩文件放在FTP上:(

我遇到以下情况的mule子困难:

我创建了一个文件进来的Spring bean。我想使用ZipOutputStream类压缩这个文件并将它传递给我们的ftp。

这是我的流程配置:

        

这是我的zip压缩器的代码:

 @Component public class ZipCompressor implements Callable { private static final Logger LOG = LogManager.getLogger(ZipCompressor.class.getName()); @Override @Transactional public Object onCall(MuleEventContext eventContext) throws Exception { if (eventContext.getMessage().getPayload() instanceof File) { final File srcFile = (File) eventContext.getMessage().getPayload(); final String fileName = srcFile.getName(); final File zipFile = new File(fileName + ".zip"); try { // create byte buffer byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); FileInputStream fis = new FileInputStream(srcFile); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(srcFile.getName())); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); // close the InputStream fis.close(); // close the ZipOutputStream zos.close(); } catch (IOException ioe) { LOG.error("Error creating zip file" + ioe); } eventContext.getMessage().setPayload(zipFile); } return eventContext.getMessage(); } } 

我写了一个unit testing,压缩效果很好。 文件确实已传输到具有正确名称的FTP,但zip文件无效,在NotePad ++中打开它只包含原始文件名。

我认为将zip文件传回mule子流程时我做错了,但我现在卡住了所以任何帮助都会非常感激!

我为此实现了变压器

  package com.test.transformer; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.mule.api.MuleMessage; import org.mule.api.transformer.TransformerException; import org.mule.transformer.AbstractMessageTransformer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ZipTransformer extends AbstractMessageTransformer { private static final Logger log = LoggerFactory.getLogger(ZipTransformer.class); public static final int DEFAULT_BUFFER_SIZE = 32768; public static byte[] MAGIC = { 'P', 'K', 0x3, 0x4 }; public ZipTransformer() { registerSourceType(InputStream.class); registerSourceType(byte[].class); } public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { Object payload = message.getPayload(); try{ byte[] data; if (payload instanceof byte[]) { data = (byte[]) payload; } else if (payload instanceof InputStream) { data = IOUtils.toByteArray((InputStream)payload); } else if (payload instanceof String) { data = ((String) payload).getBytes(outputEncoding); } else { data = muleContext.getObjectSerializer().serialize(payload); } return compressByteArray(data); }catch (Exception ioex) { throw new TransformerException(this, ioex); } } public Object compressByteArray(byte[] bytes) throws IOException { if (bytes == null || isCompressed(bytes)) { if (logger.isDebugEnabled()) { logger.debug("Data already compressed; doing nothing"); } return bytes; } if (logger.isDebugEnabled()) { logger.debug("Compressing message of size: " + bytes.length); } ByteArrayOutputStream baos = null; ZipOutputStream zos = null; try { baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); zos = new ZipOutputStream(baos); zos.putNextEntry(new ZipEntry("test.txt")); zos.write(bytes, 0, bytes.length); zos.finish(); zos.close(); byte[] compressedByteArray = baos.toByteArray(); baos.close(); if (logger.isDebugEnabled()) { logger.debug("Compressed message to size: " + compressedByteArray.length); } return compressedByteArray; } catch (IOException ioex) { throw ioex; } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(baos); } } public boolean isCompressed(byte[] bytes) throws IOException { if ((bytes == null) || (bytes.length < 4 )) { return false; } else { for (int i = 0; i < MAGIC.length; i++) { if (bytes[i] != MAGIC[i]) { return false; } } return true; } } } 

用它作为

  

截至目前,将文件名设置为test.txt。 你可以改变使用任何属性或变量。

希望这可以帮助。

一种更简单的方法是在mule中使用gzip变换器来压缩文件。 请注意,您必须通过xml执行此操作。

  

在ZipTransformer构造函数中,不推荐使用以下内容。

 registerSourceType(InputStream.class); registerSourceType(byte[].class); 

改为使用它:

 registerSourceType(DataTypeFactory.create(InputStream.class)); registerSourceType(DataTypeFactory.create(byte[].class));