/// <summary> /// Creates a archive that contains the files and directories from the specified /// directory. /// </summary> /// <param name="srcDir"> /// The path to the directory to be archived. /// </param> /// <param name="destPath"> /// The path of the archive to be created. /// </param> public static void ZipDir(string srcDir, string destPath) { try { var src = PathEx.Combine(srcDir); if (string.IsNullOrEmpty(src)) { throw new ArgumentNullException(nameof(src)); } if (!Directory.Exists(src)) { throw new DirectoryNotFoundException(); } var dest = PathEx.Combine(destPath); if (string.IsNullOrEmpty(dest)) { throw new ArgumentNullException(nameof(dest)); } if (!PathEx.IsValidPath(dest)) { throw new ArgumentException(); } if (File.Exists(dest)) { File.Delete(dest); } ZipFile.CreateFromDirectory(src, dest); } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); } }
/// <summary> /// Formats the specified JSON file and overwrites it if necessary. /// </summary> /// <exception cref="ArgumentNullException"> /// path is null. /// </exception> /// <exception cref="IOException"> /// path is invalid. /// </exception> public static bool FormatFile(string path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } var srcFile = PathEx.Combine(path); if (!PathEx.IsValidPath(srcFile)) { throw new IOException(); } var srcDir = Path.GetDirectoryName(srcFile); var newFile = PathEx.GetUniquePath(srcDir, "tmp", ".json"); using (var sr = new StreamReader(srcFile)) { using var fs = new FileStream(newFile, FileMode.Create); int count; var ca = new char[4096]; var depth = 0; var isEscape = false; var isValue = false; while ((count = sr.Read(ca, 0, ca.Length)) > 0) { Format(fs, ca, count, ' ', ref depth, ref isEscape, ref isValue); } } if (!FileEx.ContentIsEqual(srcFile, newFile)) { return(FileEx.Move(newFile, srcFile, true)); } FileEx.TryDelete(newFile); return(false); }
/// <summary> /// Determines the CPU type of the specified file. /// </summary> /// <param name="path"> /// The file to check. /// </param> public static MachineType GetMachineTypes(string path) { var pe = MachineType.Unknown; try { var file = PathEx.Combine(path); if (!PathEx.IsValidPath(file)) { throw new ArgumentException(); } if (!File.Exists(file)) { throw new PathNotFoundException(file); } var fs = default(FileStream); try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); using (var br = new BinaryReader(fs)) { var i = fs; fs = null; i.Seek(0x3c, SeekOrigin.Begin); i.Seek(br.ReadInt32(), SeekOrigin.Begin); br.ReadUInt32(); pe = (MachineType)br.ReadUInt16(); } } finally { fs?.Dispose(); } } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); } return(pe); }
/// <summary> /// Extracts all the files in the specified zip archive to the specified directory on /// the file system. /// </summary> /// <param name="srcPath"> /// The path of the zip archive to extract. /// </param> /// <param name="destDir"> /// The path to the directory to place the extracted files in. /// </param> /// <param name="delSrcPath"> /// true to delete the source archive after extracting; otherwise, false. /// </param> public static bool Unzip(string srcPath, string destDir, bool delSrcPath = true) { try { var src = PathEx.Combine(srcPath); if (string.IsNullOrEmpty(src)) { throw new ArgumentNullException(nameof(src)); } if (!File.Exists(src)) { throw new FileNotFoundException(); } var dest = PathEx.Combine(destDir); if (string.IsNullOrEmpty(dest)) { throw new ArgumentNullException(nameof(dest)); } using (var archive = ZipFile.OpenRead(src)) try { archive.ExtractToDirectory(dest); } catch { foreach (var ent in archive.Entries) { try { var entPath = ent.FullName; var entIsDir = entPath.EndsWithEx(Path.AltDirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)); entPath = PathEx.Combine(dest, entPath); if (!PathEx.IsValidPath(entPath)) { throw new NotSupportedException(); } if (entIsDir && !Directory.Exists(entPath)) { Directory.CreateDirectory(entPath); continue; } if (ent.Length == 0) { continue; } FileEx.Delete(entPath); var entDir = Path.GetDirectoryName(entPath); if (string.IsNullOrEmpty(entDir)) { continue; } if (!Directory.Exists(entDir)) { FileEx.Delete(entDir); Directory.CreateDirectory(entDir); } ent.ExtractToFile(entPath, true); } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); } } } if (delSrcPath) { FileEx.TryDelete(src); } return(true); } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); return(false); } }
private static string GetVariableFromPathIntern(string path, bool curDir, bool special, out int length) { try { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException(nameof(path)); } var current = path; if (current.StartsWith("%", StringComparison.Ordinal) && (current.ContainsEx($"%{Path.DirectorySeparatorChar}", $"%{Path.AltDirectorySeparatorChar}") || current.EndsWith("%", StringComparison.Ordinal))) { length = current.IndexOf('%', 1); return(current.Substring(1, --length)); } if (!PathEx.IsValidPath(path)) { throw new ArgumentInvalidException(nameof(path)); } if (curDir) { var localDir = PathEx.LocalDir; if (current.StartsWithEx(localDir)) { length = localDir.Length; return("CurDir"); } } var table = (Hashtable)Environment.GetEnvironmentVariables(); foreach (var varKey in table.Keys) { var varValue = (string)table[varKey]; if (varValue.Length < 3 || !PathEx.IsValidPath(varValue) || !current.StartsWithEx(varValue)) { continue; } length = varValue.Length; return((string)varKey); } if (special) { var type = typeof(Environment.SpecialFolder); foreach (var item in Enum.GetValues(type).Cast <Environment.SpecialFolder>()) { var folder = Environment.GetFolderPath(item); if (folder.Length < 3 || !current.StartsWithEx(folder)) { continue; } var name = Enum.GetName(type, item); length = folder.Length; return(name); } } var sysDrive = Environment.GetEnvironmentVariable("SystemDrive"); if (!string.IsNullOrEmpty(sysDrive) && current.StartsWithEx(sysDrive)) { length = sysDrive.Length; return("SystemDrive"); } } catch (InvalidOperationException ex) { if (Log.DebugMode > 1) { Log.Write(ex); } } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); } length = 0; return(string.Empty); }