I have two AFP files and I want to concatenate them together, how can I accomplish this. I have written java code to concatenate them, using BufferedInputStream and BufferedOutputStream and the result AFP is not correctly format. I even try to use linux cat but yield the same incorrect result. Please help. I dont think the problem is my java code, but I post the code below just in case.
NOTE: One strange thing is that if I switch the order of the concatenation then it yield the right format output. For example if I concatenate A.afp then B.afp, then the output is messed up, but if I concatenate B.afp, then A.afp then it yield correct format result. But I need A.afp to appear before B.afp
public static void main(String[] args) {
String filePath1 = "C:\\dev\\harry\\ETCC_data\\3199_FI_20_20110901143009.afp";
String filePath2 = "C:\\dev\\harry\\ETCC_data\\3643_FI_49_20110901143006.afp";
ConcatenateMain cm = new ConcatenateMain();
cm.concate(filePath1, filePath2);
}
private void concate(String filePath1, String filePath2){
BufferedInputStream bis1 = null;
BufferedInputStream bis2 = null;
FileInputStream inputStream1 = null;
FileInputStream inputStream2 = null;
FileOutputStream outputStream = null;
BufferedOutputStream output = null;
try{
inputStream1 = new FileInputStream(filePath1);
inputStream2 = new FileInputStream(filePath2);
bis1 = new BufferedInputStream(inputStream1);
bis2 = new BufferedInputStream(inputStream2);
List<BufferedInputStream> inputStreams = new ArrayList<BufferedInputStream>();
inputStreams.add(bis1);
inputStreams.add(bis2);
outputStream = new FileOutputStream("C:\\dev\\harry\\ETCC_data\\output.afp");
output = new BufferedOutputStream(outputStream);
byte [] buffer = new byte[BUFFER_SIZE];
for(BufferedInputStream input : inputStreams){
try{
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1)
{
output.write(buffer, 0, bytesRead);
}
}finally{
input.close();
}
}
}catch(IOException e){
}finally{
try {
output.close();
} catch (IOException e) {
}
}
}
To piggyback off the answer above about re-ordering the contents of the files, here is a suggested tool I made back when I worked at DST Output (a very large print & mail company).
I made a utility called "afp_dd", which worked like the unix "dd" command, allowing me to specify record skip and count values on the command line to extract sub-files which broke on record boundaries (the standard dd program expects fixed size records, rather than variable size with a length indicator near the beginning of each record). I could pipe the sub-files through our AFP dump program to check them, then use the output subfiles as input to create altered files.
AFP generated by Xenos D2e software by default contain inline resources at the top of the pages, like this
However when I try to concatenate these two file together, some resources will be at the middle of the concatenated file, hence mess up the result
so the solution is to export all resources to an external file, then you can concatenated as follow
This will fix the problem.
From example depot, here's a quick function to get the bytes from a file:
Then just load them both and write it out to a file