![](/img/trans.png)
[英]C# http download with large files (300 Mb) stops after a few minutes (after 80 Mb)
[英]Best way to compress (ie best combo of compression ratio and speed) a directory of large files (each one b/w 100-300 MB) in C#?
我正在编写一个控制台应用程序来压缩大文件目录(大约30个),每个文件大约100-300 MB,每天一次(新文件进入)。 我尝试使用内置的GZipStream类,每个文件花费了大约15秒,压缩率约为0.212。 我想知道第三方库是否有更有效的方法,或者是否有提高压缩率的方法。 最后,是否可以选择加快此过程的速度?
这是Im目前正在使用的代码(基本上来自GZipStream上的MSDN文章)
private void CompressFile(FileInfo fileInfo)
{
// Get the stream of the source file.
using (FileStream inFile = fileInfo.OpenRead())
{
Timer.Reset();
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fileInfo.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileInfo.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile = File.Create(fileInfo.FullName + ".gz"))
{
using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
Timer.Start();
inFile.CopyTo(Compress);
Timer.Stop();
Console.WriteLine("Compressed {0} from {1} to {2} bytes in {3} seconds.",
fileInfo.Name, fileInfo.Length.ToString(), outFile.Length.ToString(), ((double)Timer.ElapsedMilliseconds / 1000));
}
}
}
}
}
谢谢!
这个答案: 在多个线程上并行调用ICsharpCode.SharpZipLib是否安全?
给出了一些GZIP压缩替代方案的比较。
您的数据足够大,您可以从并行压缩中受益。
此示例代码执行并行压缩。
与内置的GZipStream相比,并行方法花费了大约一半的时间并且呈现“稍好一点”的压缩。
DotNetZip还具有用于BZip2压缩的类(包括并行实现)。 BZip2比GZIP慢得多,但为您提供了更好的压缩比。
没有通用的方法。 您需要对其进行分析
您可以将Level
参数传递给GZipStream构造函数
我会考虑使用预先存在的(外部)工具来完成这项工作。 比较基准测试你会更快,因为你不必去实现它们。 我真的建议使用unix之类的工具,但是你可能无法在Windows平台上找到它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.