C# 是否有其他方法将数据写入文件?

C# 是否有其他方法将数据写入文件?,c#,C#,我正在做一个关于解压缩tar或tar.gz文件的项目,我访问了很多网站,我已经完成了这些功能,但是现在,我需要研究加速这个项目的方法。 大多数使用TarInputStream打开和读取压缩文件,使用streamWriter将压缩文件中的数据写入新的创建文件(空文件),我使用streamWriter与线程结合 我的选择是使用每个线程捕获压缩文件中的数据,一个线程一个文件(如果压缩包含很多文件),所有线程一起运行,或者给它一个线程并忽略它,但是当我运行project.exe时,它将显示此消息 进程无

我正在做一个关于解压缩
tar
tar.gz
文件的项目,我访问了很多网站,我已经完成了这些功能,但是现在,我需要研究加速这个项目的方法。 大多数使用
TarInputStream
打开和读取压缩文件,使用
streamWriter
将压缩文件中的数据写入新的创建文件(空文件),我使用
streamWriter
与线程结合

我的选择是使用每个线程捕获压缩文件中的数据,一个线程一个文件(如果压缩包含很多文件),所有线程一起运行,或者给它一个线程并忽略它,但是当我运行project.exe时,它将显示此消息

进程无法访问该文件,因为其他进程正在使用该文件

所以,我认为streamWriter在线程安全方面可能存在一些问题

这是我的密码

//write the data from compress file into new file 
    public static void StreamWriterFun(string pathToTar , string[] args, List<byte[]> mydata)
    {
        string[] str = new string[3];
        for (int i = 0; i < args.Length; i++)
        {
            str[i] = args[i];
        }
    string directoryName = "";

    long size = 0;
    if (pathToTar != "")
        directoryName = Path.GetDirectoryName(pathToTar) + "\\";

    string fileName = Path.GetFileName(pathToTar);

    Directory.CreateDirectory(str[1] + "\\" + directoryName);

    if (fileName != "")
    {
        if ((File.Exists(str[1] + "\\" + directoryName + fileName)) || (!File.Exists(str[1] + "\\" + directoryName + fileName)))
        {
            string test = "";
            using (FileStream streamWriter = File.Create(str[1] + "\\" + directoryName + fileName))
            {
                for (int i = 0; i < mydata.Count; i++)
                {
                    streamWriter.Write(mydata[i], 0, mydata[i].Length);
                }
                streamWriter.Close();
            }
        }
    }
}

public static void TGZWriteCharacters(string[] args)
{
    //args[] define
    string[] str = new string[3];
    for (int i = 0; i < args.Length; i++)
    {
        str[i] = args[i];
    }
    try
    {
        Program file = new Program();
        file.ExtractGZipSample(str[0], str[1]);

        string[] filePaths = Directory.GetFiles(@str[1], "*.tar", SearchOption.TopDirectoryOnly);
        str[2] = filePaths[0];

        if (str[1] == "")
            str[1] = Directory.GetCurrentDirectory();
        if (!str[1].EndsWith("\\"))
            str[1] = str[1] + "\\";// C:\Tar.gz

        ArrayList myList = new ArrayList();

        using (TarInputStream s = new TarInputStream(File.OpenRead(str[2])))// C:\python.tar
        {
            TarEntry theEntry;

            while ((theEntry = s.GetNextEntry()) != null)
            {
                myList.Add(theEntry.Name);
            }
            s.Close();
        }

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        using (TarInputStream s = new TarInputStream(File.OpenRead(str[2])))
        {
            TarEntry theEntry;

            String[] myArray = (String[])myList.ToArray(typeof(string));

            for (int i = 0; i < myArray.Length-1; i++)
            {
                Thread[] test = new Thread[myArray.Length];

                while ((theEntry = s.GetNextEntry()) != null)
                {

                    string directoryName = "";
                    string pathToTar = "";
                    pathToTar = theEntry.Name;

                    if (pathToTar != "")
                        directoryName = Path.GetDirectoryName(pathToTar) + "\\";

                    string fileName = Path.GetFileName(pathToTar);

                    Directory.CreateDirectory(str[1] + directoryName);

                    if (fileName != "")
                    {
                        if ((File.Exists(str[1] + directoryName + fileName)) || (!File.Exists(str[1] + directoryName + fileName)))
                        {
                            using (FileStream streamWriter = File.Create(str[1] + directoryName + fileName))
                            {
                                int size = 2048;
                                int count = 0;
                                List<byte[]> mydatalist = new List<byte[]>();

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

                                    if (size > 0)
                                    {
                                        mydatalist.Add(data);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                test[i] = new Thread(() =>
                                   StreamWriterFun(pathToTar, args, mydatalist)
                                );
                                test[i].Start();
                                streamWriter.Close();
                            }
                        }
                    }   
                }
            }
            s.Close();
            System.IO.File.Delete(str[2]); // delete the tar file
        }
        sw.Stop();
        string result1 = sw.Elapsed.TotalMilliseconds.ToString();
        Console.WriteLine(result1);
    }
    catch (Exception)
    {
        Console.WriteLine("error1");
    } 
}
//将压缩文件中的数据写入新文件
公共静态void StreamWriterFun(字符串pathtoar、字符串[]args、列表mydata)
{
字符串[]str=新字符串[3];
for(int i=0;i0)
{
添加(数据);
}
其他的
{
打破
}
}
测试[i]=新线程(()=>
StreamWriterFun(路径、参数、mydatalist)
);
测试[i].Start();
streamWriter.Close();
}
}
}   
}
}
s、 Close();
System.IO.File.Delete(str[2]);//删除tar文件
}
sw.Stop();
字符串result1=sw.appeased.totalmillizes.ToString();
控制台写入线(结果1);
}
捕获(例外)
{
控制台写入线(“错误1”);
} 
}

是否有其他方法将压缩中的数据写入新文件?不要使用streamWriter

StreamReader和StreamWriter都是专为处理文本流而设计的。如果您切换到使用th,您将有更多的控制权
public static void WriteTo(string targetFile, Stream inputStream)
{
    using (var fileStream = File.Create(targetFile))
    {
        inputStream.CopyTo(fileStream);
    }
}