与FileStream中的写作有什么不同?

时间:2017-09-27 07:31:10

标签: c# arrays filestream

当我搜索关于使用SharpZipLib解压缩文件的方法时,我发现了很多像这样的方法:

public static void TarWriteCharacters(string tarfile, string targetDir)
{
    using (TarInputStream s = new TarInputStream(File.OpenRead(tarfile)))
    {
        //some codes here

        using (FileStream fileWrite = File.Create(targetDir + directoryName + fileName))
        {                          
            int size = 2048;
            byte[] data = new byte[2048];
            while (true)
            {
                size = s.Read(data, 0, data.Length);
                if (size > 0)
                {
                    fileWrite.Write(data, 0, size);
                }
                else
                {
                    break;
                }
            }
            fileWrite.Close();
        }
    }
}

格式FileStream.Write是:

  

FileStream.Write(byte [] array,int offset,int count)

现在我尝试将读写部分分开,因为我想使用线程来加速写入函数中的解压缩率,并使用动态数组byte[]int[]来存放文件的数据和大小如下

读:

public static void TarWriteCharacters(string tarfile, string targetDir)
{
    using (TarInputStream s = new TarInputStream(File.OpenRead(tarfile)))
    {
        //some codes here

        using (FileStream fileWrite= File.Create(targetDir + directoryName + fileName))
        {                          
            int size = 2048;

            List<int> SizeList = new List<int>();
            List<byte[]> mydatalist = new List<byte[]>();

            while (true)
            {
                byte[] data = new byte[2048];
                size = s.Read(data, 0, data.Length);

                if (size > 0)
                {
                    mydatalist.Add(data);
                    SizeList.Add(size);
                }
                else
                {
                    break;
                }
            }
            test = new Thread(() =>
                FileWriteFun(pathToTar, args, SizeList, mydatalist)
            );
            test.Start();
            streamWriter.Close();
        }
    }
}

写:

public static void FileWriteFun(string pathToTar , string[] args, List<int> SizeList, List<byte[]> mydataList)
{
    //some codes here

    using (FileStream fileWrite= File.Create(targetDir + directoryName + fileName))
    {
        for (int i = 0; i < mydataList.Count; i++)
        {
            fileWrite.Write(mydataList[i], 0, SizeList[i]);
        }
        fileWrite.Close();
    }
}

修改

  

(1)byte[] data = new byte[2048]进入while循环以将数据分配给新数组。

     

(2)因为int范围

而将int[] SizeList = new int[2048]更改为List<int> SizeList = new List<int>()

1 个答案:

答案 0 :(得分:0)

因为在流上读取只保证返回一个字节(通常它会更多,但你不能每次都依赖于完整的请求长度),理论上你的解决方案在2048字节之后就会失败,因为你的SizeList只能持有2048个条目。

您可以使用列表来保存尺寸。

或者使用MemoryStream而不是发明自己的。

但两个主要问题是: 1)您继续读取相同的字节数组,覆盖以前读取的数据。将数据字节数组添加到mydatalist时,必须将数据分配给新的字节数组。 2)在第二个线程写完之前关闭你的流。

通常,线程很难处理,只应在您知道提高性能的地方使用。简单地读取和写入数据通常是性能上的IO绑定,而不是cpu绑定,因此引入第二个线程只会带来很小的性能损失并且速度没有提高。您可以使用多线程来确保并发读/写操作,但如果您坚持第一个解决方案,很可能磁盘缓存会为您执行此操作 - 如果不是,则使用async比多线程更容易实现此目的。