简体   繁体   English

如何将字节数组列表写入文件C#

[英]How to write a List of Byte Arrays to a file c#

I have an uploader that I use to split files up and upload them to my sql server. 我有一个上传器,用于拆分文件并将其上传到我的sql服务器。 I then download each chunk and create a temporary file. 然后,我下载每个块并创建一个临时文件。 I am trying to write a list of byte arrays(byte[]) into one file to recreate that file. 我正在尝试将字节数组(byte [])的列表写入一个文件以重新创建该文件。 This is because when I try to read the list of byte arrays into one array, I get an OutOfMemory exception. 这是因为当我尝试将字节数组的列表读取到一个数组中时,出现了OutOfMemory异常。 I am just wondering what the best way to do this is. 我只是想知道什么是最好的方法。 Thanks! 谢谢!

 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        int currentRowSelection = fUS_FileDataGridView.CurrentCell.RowIndex;
        var totalNumber = fUS_FileDataGridView.Rows[currentRowSelection].Cells[6].Value;
        for (int i = 1; i < 149; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    list_.Add(reader.ReadBytes((int)stream1.Length));
                    stream1.Close();
                    stream1.Dispose();
                    reader.Close();
                    reader.Dispose();

                }
            }
        }
        //array_ = list_.SelectMany(a => a).ToArray();

        filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
        foreach (byte[] bytes in list_)
        {
            var doc = System.Text.Encoding.Default.GetString(bytes);
            string textToAdd1 = bytes.ToString();
            try
            {
                using (FileStream fs = File.Create(filePaths_))
                using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
                {
                    writer.Write(textToAdd1);
                    writer.Close();
                    writer.Dispose();
                }
            }
            finally
            {
            }
        }
    }

Update: my question is different from the others I have found because I cannot put my list of byte arrays into a single array to write a file. 更新:我的问题与我发现的其他问题不同,因为我无法将字节数组列表放入单个数组中以写入文件。 I am currently only getting a 1 KB file out of my code where I should be getting a 100KB file. 我目前仅从我的代码中得到1 KB文件,而我应该得到100KB文件。

Update 2: The code below makes much more sense but now I am getting a "stream was not writable error" 更新2:下面的代码更有意义,但现在我收到“流不可写错误”

filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
        using (FileStream fs = File.Create(filePaths_))
        for (int i = 0; i < 151; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
                    {
                        writer.Write(reader);
                    }
                }
            }
        } 

If the problem is out of memory then you should think about how to reduce the amount of memory being used. 如果问题是内存不足,则应考虑如何减少正在使用的内存量。

I don't know what your requirements are but based on the code you provided you could do all the writing inside your first foreach loop. 我不知道您的要求是什么,但是根据您提供的代码,您可以在第一个foreach循环内完成所有编写工作。 This way you are only loading one file at a time and GC will free up the memory once you are done with each file. 这样,您一次只加载一个文件,一旦处理完每个文件,GC就会释放内存。

    using (FileStream fs = File.AppendText(filePaths_)) 
    {
        for (int i = 1; i < 149; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    //list_.Add(reader.ReadBytes((int)stream1.Length));
                    //Instead of adding that to list, write them to disk here
                    //fs.Write(...)
                    //...


                    stream1.Close();//No need for this, using is going to call it. 
                    stream1.Dispose();//No need for this, using is going to call it. 
                    reader.Close();//No need for this, using is going to call it. 
                    reader.Dispose();//No need for this, using is going to call it. 

                }
            }
        } 
     }

If I right understand what do you want: 如果我正确理解您想要什么:

// Fill list_
List<byte[]> list_ = null;
// ....

string filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
// Create FileStream and BinaryWriter
using (var fs = File.OpenWrite(filePaths_)){
    using (var bw = new BinaryWriter(fs)){
        foreach (var bytes in list_)
            bw.Write(bytes); // Write each byte array to the stream
    }
}

Updated. 更新。 You can do it in a better way, direct copy one stream to another: 您可以采用更好的方法,将一个流直接复制到另一个流:

string filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
// Result FileStream and BinaryWriter
using (var fs = File.OpenWrite(filePaths_))
{
    for (int i = 1; i < 149; i++)
    {
        using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
        {
            // Just copy stream1 to fs
            stream1.CopyTo(fs);
        }
    }
}

It sounds like what you effectively want to do is append each byte array to the existing file. 听起来您实际上想要做的是将每个字节数组追加到现有文件中。 So I think at the time you want to write each individual array to the file you would simply open the filestream using FileMode.Append (instead of FileMode.Open). 因此,我认为您想将每个单独的数组写入文件时,只需使用FileMode.Append (而不是FileMode.Open)打开文件流即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM