// Constructor public PK3Reader(DataLocation dl, bool asreadonly) : base(dl, asreadonly) { General.WriteLogLine("Opening " + Path.GetExtension(location.location).ToUpper().Replace(".", "") + " resource \"" + location.location + "\""); if (!File.Exists(location.location)) { throw new FileNotFoundException("Could not find the file \"" + location.location + "\"", location.location); } // Make list of all files List <DirectoryFileEntry> fileentries = new List <DirectoryFileEntry>(); // Create archive archive = ArchiveFactory.Open(location.location, Options.KeepStreamsOpen); archivetype = archive.Type; // Random access of 7z archives works TERRIBLY slow in SharpCompress if (archivetype == ArchiveType.SevenZip) { isreadonly = true; // Unsaveable... sevenzipentries = new Dictionary <string, byte[]>(StringComparer.Ordinal); IReader reader = archive.ExtractAllEntries(); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory || !CheckInvalidPathChars(reader.Entry.Key)) { continue; } MemoryStream s = new MemoryStream(); reader.WriteEntryTo(s); sevenzipentries.Add(reader.Entry.Key.ToLowerInvariant(), s.ToArray()); fileentries.Add(new DirectoryFileEntry(reader.Entry.Key)); } } else { foreach (IArchiveEntry entry in archive.Entries) { if (!entry.IsDirectory && CheckInvalidPathChars(entry.Key)) { fileentries.Add(new DirectoryFileEntry(entry.Key)); } } } // Get rid of archive archive.Dispose(); archive = null; // Make files list files = new DirectoryFilesList(dl.GetDisplayName(), fileentries); // Initialize without path (because we use paths relative to the PK3 file) Initialize(); // We have no destructor GC.SuppressFinalize(this); }
private void LoadFrom(DataLocation dl, bool asreadonly) { FileAccess access; FileShare share; isreadonly = asreadonly; // Determine if opening for read only if (isreadonly) { // Read only access = FileAccess.Read; share = FileShare.ReadWrite; } else { // Private access access = FileAccess.ReadWrite; share = FileShare.Read; } General.WriteLogLine("Opening " + Path.GetExtension(location.location).ToUpper().Replace(".", "") + " resource \"" + location.location + "\""); if (!File.Exists(location.location)) throw new FileNotFoundException("Could not find the file \"" + location.location + "\"", location.location); // Make list of all files List<DirectoryFileEntry> fileentries = new List<DirectoryFileEntry>(); // Take the detour with a FileStream because SharpCompress doesn't directly support opening files as read-only filestream = File.Open(location.location, FileMode.OpenOrCreate, access, share); // Create archive archive = ArchiveFactory.Open(filestream); archivetype = archive.Type; // Random access of 7z archives works TERRIBLY slow in SharpCompress if (archivetype == ArchiveType.SevenZip) { isreadonly = true; // Unsaveable... sevenzipentries = new Dictionary<string, byte[]>(StringComparer.Ordinal); IReader reader = archive.ExtractAllEntries(); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory || !CheckInvalidPathChars(reader.Entry.Key)) continue; MemoryStream s = new MemoryStream(); reader.WriteEntryTo(s); sevenzipentries.Add(reader.Entry.Key.ToLowerInvariant(), s.ToArray()); fileentries.Add(new DirectoryFileEntry(reader.Entry.Key)); } } else { foreach (IArchiveEntry entry in archive.Entries) { if (!entry.IsDirectory && CheckInvalidPathChars(entry.Key)) fileentries.Add(new DirectoryFileEntry(entry.Key)); } } // Get rid of archive archive.Dispose(); archive = null; filestream.Dispose(); filestream = null; // Make files list files = new DirectoryFilesList(dl.GetDisplayName(), fileentries); // Initialize without path (because we use paths relative to the PK3 file) Initialize(); // We have no destructor GC.SuppressFinalize(this); }