重构部分代码
This commit is contained in:
parent
e33f97b192
commit
4c0ebe10fb
@ -19,6 +19,7 @@
|
||||
* 2020-01-01: V2.2.0 增加文件说明
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
@ -31,6 +32,48 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
public static class DirEx
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个目录
|
||||
/// </summary>
|
||||
/// <param name="dir">目录的绝对路径</param>
|
||||
public static bool CreateDir(string dir)
|
||||
{
|
||||
if (dir.IsNullOrEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(dir, "目录不能为空。");
|
||||
}
|
||||
|
||||
if (!Path.IsPathRooted(dir))
|
||||
{
|
||||
throw new ArgumentException(dir, "不包含根路径。");
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
return Directory.Exists(dir);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回当前基目录
|
||||
/// </summary>
|
||||
/// <returns>基目录</returns>
|
||||
public static string CurrentDir()
|
||||
{
|
||||
return AppDomain.CurrentDomain.BaseDirectory.DealPath();
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择文件夹
|
||||
/// </summary>
|
||||
|
@ -37,6 +37,7 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Sunny.UI
|
||||
@ -55,7 +56,7 @@ namespace Sunny.UI
|
||||
timer = new Timer();
|
||||
timer.Tick += Timer_Tick;
|
||||
|
||||
if (FileEx.Exists(fileName))
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
Image = Image.FromFile(fileName);
|
||||
}
|
||||
|
@ -19,11 +19,8 @@
|
||||
* 2020-01-01: V2.2.0 增加文件说明
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Sunny.UI
|
||||
{
|
||||
@ -189,67 +186,5 @@ namespace Sunny.UI
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 压缩数组
|
||||
/// </summary>
|
||||
/// <param name="input">数组</param>
|
||||
/// <returns>结果</returns>
|
||||
public static byte[] Compress(byte[] input)
|
||||
{
|
||||
string filename = FileEx.TempFileName();
|
||||
string filename7z = FileEx.TempFileName();
|
||||
File.WriteAllBytes(filename, input);
|
||||
ZipFile(filename, filename7z);
|
||||
byte[] bts = File.ReadAllBytes(filename7z);
|
||||
FileEx.TryDelete(filename);
|
||||
FileEx.TryDelete(filename7z);
|
||||
return bts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解压缩数组
|
||||
/// </summary>
|
||||
/// <param name="input">数组</param>
|
||||
/// <returns>结果</returns>
|
||||
public static byte[] Decompress(byte[] input)
|
||||
{
|
||||
string zipfile = FileEx.TempFileName();
|
||||
File.WriteAllBytes(zipfile, input);
|
||||
|
||||
string unzipDir = DirEx.TempRandomPath();
|
||||
UnZipFile(zipfile, unzipDir);
|
||||
string[] fall = Directory.GetFiles(unzipDir, "*.*", SearchOption.TopDirectoryOnly);
|
||||
string unzipfile = fall[0];
|
||||
byte[] bts = File.ReadAllBytes(unzipfile);
|
||||
FileEx.TryDelete(unzipfile);
|
||||
FileEx.TryDelete(zipfile);
|
||||
DirEx.TryDelete(unzipDir);
|
||||
return bts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 压缩字符串
|
||||
/// </summary>
|
||||
/// <param name="input">字符串</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string Compress(string input)
|
||||
{
|
||||
byte[] inputBytes = Encoding.Default.GetBytes(input);
|
||||
byte[] result = Compress(inputBytes);
|
||||
return Convert.ToBase64String(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解压缩字符串
|
||||
/// </summary>
|
||||
/// <param name="input">字符串</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string Decompress(string input)
|
||||
{
|
||||
byte[] inputBytes = Convert.FromBase64String(input);
|
||||
byte[] depressBytes = Decompress(inputBytes);
|
||||
return Encoding.Default.GetString(depressBytes);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user