【发布时间】:2010-10-19 10:47:50
【问题描述】:
我想知道如何在不使用太多系统资源的情况下拆分大文件。 我目前正在使用此代码:
public static void SplitFile(string inputFile, int chunkSize, string path)
{
byte[] buffer = new byte[chunkSize];
using (Stream input = File.OpenRead(inputFile))
{
int index = 0;
while (input.Position < input.Length)
{
using (Stream output = File.Create(path + "\\" + index))
{
int chunkBytesRead = 0;
while (chunkBytesRead < chunkSize)
{
int bytesRead = input.Read(buffer,
chunkBytesRead,
chunkSize - chunkBytesRead);
if (bytesRead == 0)
{
break;
}
chunkBytesRead += bytesRead;
}
output.Write(buffer, 0, chunkBytesRead);
}
index++;
}
}
}
该操作需要 52.370 秒才能将 1.6GB 文件拆分为 14mb 文件。我不关心操作需要多长时间,我更关心使用的系统资源,因为此应用程序将部署到共享托管环境。目前,此操作使我的系统 HDD IO 使用率达到 100%,并大大降低了我的系统速度。 CPU使用率低; RAM 增加了一点,但看起来还不错。
有没有办法限制此操作使用过多资源?
谢谢
【问题讨论】:
-
你不能在单独的低优先级线程上运行它吗?
-
@w69rdy - 注意“CPU 使用率低” - CPU 不是这里的瓶颈。