Files
DDUtility/DDUtilityApp/MESDOWNLOADER/Utili.cs

100 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace DDUtilityApp.MESDOWNLOADER
{
public static class Util
{
public static string GetFileMerge(List<string> filePaths)
{
string mergedFilePath = string.Empty;
if (filePaths.Count == 0)
{
Console.WriteLine("병합할 파일이 없습니다.");
return mergedFilePath;
}
// 실행 시간 측정을 위한 Stopwatch 시작
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 첫 번째 파일명에서 경로 제외
string firstFileName = Path.GetFileName(filePaths.First());
string mergedFileName = "merge_" + firstFileName;
// 병합된 파일을 저장할 경로 (첫 번째 파일과 동일한 폴더)
mergedFilePath = Path.Combine(Path.GetDirectoryName(filePaths.First()), mergedFileName);
string extension = Path.GetExtension(mergedFilePath);
if (string.Compare(extension, ".Log", true) != 0) mergedFilePath = mergedFilePath + ".Log";
try
{
using (StreamWriter writer = new StreamWriter(mergedFilePath, false, Encoding.Default))
{
foreach (string file in filePaths)
{
if (File.Exists(file))
{
using (StreamReader reader = new StreamReader(file, Encoding.Default))
{
writer.Write(reader.ReadToEnd());
}
Console.WriteLine($"{file} -> 병합 완료");
}
else
{
Console.WriteLine($"파일 없음: {file}");
}
}
}
// 실행 시간 측정 종료
stopwatch.Stop();
Console.WriteLine($"모든 파일이 {mergedFileName}로 병합되었습니다.");
Console.WriteLine($"병합 실행 시간: {stopwatch.ElapsedMilliseconds} ms");
return mergedFilePath;
}
catch (Exception ex)
{
Console.WriteLine($"오류 발생: {ex.Message}");
}
return mergedFilePath;
}
public static string GetStr(object str )
{
return string.IsNullOrEmpty(str.ToString())?"": str.ToString();
}
public static string GetBase64(string base64, string type = "decoded")
{
string rtnStr = string.Empty;
if (string.IsNullOrEmpty(base64) == false)
{
if (type == "decoded")
// Base64 디코딩
rtnStr = Encoding.UTF8.GetString(Convert.FromBase64String(base64));
else
// Base64 디코딩
rtnStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(base64));
}
return rtnStr;
}
public static string[] GetUser(string userInfo)
{
string[] rtnArry = null;
if (string.IsNullOrEmpty(userInfo) == false)
{
rtnArry = GetBase64(userInfo).Split(':');
}
return rtnArry;
}
}
}