2013-02-25 82 views
2

我需要的帮助是知道如何将多个1024字节长的字节数组合成一个字节数组来创建文件。如何将多个字节数组合并为一个来创建文件

我需要这样做,因为我必须将多个文件发送到SAP,但文件必须以1024(byte [1024])的字节数组分割,并且在分割文件后,我将这些文件保存为收集,而我遇到的问题是在SAP中创建文件时这个文件已损坏。我想,当我分割的文件

这个的有我使用分割文件

for (int i = 0; i < attachRaw.Count(); i++) 
     { 
      countLine = attachRaw[i].content.Length/1024; 
      if (attachRaw[i].content.Length % 1024 != 0) 
       countLine++; 
      ZFXX_ATTATTACHMENT_VBA[] attachArray = new ZFXX_ATTATTACHMENT_VBA[countLine];    
      for (int y = 0; y < countLine; y++) 
      { 
       ZFXX_ATTATTACHMENT_VBA attach = new ZFXX_ATTATTACHMENT_VBA(); 
       attach.CONTENT = new byte[i == (countLine - 1) ? (attachRaw[i].content.Length - i * 1024) : 1024]; 
       if (i == (countLine - 1)) 
       { 
        countLine++; 
        countLine--; 
       } 
       if (attachRaw[i].content.Length < 1024) 
       { 
        attach.CONTENT = attachRaw[i].content; 
       } 
       else 
       { 
        attach.CONTENT = FractionByteArray(i * 1024, (i == (countLine - 1) ? attachRaw[i].content.Length : (i * 1024 + 1024)), attachRaw[i].content); 
       } 
       attach.FILE_LINK = attachRaw[i].fileLink; 
       attachmentRaw.Add(attach); 

      } 

     } 


private static byte[] FractionByteArray(int start, int finish, byte[] array) 
    { 
     byte[] returnArray = new byte[finish - start]; 

     for (int i = 0; i < finish - start; i++) 
     { 
      returnArray[i] = array[start + i]; 
     } 
     return returnArray; 
    } 
+0

看一看[阵列在C#合并(http://stackoverflow.com/questions/5395859/is-there-a-function-equivalent-of-php-阵列合并入-C-尖锐)。 – Killrawr 2013-02-25 02:29:23

+0

您是否必须创建一个字节数组,或者只是将多个字节数组写入(追加)到一个文件中?此外,对于'FractionByteArray'函数,可以使用linq的'Take'和'Skip'方法,或使用'Array.Copy'(忽略for循环)。 – Matthew 2013-02-25 02:49:01

+0

我需要只创建一个字节的数组来创建文件,因为在读取文件时创建的原始字节数组被分割成长度为1024的字节数组,并且我想重新创建原始字节数组检查是否存在任何数据丢失,因为在SAP中创建文件时,这个文件会被破坏 – 2013-02-25 02:53:21

回答

7

的方法你可以使用BlockCopy加入所有的阵列放弃任何问题。

喜欢的东西:

private byte[] JoinArrays(IEnumerable<byte[]> arrays) 
    { 
     int offset = 0; 
     byte[] fullArray = new byte[arrays.Sum(a => a.Length)]; 
     foreach (byte[] array in arrays) 
     { 
      Buffer.BlockCopy(array, 0, fullArray, offset, array.Length); 
      offset += array.Length; 
     } 
     return fullArray; 
    } 
相关问题