Reputation: 59
The help I need is to know how can I combine multiples byte array of 1024 bytes length each, into one byte array to create the file.
I need to do this because I have to send multiples files to SAP, but the files must be split in byte arrays of 1024 (byte[1024]) and after I split the files I'm saving this ones into a collection, and the problem I having is when the file is created in SAP this one is corrupt. And I want to discard any problem when I split the files
this ones are the methods I'm using to split the files
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;
}
Upvotes: 2
Views: 4586
Reputation: 43596
You could use BlockCopy
to Join all your arrays.
Something like:
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;
}
Upvotes: 9