自从将我的代码转换为从档案中读取资源后,我注意到我的应用程序的性能略有下降。我将如何改进以下代码以帮助将性能提高到使用平面文件系统时或尽可能接近时的性能。
class Resource
{
private static readonly string ResourcePath = AppDomain.CurrentDomain.BaseDirectory + "resources.pak";
private const string ResourcePassword = "0ORzjHcFxV0QXTuOizu0";
private static ZipFile Read(string filepath, string password = null)
{
var file = ZipFile.Read(filepath);
file.Password = password;
return file;
}
internal static List<string> GetFiles(string haystack, string needle = "*")
{
var resource = Read(ResourcePath, ResourcePassword).SelectEntries(needle, haystack);
return resource.Select(filepath => filepath.ToString().Replace("ZipEntry::", "")).ToList();
}
internal static MemoryStream ReadFile(string filepath)
{
var file = Read(ResourcePath, ResourcePassword);
var stream = new MemoryStream();
file[filepath].Extract(stream);
return stream;
}
internal static ImageSource StreamImage(string filepath)
{
var getFile = Resource.ReadFile(filepath);
var imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = getFile;
imageSource.EndInit();
return imageSource;
}
internal static int[] ImageSize(string filepath)
{
int[] sizeValues = {0, 0};
var readImage = Resource.ReadFile(filepath);
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = readImage;
image.EndInit();
sizeValues[0] = image.PixelWidth;
sizeValues[1] = image.PixelHeight;
return sizeValues;
}
}
请注意,我已删除错误检查以保持代码简短和重点。