public static void ExtractToDirectory(string zipFile, string targetDir) { var dirsCreated = new HashSet <string>(); var archive = ZipFile.Open(zipFile, ZipArchiveMode.Read); foreach (var entry in archive.Entries) { var fullName = entry.FullName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); foreach (var dirsToCreate in FindDirectoriesToCreate(dirsCreated, fullName)) { LongPathDisk.CreateDirectory(Path.Combine(targetDir, dirsToCreate)); dirsCreated.Add(dirsToCreate); } if (fullName.EndsWith(Path.DirectorySeparatorChar.ToString())) { continue; // Continue when entry is a directory } using (MemoryStream ms = new MemoryStream()) { entry.Open().CopyTo(ms); LongPathDisk.WriteAllBytes(Path.Combine(targetDir, fullName), ms.ToArray()); } } }
public static void DeleteFile(Log log, string name) { var fileExists = PlatformDetection.IsWindows ? LongPathDisk.FileExists(name) : File.Exists(name); if (!fileExists) { return; } try { log.Event(IOEvent.Rm, name); if (PlatformDetection.IsWindows) { LongPathDisk.DeleteFile(name); } else { File.Delete(name); } } catch (Exception e) { log.Warning("Failed to delete " + name.ToRelativePath() + ": " + e.Message); } }
public static void DeleteDirectory(Log log, string name) { var directoryExists = PlatformDetection.IsWindows ? LongPathDisk.DirectoryExists(name) : Directory.Exists(name); if (!directoryExists) { return; } try { log.Event(IOEvent.RmDir, name); if (PlatformDetection.IsWindows) { LongPathDisk.DeleteDirectory(name, true); } else { Directory.Delete(name, true); } } catch (Exception e) { log.Warning("Failed to delete " + name.ToRelativePath() + ": " + e.Message); } }
public static void CreateDirectory(Log log, string path) { var dirExists = PlatformDetection.IsWindows ? LongPathDisk.DirectoryExists(path) : Directory.Exists(path); if (path.Length > 1 && !dirExists) { CreateDirectory(log, Path.GetDirectoryName(path)); log.Event(IOEvent.MkDir, path); var isHidden = Path.GetFileName(path).StartsWith("."); if (PlatformDetection.IsWindows) { LongPathDisk.CreateDirectory(path); if (isHidden) { LongPathDisk.SetFileAttributes(path, FileAttributes.Hidden); } } else { var di = Directory.CreateDirectory(path); // Hide directory when starts with a dot if (isHidden) { di.Attributes |= FileAttributes.Hidden; } } } }