我知道Mule对使用元素的gzip压缩数据有很大的支持。然而,客户现在想zip压缩,因为该文件已被放置在一个FTP作为一个zip压缩文件:(Mule Zip文件并向FTP服务器发送压缩文件
我遇到骡子困难以下情形:
我创建了一个Spring bean,其中一个文件来在我想用ZipOutputStream类压缩这个文件,并通过它向我们的FTP
这是我的流程配置:
<flow name="testFlow" initialState="stopped">
<file:inbound-endpoint path="${home.dir}/out" moveToDirectory="${hip.dir}/out/hist" fileAge="10000" responseTimeout="10000" connector-ref="input"/>
<component>
<spring-object bean="zipCompressor"/>
</component>
<set-variable value="#[message.inboundProperties.originalFilename]" variableName="originalFilename" />
<ftp:outbound-endpoint host="${ftp.host}" port="${ftp.port}" user="${ftp.username}" password="${ftp.password}" path="${ftp.root.out}" outputPattern="#[flowVars['originalFilename']].zip" />
</flow>
这是我zipCompressor的代码:
@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();
}
}
我写了一个单元测试,压缩效果很好。使用正确的名称确实将文件传输到FTP,但zip文件无效,并且通过在NotePad ++中打开它,它只包含原始文件名。
我想我做错了将zip文件传递回骡流,但我现在卡住了,所以任何帮助将不胜感激!
我跑你的流量和zipCompressor的副本。我也收到一个文件,但文件从不压缩。它处于原始状态。 – tbriscoe