How do I concatenate two AFP files together

橙三吉。 提交于 2020-02-03 10:10:28

问题


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) {

        }
    }
}

回答1:


AFP generated by Xenos D2e software by default contain inline resources at the top of the pages, like this

AFP file 1 resources       AND        AFP file 2 resources
AFP file 1 content                    AFP file 2 content

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

AFP file 1 resources
AFP file 1 content
AFP file 2 resources ------> resources should not be in the middle page
AFP file 2 content

so the solution is to export all resources to an external file, then you can concatenated as follow

AFP file resources
AFP file 1 content
AFP file 2 content

This will fix the problem.




回答2:


From example depot, here's a quick function to get the bytes from a file:

public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}

Then just load them both and write it out to a file

try {
    FileOutputStream fos = new FileOutputStream("C:\\dev\\harry\\ETCC_data\\output.afp");
    byte[] bytes1 = getBytesFromFile(new File(filePath1));
    byte[] bytes2 = getBytesFromFile(new File(filePath2));
    fos.write(bytes1);
    fos.write(bytes2);
    fos.flush(); 
    fos.close(); 
}
catch(FileNotFoundException ex) { System.out.println("FileNotFoundException : " + ex); }
catch(IOException ioe) { System.out.println("IOException : " + ioe); }



回答3:


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.



来源:https://stackoverflow.com/questions/7589075/how-do-i-concatenate-two-afp-files-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!