Files
HLib/Tools/FileHack/SplitFileBySizeUxer.cs
2026-06-01 13:23:34 +09:00

70 lines
2.4 KiB
C#

using KssSmaPlaLib.Tools.ClaraHack.NamespaceAdjust;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Tools.FileHack
{
public static class SplitFileBySizeUxer
{
public static bool TuckIntoMain(string[] args)
{
if (args.Length != 3) return false;
if (args[0].ToLower() != "/filesplit") return false;
var path = args[1];
if (!int.TryParse(args[2], out var kb))
{
Console.WriteLine($"分割サイズ(KB)が数値でありません。[分割サイズ:{args[2]}]");
return false;
}
if (!Try(path, kb, out var messageForMe))
{
Console.WriteLine($"ファイル分割に失敗しました。[エラー内容:{messageForMe}]");
return false;
}
Console.WriteLine($"ファイル分割が成功しました。");
return true;
}
public static bool Try(string filePath, int sizeKB, out string resultMessage)
{
try
{
int partSize = sizeKB * 1024;
string folder = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
int partNum = 1;
using (var input = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[partSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
string partPath = Path.Combine(folder, $"{fileName}.{partNum:D3}");
using (var output = new FileStream(partPath, FileMode.Create, FileAccess.Write))
{
output.Write(buffer, 0, bytesRead);
partNum++;
}
}
}
resultMessage = $"分割成功:{partNum - 1}個のパートに分割されました。";
return true;
}
catch (Exception ex)
{
resultMessage = $"分割失敗: {ex.Message}";
return false;
}
}
}
}