假设我通过套接字流接收文件,一次接收 1024 字节。每次我写入硬盘时,我的防病毒软件都会扫描整个文件。文件越大,写入接下来的 1024 字节所需的时间就越长。更不用说"文件正被另一个进程使用"错误。
我目前的解决方法是将字节存储在内存中的字节数组中,最多 X 兆字节(用户定义),每次填满时,字节数组都会附加到硬盘上的文件中。
byte[] filebytearray = new byte[filesize]; //Store entire file in this byte array.
do
{
serverStream = clientSocket.GetStream();
bytesRead = serverStream.Read(inStream, 0, buffSize); //How many bytes did we just read from the stream?
recstrbytes = new byte[bytesRead]; //Final byte array this loop
Array.Copy(inStream, recstrbytes, bytesRead); //Copy from inStream to the final byte array this loop
Array.Copy(recstrbytes, 0, filebytearray, received, bytesRead); //Copy the data from the final byte array this loop to filebytearray
received += recstrbytes.Length; //Increment bytes received
}while (received < filesize);
addToBinary(filebytearray, @"C:testtest.exe"); //Append filebytearray to binary
(在这个简化的示例中,它只是将整个文件存储在内存中,然后再将其卸载到 hdd)
但我绝对讨厌这种方法,因为它显着增加了我的程序使用的内存。
其他程序员如何解决这个问题?例如,当我使用 Firefox 下载时,它只是全速下载,我的 AV 似乎在完成之前不会拿起它,而且它几乎没有增加进程的内存使用量。这有什么大秘密?
附加到我正在使用的二进制函数 (WIP):
private bool addToBinary(byte[] msg, string filepath)
{
Console.WriteLine("Appending "+msg.Length+" bytes of data.");
bool succ = false;
do
{
try
{
using (Stream fileStream = new FileStream(filepath, FileMode.Append, FileAccess.Write, FileShare.None))
{
fileStream.Write(msg, 0, msg.Length);
fileStream.Flush();
fileStream.Close();
}
succ = true;
}
catch (IOException ex) { /*Console.WriteLine("Write Exception (addToBinary) : " + ex.Message);*/ }
catch (Exception ex) { Console.WriteLine("Some Exception occured (addToBinary) : " + ex.Message); return false; }
} while (!succ);
return true;
}
我看到您每次写入数据时都会重新打开文件。为什么不保持文件流打开?每次关闭它时,防病毒软件都会对其进行扫描,因为它已被修改。
还有一个建议,WriteLine 函数的工作方式类似于 c++ 中的 printf,所以......而不是做:
Console.WriteLine("Appending "+msg.Length+" bytes of data.");
你可以做:
Console.WriteLine("Appending {0} bytes of data.", msg.Length);
这有时确实可以节省您的时间。
首先,您可以使用内存流。其次,您必须多次写入磁盘,只需在后台进行,这样用户就不会注意到。
创建内存流的并发队列,并创建一个将尝试清空队列的处理程序。
您可以向防病毒软件添加排除项以阻止其干扰。如果要扫描数据,请将其下载到排除的文件夹,然后在文件完成后将其移动到文件夹(将扫描)。
其他方法是缓冲数据,这样您就不会以微小的 1k 增量写入,并在完成写入之前保持文件打开状态。