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

发布于 2024-12-07 02:26:54 字数 2134 浏览 1 评论 0原文

我有两个 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) {

        }
    }
}

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

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凝望流年 2024-12-14 02:26:54

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 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.

蘸点软妹酱 2024-12-14 02:26:54

从示例仓库中,这是一个从文件中获取字节的快速函数:

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

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); }
拧巴小姐 2024-12-14 02:26:54

为了借鉴上面关于重新排序文件内容的答案,这里是我在 DST Output(一家非常大的打印和邮件公司)工作时制作的建议工具。

我制作了一个名为“afp_dd”的实用程序,它的工作方式类似于 unix“dd”命令,允许我在命令行上指定记录跳过和计数值,以提取超出记录边界的子文件(标准 dd 程序需要固定大小)记录,而不是在每个记录开头附近带有长度指示符的可变大小)。我可以通过 AFP 转储程序传输子文件来检查它们,然后使用输出子文件作为输入来创建更改的文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文