web-dev-qa-db-ja.com

大きなファイルをC#でチャンクに分割するにはどうすればよいですか?

簡単なファイル転送の送信者と受信者のアプリをネットワーク経由で作成しています。私がこれまでに持っているのは、送信者がファイルをバイト配列に変換し、その配列のチャンクを受信者に送信することです。

これは、最大256mbのファイルで機能しますが、それより上の行は次のとおりです。

byte[] buffer = StreamFile(fileName); //This is where I convert the file

Throws a System out of memory exception.

ファイル全体をbyteにロードする代わりに、ファイルをチャンクで読み取り、そのチャンクを書き込む方法を探しています。 FileStreamでこれを行うにはどうすればよいですか?

編集:

申し訳ありませんが、これまでの私のくだらないコードは次のとおりです。

    private void btnSend(object sender, EventArgs e)
    {
        Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


        byte[] fileName = Encoding.UTF8.GetBytes(fName); //file name
        byte[] fileData = null;
        try
        {
             fileData = StreamFile(textBox1.Text); //file
        }
        catch (OutOfMemoryException ex)
        {
            MessageBox.Show("Out of memory");
            return;
        }

        byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //length of file name
        clientData = new byte[4 + fileName.Length + fileData.Length];
        fileNameLen.CopyTo(clientData, 0);
        fileName.CopyTo(clientData, 4);
        fileData.CopyTo(clientData, 4 + fileName.Length);
        clientSock.Connect("172.16.12.91", 9050);
        clientSock.Send(clientData, 0, 4 + fileName.Length, SocketFlags.None);

        for (int i = 4 + fileName.Length; i < clientData.Length; i++)
        {
            clientSock.Send(clientData, i, 1 , SocketFlags.None);
        }

        clientSock.Close();
    }

そして、これが私が受け取る方法です(コードはチュートリアルからのものでした)

   public void ReadCallback(IAsyncResult ar)
    {

        int fileNameLen = 1;
        String content = String.Empty;
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        int bytesRead = handler.EndReceive(ar);
        if (bytesRead > 0)
        {

            if (flag == 0)
            {
                Thread.Sleep(1000);
                fileNameLen = BitConverter.ToInt32(state.buffer, 0);
                string fileName = Encoding.UTF8.GetString(state.buffer, 4, fileNameLen);
                receivedPath = fileName;
                flag++;
            }
                if (flag >= 1)
                {
                    BinaryWriter writer = new BinaryWriter(File.Open(receivedPath, FileMode.Append));
                    if (flag == 1)
                    {
                        writer.Write(state.buffer, 4 + fileNameLen, bytesRead - (4 + fileNameLen));
                        flag++;
                    }
                    else
                        writer.Write(state.buffer, 0, bytesRead);
                        writer.Close();
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReadCallback), state);
                }
        }
        else
        {
            Invoke(new MyDelegate(LabelWriter));
        }

    }

ファイルをバイトに変換する必要がないように、ファイルをチャンクで読み取る方法を本当に知りたいだけです。

これまでの回答に感謝します、私はそれを取得し始めていると思います:D

14
Raphael

小さなバッファでReadを繰り返し呼び出すだけです(私は16Kのようなものを使用する傾向があります)。 Readを呼び出すと、要求よりも小さいの量が読み取られる可能性があることに注意してください。固定チャンクサイズを使用していて、メモリ内にチャンク全体が必要な場合は、もちろんそのサイズの配列を使用できます。

ファイルの状態を知らなければsendingコードを構造化する方法について多くのアドバイスを与えるのは難しいですが、次のようになる可能性があります。

byte[] chunk = new byte[MaxChunkSize];
while (true)
{
    int index = 0;
    // There are various different ways of structuring this bit of code.
    // Fundamentally we're trying to keep reading in to our chunk until
    // either we reach the end of the stream, or we've read everything we need.
    while (index < chunk.Length)
    {
        int bytesRead = stream.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0)
        {
            break;
        }
        index += bytesRead;
    }
    if (index != 0) // Our previous chunk may have been the last one
    {
        SendChunk(chunk, index); // index is the number of bytes in the chunk
    }
    if (index != chunk.Length) // We didn't read a full chunk: we're done
    {
        return;
    }
}

もし私がもっと目覚めていたら、おそらくこれを書くためのもっと読みやすい方法を見つけるでしょうが、それは今のところうまくいくでしょう。 1つのオプションは、中央のセクションから別のメソッドを抽出することです。

// Attempts to read an entire chunk into the given array; returns the size of
// chunk actually read.
int ReadChunk(Stream stream, byte[] chunk)
{
    int index = 0;
    while (index < chunk.Length)
    {
        int bytesRead = stream.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0)
        {
            break;
        }
        index += bytesRead;
    }
    return index;
}
16
Jon Skeet
var b = new byte[1<<15]; // 32k
while((count = inStream.Read(b, 0, b.Length)) > 0)
{
  outStream.Write(b, 0, count);
}
2
Talljoe