public void RemoveFiles(IEnumerable <string> files) { if (mStream != null && files.Any()) { List <PAKFileHeader> oldHeaders = new List <PAKFileHeader>(mHeaders); foreach (string file in files) { for (int i = 0; i < oldHeaders.Count; ++i) { if (oldHeaders[i].FileName == file) { oldHeaders.RemoveAt(i); break; } } } if (oldHeaders.Count < mHeaders.Count) { byte[] oldData = new byte[mMainHeader.mDirectoryOffset + mMainHeader.mDirectoryLength]; BinaryReader reader = new BinaryReader(mStream); reader.Read(oldData, 0, oldData.Length); reader.Close(); mMainHeader = new PAKMainHeader(); mHeaders = new List <PAKFileHeader>(); mStream = File.Open(mArchivePath, FileMode.Create); BinaryWriter writer = new BinaryWriter(mStream, Encoding.UTF8, true); mMainHeader.Write(writer); foreach (PAKFileHeader header in oldHeaders) { writer.BaseStream.Seek(mMainHeader.mDirectoryOffset, SeekOrigin.Begin); writer.Write(oldData, header.mFileOffset, header.mFileSize); PAKFileHeader newHeader = new PAKFileHeader(); Array.Copy(header.mFileName, newHeader.mFileName, PAKFileHeader.FILE_NAME_LENGTH); newHeader.mFileOffset = mMainHeader.mDirectoryOffset; newHeader.mFileSize = header.mFileSize; mHeaders.Add(newHeader); mMainHeader.mDirectoryOffset += header.mFileSize; mMainHeader.mDirectoryLength += PAKFileHeader.SIZE_OF; } writer.BaseStream.Seek(mMainHeader.mDirectoryOffset, SeekOrigin.Begin); foreach (PAKFileHeader header in mHeaders) { header.Write(writer); } writer.BaseStream.Seek(0, SeekOrigin.Begin); mMainHeader.Write(writer); OnEntriesUpdated(); } } }
public void Open(String archivePath, bool newArchive) { mArchivePath = archivePath; mMainHeader = new PAKMainHeader(); mHeaders = new List <PAKFileHeader>(); if (newArchive) { mStream = File.Open(archivePath, FileMode.Create); BinaryWriter writer = new BinaryWriter(mStream, Encoding.UTF8, true); mMainHeader.Write(writer); } else { mStream = File.Open(archivePath, FileMode.Open); BinaryReader reader = new BinaryReader(mStream, Encoding.UTF8, true); mMainHeader.mSignature = reader.ReadChars(PAKMainHeader.SIGNATURE_LENGTH); if (mMainHeader.Signature != "PACK") { return; } mMainHeader.mDirectoryOffset = reader.ReadInt32(); mMainHeader.mDirectoryLength = reader.ReadInt32(); if (mMainHeader.mDirectoryOffset > PAKMainHeader.SIZE_OF) { int numFiles = mMainHeader.NumFiles; reader.BaseStream.Seek(mMainHeader.mDirectoryOffset, SeekOrigin.Begin); for (int i = 0; i < numFiles; ++i) { PAKFileHeader header = new PAKFileHeader(); header.Read(reader); mHeaders.Add(header); } } } OnEntriesUpdated(); }
public void Export(ArchiveProject project, Stream stream) { BinaryWriter writer = new BinaryWriter(stream); PAKMainHeader mainHeader = new PAKMainHeader(); long startPosition = stream.Position; stream.Seek(PAKMainHeader.SIZE_OF, SeekOrigin.Current); ICollection <ArchiveProjectEntry> entries = project.GetFiles(); List <PAKFileHeader> fileHeaders = new List <PAKFileHeader>(); foreach (ArchiveProjectEntry entry in entries) { byte[] contents = File.ReadAllBytes(entry.Path); PAKFileHeader header = new PAKFileHeader { Name = entry.RelativePath.ToCharArray(), Offset = mainHeader.DirectoryOffset, Size = contents.Length }; writer.Write(contents); mainHeader.DirectoryOffset += contents.Length; mainHeader.DirectoryLength += PAKFileHeader.SIZE_OF; fileHeaders.Add(header); } fileHeaders.ForEach(header => header.Write(writer)); stream.Position = startPosition; mainHeader.Write(writer); stream.Position = startPosition + mainHeader.DirectoryOffset + mainHeader.DirectoryLength; }
public void AddFiles(IEnumerable <string> files) { if (mStream != null && files.Any()) { string archiveDirectory = Path.GetDirectoryName(mArchivePath) + "\\"; BinaryWriter writer = new BinaryWriter(mStream, Encoding.UTF8, true); foreach (string file in files) { byte[] fileData = File.ReadAllBytes(file); writer.BaseStream.Seek(mMainHeader.mDirectoryOffset, SeekOrigin.Begin); writer.Write(fileData, 0, fileData.Length); PAKFileHeader header = new PAKFileHeader(); char[] relativePath = file.Replace(archiveDirectory, "").ToCharArray(); Array.Copy(relativePath, header.mFileName, relativePath.Length); header.mFileOffset = mMainHeader.mDirectoryOffset; header.mFileSize = fileData.Length; mHeaders.Add(header); mMainHeader.mDirectoryOffset += fileData.Length; mMainHeader.mDirectoryLength += PAKFileHeader.SIZE_OF; } writer.BaseStream.Seek(mMainHeader.mDirectoryOffset, SeekOrigin.Begin); foreach (PAKFileHeader h in mHeaders) { h.Write(writer); } writer.BaseStream.Seek(0, SeekOrigin.Begin); mMainHeader.Write(writer); OnEntriesUpdated(); } }