归档文件时加快处理时间
Speeding up process time while archiving files
我是 C#
的新手,已经创建了一个基本的文件归档器。它可以工作,并且可以完成它应该做的事情,但我认为它很慢。我加了一个简单的benchmark
来测试速度,然后运行它五次。
- 50.7120707 秒
- 46.5686564 秒
- 50.2020197 秒
- 44.8384834 秒
- 44.5264522 秒
所以这个过程到 运行 的平均时间是 47.369536648 秒。我知道这取决于它归档的文件的大小以及有多少文件起着很大的作用,所以这是我用作测试的文件大小的图像:
所以文件真的不是很大,所以我不确定这是否是一个很好的处理时间,对我来说似乎有点慢,我想知道我是否可以加快速度?
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();
}
}
}
上询问
您的代码几乎只是 ZipFile.CreateFromDirectory
的包装器,因此除了向其传递不同的参数外,您无能为力 "optimization"。
您可以尝试不同的 CompressionLevel
- 例如:
ZipFile.CreateFromDirectory(startDir, zipDir, CompressionLevel.Fastest, false);
虽然您应该注意到您会得到更差的压缩(更大的输出文件)。
我是 C#
的新手,已经创建了一个基本的文件归档器。它可以工作,并且可以完成它应该做的事情,但我认为它很慢。我加了一个简单的benchmark
来测试速度,然后运行它五次。
- 50.7120707 秒
- 46.5686564 秒
- 50.2020197 秒
- 44.8384834 秒
- 44.5264522 秒
所以这个过程到 运行 的平均时间是 47.369536648 秒。我知道这取决于它归档的文件的大小以及有多少文件起着很大的作用,所以这是我用作测试的文件大小的图像:
所以文件真的不是很大,所以我不确定这是否是一个很好的处理时间,对我来说似乎有点慢,我想知道我是否可以加快速度?
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();
}
}
}
上询问
您的代码几乎只是 ZipFile.CreateFromDirectory
的包装器,因此除了向其传递不同的参数外,您无能为力 "optimization"。
您可以尝试不同的 CompressionLevel
- 例如:
ZipFile.CreateFromDirectory(startDir, zipDir, CompressionLevel.Fastest, false);
虽然您应该注意到您会得到更差的压缩(更大的输出文件)。