0)
{
Array.Resize(ref lines, lines.Length + 1);
}
lineIndex = lines.Length - 1;
// End with new line
Array.Resize(ref lines, lines.Length + 1);
}
lines[lineIndex] = newLine;
File.WriteAllLines(checkFile, lines);
}
catch (Exception ex)
{
Log.LogWarningFromException(ex, showStackTrace: false);
}
}
private int GetCheckFileLineIndexSha1(string[] lines, string name, out string sha1)
{
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] lineParts = line.Split('=');
if (lineParts.Length == 2)
{
string lineTag = lineParts[0].Trim();
string lineSha1 = lineParts[1].Trim();
if (lineTag == name)
{
sha1 = lineSha1;
return i;
}
}
}
sha1 = null;
return -1;
}
private bool CheckFileSha1(string file, string expectedSha1, out string actualSha1)
{
using (var fs = new FileStream(file, FileMode.Open))
{
var hasher = System.Security.Cryptography.SHA1.Create();
byte[] hash = hasher.ComputeHash(fs);
actualSha1 = BytesToHexString(hash);
if (String.Equals(actualSha1, expectedSha1, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private string BytesToHexString(byte[] data)
{
var sb = new StringBuilder();
foreach (byte b in data)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
private static void ExtractZip(string zipPath, string destinationDirectory, bool overwrite)
{
var archive = ZipFile.OpenRead(zipPath);
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectory);
return;
}
foreach (ZipArchiveEntry file in archive.Entries)
{
string fileName = Path.Combine(destinationDirectory, file.FullName);
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (file.Name != String.Empty)
{
file.ExtractToFile(fileName, true);
}
}
}
]]>