如何将两个AFP文件连接在一起

3
我有两个AFP文件,想要将它们连接起来,我该如何实现?我已经写了Java代码来连接它们,使用BufferedInputStream和BufferedOutputStream,但结果的AFP格式不正确。我甚至尝试使用Linux的cat命令,但结果也是错误的。请帮忙解决。我不认为问题出在我的Java代码上,但我还是把代码附在下面以备不时之需。
注意:一个奇怪的事情是,如果我交换连接顺序,那么输出就会是正确的格式。例如,如果我先连接A.afp然后B.afp,那么输出就会混乱,但如果我先连接B.afp,然后再连接A.afp,那么输出就会是正确的格式。但我需要A.afp出现在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) {

        }
    }
}
3个回答

2
默认情况下,由Xenos D2e软件生成的AFP包含页面顶部的内联资源,如下所示。
AFP file 1 resources       AND        AFP file 2 resources
AFP file 1 content                    AFP file 2 content

然而,当我尝试将这两个文件连接在一起时,一些资源会处于连接文件的中间位置,从而搞乱结果。
AFP file 1 resources
AFP file 1 content
AFP file 2 resources ------> resources should not be in the middle page
AFP file 2 content

因此解决方案是将所有资源导出到外部文件,然后您可以按以下方式进行串联。
AFP file resources
AFP file 1 content
AFP file 2 content

这将解决问题。

如何获取AFP文件的文件数量?这样我们就可以在合并两个文件后进行验证。 - JRA

0
从示例仓库中,这里有一个快速获取文件字节的函数:
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;
}

那就把它们都加载进来,然后写入文件中

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); }

如果您查看我的代码,它确实读入byte[],并写出byte[],这与您的代码所做的工作非常相似。感谢你的关注。 - Thang Pham

0
为了借鉴上面有关重新排序文件内容的答案,这里提供一个我在DST Output(一家非常大的印刷和邮寄公司)工作时制作的建议工具。
我制作了一个名为“afp_dd”的实用程序,它类似于Unix中的“dd”命令,允许我在命令行上指定记录跳过和计数值,以提取在记录边界上中断的子文件(标准dd程序期望固定大小的记录,而不是带有每个记录开头附近的长度指示器的可变大小记录)。我可以将子文件通过我们的AFP转储程序进行管道传输以进行检查,然后使用输出子文件作为输入来创建更改后的文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接