133 lines
2.8 KiB
C#
133 lines
2.8 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using System.Runtime.InteropServices;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace Hua.DotNet.Code.Helper;
|
||
|
||
public class FileHelper
|
||
{
|
||
public static string GetDirPath(string path = "")
|
||
{
|
||
string text = DateTime.Now.ToString("yyyyMMdd");
|
||
if (!string.IsNullOrEmpty(path))
|
||
{
|
||
text = Path.Combine(path, text);
|
||
}
|
||
return text;
|
||
}
|
||
|
||
public static string HashFileName(string str = null)
|
||
{
|
||
if (string.IsNullOrEmpty(str))
|
||
{
|
||
str = Guid.NewGuid().ToString();
|
||
}
|
||
return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
|
||
}
|
||
|
||
public static void DeleteDirectory(string file)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(file))
|
||
{
|
||
return;
|
||
}
|
||
new DirectoryInfo(file).Attributes = (FileAttributes)0;
|
||
string[] fileSystemEntries = Directory.GetFileSystemEntries(file);
|
||
foreach (string text in fileSystemEntries)
|
||
{
|
||
if (File.Exists(text))
|
||
{
|
||
File.SetAttributes(file, FileAttributes.Normal);
|
||
File.Delete(text);
|
||
}
|
||
else
|
||
{
|
||
DeleteDirectory(text);
|
||
}
|
||
}
|
||
Directory.Delete(file);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log(ex.Message);
|
||
}
|
||
}
|
||
|
||
public static bool ZipGenCode(string zipPath, string genCodePath, string zipFileName)
|
||
{
|
||
if (string.IsNullOrEmpty(zipPath))
|
||
{
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
CreateDirectory(genCodePath);
|
||
string text = Path.Combine(zipPath, zipFileName);
|
||
if (File.Exists(text))
|
||
{
|
||
File.Delete(text);
|
||
}
|
||
ZipFile.CreateFromDirectory(genCodePath, text);
|
||
DeleteDirectory(genCodePath);
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("压缩文件出错。", ex.Message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static bool CreateDirectory(string path)
|
||
{
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||
{
|
||
path = path.Replace("\\", "/").Replace("//", "/");
|
||
}
|
||
try
|
||
{
|
||
if (!Directory.Exists(path))
|
||
{
|
||
Directory.CreateDirectory(path);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("创建文件夹出错了", ex.Message);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static void WriteAndSave(string path, string content)
|
||
{
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||
{
|
||
path = path.Replace("\\", "/").Replace("//", "/");
|
||
}
|
||
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
||
{
|
||
Directory.CreateDirectory(Path.GetDirectoryName(path) ?? string.Empty);
|
||
}
|
||
LogHelper.Log("开始写入文件,Path=" + path);
|
||
try
|
||
{
|
||
using FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
||
using StreamWriter streamWriter = new StreamWriter(fileStream);
|
||
streamWriter.Write(content);
|
||
streamWriter.Flush();
|
||
streamWriter.Close();
|
||
fileStream.Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("写入文件出错了:", ex.Message);
|
||
}
|
||
}
|
||
}
|