You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.3 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO.Compression;
using System.Diagnostics;
using System.Net;
namespace PUtility
{
public class PUtil
{
private static readonly string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
public static string GetRandomString(int length = 5)
{
char[] randCharArr = new char[length];
Random rand = new Random();
for (int i = 0; i < length; i++)
{
randCharArr[i] = characters[rand.Next(characters.Length)];
}
return new string(randCharArr);
}
public static void ScrollToEnd(TextBox target)
{
if (target == null)
return;
target.SelectionStart = target.Text.Length;
target.ScrollToCaret();
}
public static byte[] CompressBytes(byte[] data, CompressionLevel level = CompressionLevel.Fastest)
{
byte[] compressedData;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gs = new GZipStream(ms, level))
{
gs.Write(data, 0, data.Length);
}
compressedData = ms.ToArray();
}
Debug.WriteLine($"Compressed{data.Length} to {compressedData.Length} ({(float)compressedData.Length / (float)data.Length * 100f}%)");
return compressedData;
}
public static byte[] DecompressBytes(byte[] data)
{
byte[] decompressedData;
using (MemoryStream resultStream = new MemoryStream())
{
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
{
gs.CopyTo(resultStream);
gs.Close();
}
}
decompressedData = resultStream.ToArray();
}
//Debug.WriteLine($"Decompressed{data.Length} to {decompressedData.Length} ({(float)decompressedData.Length / (float)data.Length * 100f}%)");
return decompressedData;
}
public static byte[] ImageToBytes(Image image)
{
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
data = ms.ToArray();
}
return data;
}
public static Bitmap BytesToImage(byte[] imgByteArray)
{
Bitmap bitmap;
using (MemoryStream ms = new MemoryStream(imgByteArray))
{
bitmap = Image.FromStream(ms) as Bitmap;
}
return bitmap;
}
public static void SaveImage(string fileName, Image img, ImageFormat format)
{
using (Image saveImage = new Bitmap(img))
{
saveImage.Save(fileName, format);
}
}
public static byte[] FileToBytes(string filePath)
{
return File.ReadAllBytes(filePath);
//byte[] data;
//using (FileStream fs = new FileStream(filePath, FileMode.Open))
//{
// data = new byte[fs.Length];
// fs.Read(data, 0, data.Length);
//}
//return data;
}
public static void BytesToFile(string filePath, byte[] data)
{
File.WriteAllBytes(filePath, data);
//using (FileStream fs = new FileStream(filePath, FileMode.Create))
//{
// fs.Write(data, 0, data.Length);
//}
}
public static string CheckFileDuplication(string dirPath, string fileName)
{
if (string.IsNullOrEmpty(dirPath) || string.IsNullOrEmpty(fileName))
return null;
string resultFileName = fileName;
int dotIdx = fileName.LastIndexOf(".");
string name = fileName.Substring(0, dotIdx);
string ext = fileName.Substring(dotIdx);
bool fileExist = true;
int fileCount = 0;
string dirMapPath = "";
while (fileExist)
{
dirMapPath = dirPath;
string combinedPath = Path.Combine(dirMapPath, resultFileName);
if (!File.Exists(combinedPath))
{
fileExist = false;
break;
}
fileCount++;
resultFileName = $"{name}({fileCount}){ext}";
}
return resultFileName;
}
public static IPAddress ValidateAddress(string address)
{
try
{
IPAddress ip;
if (!string.IsNullOrEmpty(address) && IPAddress.TryParse(address, out ip))
return ip;
// find ip through dns
IPHostEntry hostEntry = Dns.GetHostEntry(address);
if (hostEntry.AddressList == null || hostEntry.AddressList.Length < 1)
return null;
return hostEntry.AddressList[0];
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
}
}