2011-09-28 29 views
3

我有两个AFP文件,我想将它们连接在一起,我该如何实现这一点。我已经编写了Java代码来连接它们,使用BufferedInputStream和BufferedOutputStream,结果AFP格式不正确。我甚至尝试使用Linux猫,但产生相同的错误结果。请帮忙。我不认为这个问题是我的Java代码,但我发布了下面的代码以防万一。如何将两个AFP文件连接在一起

注意:一个奇怪的是,如果我切换串联的顺序,那么它会产生正确的格式输出。例如,如果我将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) { 

     } 
    } 
} 

回答

2

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 

这将解决这个问题。

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

然后,只需加载它们都写它在默认情况下通过的Xenos D2E软件生成的文件

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

如果你看看我的代码,它会在'byte []'中读取,并写出'byte []',这正是你的代码正在做的事情。不过谢谢。 –

0

为了搭载上面有关重新排序文件内容的答案,这里是我在DST输出(一个非常大的打印&邮件公司)工作时建立的一个建议工具。

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

相关问题