public void AddZipEntry(ScsZipEntry entry, string path) { if (_rootDirectory == null) { _rootDirectory = new ScsDirectory(this, ""); _rootDirectory.AddZipEntry(entry, path); } else { _rootDirectory.AddZipEntry(entry, path); } }
public void AddZipEntry(ScsZipEntry entry, string path) { if (entry == null || path == "") { return; } var slashIndex = path.IndexOf("/", StringComparison.Ordinal); if (slashIndex == -1) // no slash found => end of path = file location { var fileHash = entry.GetHash(); var newPath = Path.Combine(EntryPath, path); newPath = newPath.Replace('\\', '/'); if (Files.ContainsKey(fileHash)) { // Log.Msg($"File '{filePath}' already exists => overwriting"); Files[fileHash] = new ScsFile(entry, newPath); } else { Files.Add(fileHash, new ScsFile(entry, newPath)); } return; } if (path.StartsWith("/")) { path = path.Substring(1); } var currentDir = path.Substring(0, slashIndex); var hashName = Helper.CombinePath(EntryPath, currentDir); var hash = CityHash.CityHash64(Encoding.UTF8.GetBytes(hashName), (ulong)hashName.Length); if (Directories.ContainsKey(hash)) { Directories[hash].AddZipEntry(entry, path.Substring(slashIndex + 1)); } else { var newPath = Path.Combine(EntryPath, currentDir); newPath = newPath.Replace('\\', '/'); var dir = new ScsDirectory(_rfs, newPath); dir.AddZipEntry(entry, path.Substring(slashIndex + 1)); Directories.Add(hash, dir); } }
public ScsZipFile(string path, RootFileSystem rfs) : base(path, rfs) { if (!File.Exists(_path)) { return; } Br = new BinaryReader(File.OpenRead(_path)); Entries = new Dictionary <string, ScsZipEntry>(); var entryCount = (short)ReadUInt16(Br, -22 + 10, SeekOrigin.End); var fileOffset = 0; for (var i = 0; i < entryCount; i++) { var entry = new ScsZipEntry(this) { CompressionMethod = ReadUInt16(Br, fileOffset += 8), CompressedSize = ReadInt32(Br, fileOffset += 10), Size = ReadInt32(Br, fileOffset += 4), NameLength = (short)ReadUInt16(Br, fileOffset += 4), }; var extraFieldLength = ReadUInt16(Br, fileOffset += 2); Br.BaseStream.Seek(fileOffset += 2, SeekOrigin.Begin); entry.Name = Encoding.UTF8.GetString(Br.ReadBytes(entry.NameLength)); fileOffset += entry.NameLength + extraFieldLength; entry.Offset = fileOffset; // Offset to data fileOffset += entry.CompressedSize; if (entry.CompressedSize != 0) // only files { var filePath = entry.Name.Replace('\\', '/'); _rfs.AddZipEntry(entry, filePath); } Entries.Add(entry.Name, entry); } }