public static void ExtractToDirectory(this PackArchive source, string destinationDirectoryName) { if (source == null) { throw new ArgumentNullException("source"); } if (destinationDirectoryName == null) { throw new ArgumentNullException("destinationDirectoryName"); } var directoryInfo = Directory.CreateDirectory(destinationDirectoryName); var directoryName = directoryInfo.FullName; var length = directoryName.Length; if (length != 0 && directoryName[length - 1] != Path.DirectorySeparatorChar) { directoryName += Path.DirectorySeparatorChar.ToString(); } foreach (var packArchiveEntry in source.Entries) { var fullPath = Path.GetFullPath(Path.Combine(directoryName, packArchiveEntry.Path)); if (!fullPath.StartsWith(directoryName, StringComparison.OrdinalIgnoreCase)) { throw new IOException(Messages.IO_ExtractingResultsInOutside); } if (packArchiveEntry.Type == PackArchiveEntry.EntryType.Directory) { if (File.Exists(fullPath)) { throw new IOException(Messages.IO_ExistingDirectoryPathIsFile); } else if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } } else { var dirPath = Path.GetDirectoryName(fullPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } source.ExtractToFile(packArchiveEntry, fullPath, false); } } }
public static PackArchive Open(string archiveFileName, PackArchiveMode mode) { FileMode fileMode; FileAccess fileAccess; FileShare fileShare; switch (mode) { case PackArchiveMode.Read: fileMode = FileMode.Open; fileAccess = FileAccess.Read; fileShare = FileShare.Read; break; case PackArchiveMode.Create: fileMode = FileMode.CreateNew; fileAccess = FileAccess.Write; fileShare = FileShare.None; break; case PackArchiveMode.Update: fileMode = FileMode.OpenOrCreate; fileAccess = FileAccess.ReadWrite; fileShare = FileShare.None; break; default: throw new ArgumentOutOfRangeException("mode"); } FileStream fileStream = null; PackArchive result; try { fileStream = File.Open(archiveFileName, fileMode, fileAccess, fileShare); result = new PackArchive(fileStream, mode, false); } catch { if (fileStream != null) { fileStream.Dispose(); } throw; } return(result); }
public static void ExtractToFile(this PackArchive source, PackArchiveEntry entry, string destinationFileName, bool overwrite) { if (source == null) { throw new ArgumentNullException("source"); } if (destinationFileName == null) { throw new ArgumentNullException("destinationFileName"); } var mode = overwrite ? FileMode.Create : FileMode.CreateNew; using (var stream = File.Open(destinationFileName, mode, FileAccess.Write, FileShare.None)) { using (var stream2 = source.Open(entry)) { stream2.CopyTo(stream); } } }
private static void DoCreateFromDirectory(PackArchive archive, PackArchiveEntry rootEntry, DirectoryInfo rootInfo) { var countBefore = archive.GetEntryCount(); var directories = rootInfo.GetDirectories(); var files = rootInfo.GetFiles(); var index = 0; for (int i = 0; i < directories.Length; i++) { if (IsNormalDirectory(directories[i].Attributes)) { archive.AddEntry(CreateDirectory(rootEntry.Path, directories[i].Name, 0)); directories[index++] = directories[i]; } } for (int j = 0; j < files.Length; j++) { if (IsNormalFile(files[j].Attributes)) { archive.AddEntry(CreateFile(rootEntry.Path, files[j].Name, (int)files[j].Length)); } } int countAfter = archive.GetEntryCount(); rootEntry.Offset = countBefore; rootEntry.Count = countAfter - countBefore; index = 0; for (int i = countBefore; i < countAfter; i++) { if (archive.GetEntry(i).Type == PackArchiveEntry.EntryType.Directory) { DoCreateFromDirectory(archive, archive.GetEntry(i), directories[index++]); } } }