In real time integration, especially file based integration, we normally starts from fetching file from a sftp server.
It’s common that the files put on the sftp server is a zipped file with multiple files compressed together.
Following shows how to use a Camel route to fetch a zip file from the sftp server, and after that unzip the file and put to local folders.
Please refer to Spring boot + Camel hello world to see how to setup the project before creating the Route.
The code contains both getting file from a sftp, and fetch from a local folder(commented). It also includes a Choice to do the file filtering and switch to different sub-Routes if required.
package com.zzyan.route;
import org.apache.camel.Predicate;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.ZipFileDataFormat;
import org.springframework.stereotype.Component;
import java.util.Iterator;
//@Component
public class UnzipRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
// Define a Zip File format, since our Zip file have multiple zip files
ZipFileDataFormat zipFile = new ZipFileDataFormat();
zipFile.setUsingIterator(true);
from("timer:hello?period=10s") // Triggered by Timer every 10 seconds
.log("Timer Invoked and the body is ${body}")
.pollEnrich("sftp://USERNAME@HOST/FILEPATH?password=PASSWORD&noop=true")
// following shows pull the file from local c:/data/zipInput
//.pollEnrich("file:c:/data/zipInput?noop=true&readLock=none")
.log("${file:name}")
.unmarshal(zipFile)
.split(bodyAs(Iterator.class)).streaming()
.log("${file:name}")
.choice()
.when(header("CamelFileName").startsWith("Headers"))
.log("This is the Header")
.to("file:c:/data/output")
.otherwise()
.to("file:c:/data/output")
.endChoice()
.end();
}
}