2

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;
    }
Jose Retana
  • 59
  • 3
  • 9
  • Have a look at [Array Merge in C#](http://stackoverflow.com/questions/5395859/is-there-a-function-equivalent-of-php-array-merge-in-c-sharp). – classicjonesynz Feb 25 '13 at 02:29
  • Do you have to make one byte array, or can you just write (append) multiple byte arrays to a file? Also, for your `FractionByteArray` function, you can use linq's `Take` and `Skip` methods, or use `Array.Copy` (to omit the for loop). – Matthew Feb 25 '13 at 02:49
  • I need to make just one byte array to create the file, because the original byte array that is created when the file is read it, is split it in byte arrays of 1024 length, and i want to re-created the original byte array to check is there if there is any loss of data, because the when the file is create in SAP this one is currupt – Jose Retana Feb 25 '13 at 02:53

1 Answers1

8

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;
    }
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110