我需要的帮助是知道如何将每个1024字节长度的多字节数组合成一个字节数组来创建文件。
我需要这样做,因为我必须将多个文件发送到SAP,但文件必须以1024的字节数组(字节[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 :(得分: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;
}