我是C#
的新手,并创建了一个基本文件存档程序。它起作用,它做了它想做的事情,但我觉得它很慢。我为它添加了一个简单的benchmark
以测试速度并运行了五次。
因此,此进程的平均运行时间为47.369536648秒。我了解这取决于文件的大小,并根据文件的大小进行归档,所以这里是我用作测试的文件大小的图像:
所以这些文件真的不大,所以我不确定这是否是一个很好的处理时间,对我来说似乎有点慢,我想知道是否有#s;无论如何,我可以加快速度吗?
using System;
using System.IO;
using System.IO.Compression;
namespace ArchiveCreator
{
class Archive
{
//These static strings are used for
//information handling they will be
//color coordinated so you can see
//what kind of information is being
//passed to you
static string Success(string input)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(input);
return input;
}
static string Warn(string input)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(input);
return input;
}
static string Say(string input)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(input);
return input;
}
static string FatalErr(string input)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine(input);
return input;
}
static string MinorErr(string input)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(input);
return input;
}
//Main method
static void Main(string[] args)
{
//These variables are used to create a
//random string that will be used as the
//zip files name
var chars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var randFileName = new char[4];
var random = new Random();
//Create the zip file name
for (int i = 0; i < randFileName.Length; i++)
{
randFileName[i] = chars[random.Next(chars.Length)];
}
string finalString = new String(randFileName);
Say("Starting file extraction..");
string day = DateTime.Now.ToString("MM-dd-yy ");
string userName = Environment.UserName;
string startDir = $"c:/users/{userName}/test_folder";
string zipDir = $"c:/users/{userName}/archive/{day}{finalString}.zip";
string dirName = $"c:/users/{userName}/archive";
//Check if the directory exists
Say("Attempting to create archive directory..");
if (Directory.Exists(dirName))
{
MinorErr("Directory already exists, resuming extraction process");
}
else
{
//Create it if it doesn't
Warn($"Creating archive directory here: {dirName}");
Directory.CreateDirectory(dirName);
Say("Directory created, resuming process..");
}
try
{
//Attempt to extract to zip file
Say($"Attempting to extract files into: {zipDir}");
ZipFile.CreateFromDirectory(startDir, zipDir);
Success($"Extracted files successfully to: {zipDir}");
}
catch (Exception e)
{
//Catch any error that occurs during
//the archiving stage and log the error
//to a text file for further analysis
var programPath = System.Reflection.Assembly.GetExecutingAssembly();
FatalErr($"Something went wrong and the program cannot continue, exiting process with error code {e}..");
FatalErr("Writing error to file for further analysis.");
File.WriteAllText($"{programPath}/log/errorlog.txt", e.ToString());
}
Say("Press enter to exit..");
Console.ReadLine();
}
}
}
答案 0 :(得分:2)
您的代码几乎只是ZipFile.CreateFromDirectory
的包装器,所以没有任何&#34;优化&#34;你可以做,除了传递不同的论据。
您可以尝试使用其他CompressionLevel
- 例如:
ZipFile.CreateFromDirectory(startDir, zipDir, CompressionLevel.Fastest, false);
虽然您应该注意到压缩率会变差(输出文件较大)。