using Apewer.Internals; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Security.Permissions; namespace Apewer { /// 存储实用工具。 public static class StorageUtility { private static Exception Try(Action action) { try { action?.Invoke(); return null; } catch (Exception ex) { return ex; } } #region delete /// 删除文件。 public static Exception DeleteFile(string path, bool useTemp = false) { if (useTemp) { try { if (string.IsNullOrEmpty(path)) return new ArgumentException(); if (!File.Exists(path)) return new FileNotFoundException(); Try(() => new FileInfo(path).Attributes = FileAttributes.Normal); var temp = Environment.GetEnvironmentVariable("TEMP"); var name = Path.GetFileName(path); var dest = null as string; while (dest == null || File.Exists(dest)) { var guid = Guid.NewGuid().ToString("n").Substring(0, 8); dest = $"trash_{guid}_{name}"; } File.Move(path, dest); File.Delete(dest); return null; } catch { } } try { File.Delete(path); return null; } catch (Exception ex) { return ex; } } /// 删除目录、子目录和子文件。 public static Exception DeleteDirectory(string path, bool useTemp = false) { if (useTemp) { try { if (string.IsNullOrEmpty(path)) return new ArgumentException(); if (!Directory.Exists(path)) return new DirectoryNotFoundException(); var temp = Environment.GetEnvironmentVariable("TEMP"); var name = Path.GetDirectoryName(path); var dest = null as string; while (dest == null || Directory.Exists(dest)) { var guid = Guid.NewGuid().ToString("n").Substring(0, 8); dest = $"trash_{guid}_{name}"; } Directory.Move(path, dest); Directory.Delete(dest, true); return null; } catch { } } try { Directory.Delete(path, true); return null; } catch (Exception ex) { return ex; } } #endregion /// 向文件追加数据。文件不存在将创建,存在则覆盖。 public static bool AppendFile(string path, params byte[] bytes) { return StorageHelper.AppendFile(path, bytes); } /// 向文件写入数据,每写入 1 KB 数据后将执行回调。若写入失败则返回 -1 值。 public static long AppendFile(string path, Stream stream, Action callback = null) { return StorageHelper.AppendFile(path, stream, callback); } /// 确保目录存在,若不存在则创建,返回目录的存在状态。 public static bool AssureDirectory(string path) { return StorageHelper.AssureDirectory(path); } /// 确保目标路径的上级目录存在,若不存在则创建,返回上级目录的存在状态。 public static bool AssureParent(string path) { return StorageHelper.AssureParent(path); } /// 获取文件或目录的上级目录完整路径,失败时返回 NULL 值。。 public static string GetParentPath(string path) { try { if (File.Exists(path)) return Path.GetDirectoryName(path); if (Directory.Exists(path)) return Directory.GetParent(path).FullName; } catch { } return null; } /// 复制文件。默认不替换目标路径的文件。 public static bool CopyFile(string source, string destination, bool replace = false) { return StorageHelper.CopyFile(source, destination, replace); } /// 获取目录的存在状态。 public static bool DirectoryExists(string path) { return StorageHelper.DirectoryExists(path); } /// 获取文件的存在状态。 public static bool FileExists(string path) { return StorageHelper.FileExists(path); } /// 修正文件名,去除不允许的字符。 public static string FixFileName(string filename) { return StorageHelper.FixFileName(filename); } /// 获取指定目录的子目录,可指定递归子目录。 public static List GetSubDirectories(string path, bool recursive = false) { return StorageHelper.GetSubDirectories(path, recursive, true); } /// 获取指定目录的子目录,可指定递归子目录的深度。 public static List GetSubDirectories(string path, int depth) { return StorageHelper.GetSubDirectories(path, depth, true); } /// 获取指定目录的子文件,可指定递归子目录。 public static List GetSubFiles(string path, bool recursive = false) { return StorageHelper.GetSubFiles(path, recursive, true); } /// 获取指定目录的子文件,可指定递归子目录的深度。 public static List GetSubFiles(string path, int depth) { return StorageHelper.GetSubFiles(path, depth, true); } /// 打开文件,并获取文件流。可选文件的锁定状态。 public static FileStream OpenFile(string path, bool share = true) { return StorageHelper.OpenFile(path, share); } /// 获取文件或目录的存在状态。 public static bool PathExists(string path) { return StorageHelper.PathExists(path); } /// 读取文件,获取文件内容。当文件为 UTF-8 文本文件时,可去除 BOM 头。 public static byte[] ReadFile(string path, bool wipeBom = false) { return StorageHelper.ReadFile(path, wipeBom); } /// 将数据写入新文件,若文件已存在则覆盖。返回写入的字节数,发生错误时返回 -1 值。 public static long WriteFile(string path, Stream stream, Action callback = null) { var created = CreateFile(path, 0, true); if (created && stream != null) { var result = 0L; try { var file = OpenFile(path); if (file != null) { result = StreamHelper.Read(stream, file, Constant.DefaultBufferCapacity, callback); } KernelUtility.Dispose(file); } catch { } return result; } return -1; } /// 将数据写入新文件,若文件已存在则覆盖。 public static bool WriteFile(string path, params byte[] bytes) { if (string.IsNullOrEmpty(path)) return false; if (!CreateFile(path, 0, true)) return false; try { if (bytes != null && bytes.LongLength > 0L) { File.WriteAllBytes(path, bytes); } } catch { return false; } return true; } /// 将数据写入新文件,若文件已存在则覆盖。 public static bool WriteFile(string path, bool bom, params byte[] bytes) { return WriteFile(path, bom ? BinaryUtility.AddTextBom(bytes) : bytes); } /// 将数据写入新文件,若文件已存在则覆盖。 public static bool WriteFile(string path, Json json, bool bom = false) { if (string.IsNullOrEmpty(path)) return false; if (json == null || json.IsNull || json.IsNone) return false; var text = json.ToString(true); var bytes = TextUtility.ToBinary(text); return StorageHelper.WriteFile(path, bom, bytes); } /// 创建一个空文件且不保留句柄。 /// 文件路径,若已存在则返回失败。 /// 文件长度(字节数)。 /// 替换现有文件。 /// 创建成功。 public static bool CreateFile(string path, long length = 0, bool replace = false) { return StorageHelper.CreateFile(path, length, replace); } /// 获取文件的最后写入时间。 public static DateTime GetFileLastWriteTime(string path, bool utc = false) { try { var info = new FileInfo(path); return utc ? info.LastWriteTimeUtc : info.LastWriteTime; } catch { } return new DateTime(); } /// 设置文件的最后写入时间。 public static string SetFileLastWriteTime(string path, DateTime value) { try { var info = new FileInfo(path); info.LastWriteTime = value; return null; } catch (Exception ex) { return ex.Message; } } /// 压缩文件到流。 /// Key 为 ZIP 内的文件名,Value 为原始文件路径。 /// 要输出的 ZIP 流。 public static Exception ToZip(Dictionary files, Stream output) { if (files == null) return new ArgumentNullException("files"); var streams = new List(); var ex = BinaryUtility.ToZip(files.Keys, output, (name) => { if (name.IsEmpty()) return null; var path = files[name]; if (path.IsEmpty()) return null; if (!FileExists(path)) return null; var input = OpenFile(path); streams.Add(input); return input; }, (name) => GetFileLastWriteTime(files[name]), true); KernelUtility.Dispose(streams); return ex; } /// 压缩文件到流。 /// Key 为 ZIP 内的文件名,Value 为原始文件路径。 /// 要输出的 ZIP 文件路径,已存在的文件将被删除。 public static Exception ToZip(Dictionary files, string output) { if (output.IsEmpty()) return new ArgumentException("output"); if (FileExists(output)) DeleteFile(output); if (FileExists(output)) return new IOException(); var stream = OpenFile(output); if (stream == null) return new IOException(); var ex = ToZip(files, stream); KernelUtility.Dispose(stream, true); return ex; } /// 压缩文件到流。 /// 原始文件目录。 /// 要输出的 ZIP 流。 public static Exception ToZip(string directory, Stream output) { if (!DirectoryExists(directory)) return new DirectoryNotFoundException(); var dict = new Dictionary(); foreach (var path in GetSubFiles(directory, true)) { var name = path.Substring(directory.Length); if (name.StartsWith("\\")) name = name.Substring(1); if (name.StartsWith("/")) name = name.Substring(1); dict.Add(name, path); Console.WriteLine(name); } return ToZip(dict, output); } /// 压缩文件到流。 /// 原始文件目录。 /// 要输出的 ZIP 文件路径,已存在的旧文件将被删除。 public static Exception ToZip(string directory, string output) { if (output.IsEmpty()) return new ArgumentException("output"); if (FileExists(output)) DeleteFile(output); if (FileExists(output)) return new IOException("无法删除现有文件。"); var stream = OpenFile(output); if (stream == null) return new IOException("无法创建文件。"); var ex = ToZip(directory, stream); KernelUtility.Dispose(stream, true); return ex; } /// 解压 ZIP 文件到目标目录。已存在的旧文件将被删除。 public static Exception FromZip(string zipPath, string outputDirectory = null) { if (!FileExists(zipPath)) return new FileNotFoundException("指定的 ZIP 文件路径无效。"); var directory = outputDirectory; if (directory.IsEmpty()) { if (zipPath.SafeLower().EndsWith(".zip")) { directory = zipPath.Substring(0, zipPath.Length - 4); //if (directory.EndsWith("/") || directory.EndsWith("\\")) //if (PathExists(directory)) directory = null; } else { directory = GetParentPath(zipPath); } } if (directory.IsEmpty()) return new ArgumentException("无法判断目录路径。"); if (!AssureDirectory(directory)) return new DirectoryNotFoundException("无法创建目录。"); var input = OpenFile(zipPath); if (input == null) return new IOException("无法打开 ZIP 文件。"); var info = new Dictionary(); var ex = BinaryUtility.FromZip(input, (name, size, modifield) => { var path = Path.Combine(directory, name); if (FileExists(path)) DeleteFile(path); if (DirectoryExists(path)) DeleteDirectory(path, true); if (PathExists(path)) throw new IOException("无法删除旧文件或旧目录。"); var output = OpenFile(path); if (output == null) throw new IOException("无法创建文件 " + path + "。"); if (info.ContainsKey(path)) { KernelUtility.Dispose(output); return null; } info.Add(path, modifield); return output; }, (name, modified) => { var path = Path.Combine(directory, name); var exists = AssureDirectory(path); if (!exists) throw new IOException("无法创建 ZIP 中对应的目录。"); }, true); // 恢复文件的修改时间。 foreach (var i in info) SetFileLastWriteTime(i.Key, i.Value); return ex; } ///// 解压 ZIP 文件到字典。失败且不允许异常时返回 NULL 值。 //public static Dictionary FromZip(string zipPath, bool allowException = false) //{ // if (FileExists(zipPath)) // { // var ex = new FileNotFoundException("文件不存在。"); // if (allowException) throw ex; // return null; // } // var input = OpenFile(zipPath); // if (input == null) // { // var ex = new IOException("无法打开 ZIP 文件。"); // if (allowException) throw ex; // return null; // } // return BinaryUtility.FromZip(input, ; //} /// 获取指定程序集的资源。 public static Stream GetResource(string name, Assembly assembly = null) { try { if (assembly == null) assembly = Assembly.GetExecutingAssembly(); var stream = assembly.GetManifestResourceStream(name); return stream; } catch { } return null; } /// 获取当前进程程序集的资源。读取失败时返回 NULL 值。 public static byte[] ReadResource(string name, Assembly assembly = null) { var stream = GetResource(name, assembly); if (stream == null) return null; var bytes = BinaryUtility.Read(stream, true); return bytes; } } }