| 1 | using System.IO; |
| 2 | |
| 3 | namespace Zipelo.Ui; |
| 4 | |
| 5 | public sealed record ArchiveOpenTarget(string? Path, string? Error); |
| 6 | |
| 7 | public static class ArchiveOpenArguments |
| 8 | { |
| 9 | public static ArchiveOpenTarget Parse(IReadOnlyList<string> arguments) |
| 10 | { |
| 11 | if (arguments.Count == 0) { return new(null, null); } |
| 12 | if (arguments.Count != 1) |
| 13 | { |
| 14 | return new(null, "압축파일은 한 번에 하나만 열 수 있습니다. 단일 로컬 ZIP 경로를 전달하세요."); |
| 15 | } |
| 16 | var path = arguments[0]; |
| 17 | if (path.StartsWith('-') || (OperatingSystem.IsWindows() && path.StartsWith('/'))) |
| 18 | { |
| 19 | return new(null, "알 수 없는 옵션입니다. 옵션 없이 단일 로컬 ZIP 절대 경로를 전달하세요."); |
| 20 | } |
| 21 | return ValidatePath(path); |
| 22 | } |
| 23 | |
| 24 | public static string? ValidateName(string name) => Path.GetExtension(name).ToLowerInvariant() switch |
| 25 | { |
| 26 | ".jar" => "JAR 파일은 지원 대상에서 제외됩니다.", |
| 27 | ".zip" => null, |
| 28 | _ => "현재는 .zip 파일의 목록 열기만 지원합니다. 다른 형식은 열 수 없습니다." |
| 29 | }; |
| 30 | |
| 31 | public static ArchiveOpenTarget ValidatePath(string path) |
| 32 | { |
| 33 | try |
| 34 | { |
| 35 | // URI, UNC 및 장치 경로를 실행 인수나 원격 주소로 재해석하지 않음. |
| 36 | if (string.IsNullOrWhiteSpace(path) || path.Contains('\0') || |
| 37 | !Path.IsPathFullyQualified(path) || path.StartsWith(@"\\") || path.StartsWith("//")) |
| 38 | { |
| 39 | return new(null, "로컬 파일의 절대 경로가 필요합니다. URI, 네트워크와 장치 경로는 지원하지 않습니다."); |
| 40 | } |
| 41 | var error = ValidateName(path); |
| 42 | if (error is not null) { return new(null, error); } |
| 43 | if (!File.Exists(path)) { return new(null, "파일이 없거나 읽을 수 없습니다. 파일 위치와 접근 권한을 확인하세요."); } |
| 44 | if ((File.GetAttributes(path) & (FileAttributes.Directory | FileAttributes.Device)) != 0) |
| 45 | { |
| 46 | return new(null, "폴더나 장치는 열 수 없습니다. 일반 ZIP 파일을 선택하세요."); |
| 47 | } |
| 48 | return new(Path.GetFullPath(path), null); |
| 49 | } |
| 50 | catch (Exception error) when (error is ArgumentException or IOException or UnauthorizedAccessException or NotSupportedException) |
| 51 | { |
| 52 | return new(null, "파일 경로를 확인하지 못했습니다. 올바른 로컬 ZIP 경로를 전달하세요."); |
| 53 | } |
| 54 | } |
| 55 | } |