1

Because the maximum value of a byte array is 2GB, lets say i have a larger file and i need to convert it to a byte array. Since i can't hold the whole file, how should i convert it into two?

I tried:

long length = new System.IO.FileInfo(@"c:\a.mp4").Length;
int chunkSize = Convert.ToInt32(length / 2); 


byte[] part2;
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
    part2 = new byte[chunkSize];            // create buffer
    fileStream.Read(part2, 0, chunkSize);
}
finally
{
    fileStream.Close();
}

byte[] part3;
fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
    part3 = new byte[chunkSize];            // create buffer
    fileStream.Read(part3, 5, (int)(length - (long)chunkSize));
}
finally
{
    fileStream.Close();
}

but it's not working.

Any ideas?

6
  • 3
    Read this Commented Mar 29, 2018 at 15:24
  • do you need to hold entire file in memory ? Commented Mar 29, 2018 at 15:26
  • When you need to do something with a large file you should find an approach that doesn't try to read the entire file into memory if you can help it. Commented Mar 29, 2018 at 15:26
  • @Tigran I can't think of another option.. Commented Mar 29, 2018 at 15:27
  • 1
    @AaYy Well what do you plain to do with those byte arrays after you have them in memory? There might be an approach that doesn't require the entire file in memory. Chunking it up like that would break any valid reason anyway. This is basically a XY Problem Commented Mar 29, 2018 at 15:29

3 Answers 3

3

You can use a StreamReader to read in file too large to read into a byte array

const int max = 1024*1024;

public void ReadALargeFile(string file, int start = 0)
{
    FileStream fileStream = new FileStream(file, FileMode.Open,FileAccess.Read);
    using (fileStream)
    {
        byte[] buffer = new byte[max];
        fileStream.Seek(start, SeekOrigin.Begin);
        int bytesRead = fileStream.Read(buffer, start, max);
        while(bytesRead > 0)
        {
            DoSomething(buffer, bytesRead);
            bytesRead = fileStream.Read(buffer, start, max);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So what exactly is the array that holds my data? is it buffer?
buffer holds data between start and start + max - 1. Documentation for FileStream.Read
2

If you are working with extremely large files, you should use MemoryMappedFile, which maps a physical file to a memory space:

using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\path\to\big.file"))
{
    using (var accessor = mmf.CreateViewAccessor())
    {
        byte myValue = accessor.ReadByte(someOffset);
        accessor.Write((byte)someValue);
    }
}

See also: MemoryMappedViewAccessor

You can also read/write chunks of the file with the different methods in MemoryMappedViewAccessor.

Comments

0

This was my solution:

byte[] part1;
byte[] part2;
bool odd = false;
int chunkSize = Convert.ToInt32(length/2);


if (length % 2 == 0)
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize];
}
else
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize + 1];
    odd = true;
}

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
using (fileStream)
{
    fileStream.Seek(0, SeekOrigin.Begin);
    int bytesRead = fileStream.Read(part1, 0, chunkSize);
    if (odd)
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize + 1);
    }
    else
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize);
    }
}

1 Comment

Why is your offset always 0? FortyTwo has a more generic and better solution. Re-usability and abstraction should be kept in mind while programming with an OOP language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.