public void Unpack(string hFilePath, string unpackPath) { using (BinaryReader binaryReader = new BinaryReader((Stream) new FileStream(this.getPath(Path.GetDirectoryName(hFilePath), Path.GetFileNameWithoutExtension(hFilePath) + ".~p"), FileMode.Open))) { ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar(this.header.Header.FileCount); try { consoleProgressBar.Start(); foreach (FileIndex file in this.header.Files) { binaryReader.BaseStream.Seek(file.Offset, SeekOrigin.Begin); consoleProgressBar.PerformStep(); Directory.CreateDirectory(Path.GetDirectoryName(this.getPath(unpackPath, file.GetFilePath()))); using (MemoryStream memoryStream = new MemoryStream()) { using (ZOutputStream zoutputStream = new ZOutputStream((Stream)memoryStream)) { if (file.IsCompressed()) { zoutputStream.Write(binaryReader.ReadBytes(file.Size), 0, file.Size); } else { memoryStream.Write(binaryReader.ReadBytes(file.Size), 0, file.Size); } memoryStream.Seek(0L, SeekOrigin.Begin); if ((long)file.ContentSize < memoryStream.Length) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(this.getPath(unpackPath, file.GetFilePath() + ".header"), FileMode.Create))) { byte[] buffer = new byte[memoryStream.Length - (long)file.ContentSize]; memoryStream.Read(buffer, 0, buffer.Length); binaryWriter.Write(buffer); } } using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(this.getPath(unpackPath, file.GetFilePath()), FileMode.Create))) { int contentSize = file.ContentSize; if (file.FileType.HasSizeHeader) { contentSize -= 4; } byte[] buffer = new byte[contentSize]; if (file.FileType.HasSizeHeader) { memoryStream.Read(buffer, 0, 4); } memoryStream.Read(buffer, 0, contentSize); binaryWriter.Write(buffer); } } } } } finally { consoleProgressBar.End(); } } }
private void DownloadNextFile() { Console.Clear(); Progress.Stop(); if (CurrentFileIndex >= Files.Count) { CurrentFile = null; return; } CurrentFile = Files[CurrentFileIndex]; if (!Directory.Exists(BasePath + CurrentFile.Directory)) { Directory.CreateDirectory(BasePath + CurrentFile.Directory); } if (File.Exists(BasePath + CurrentFile.Path)) { File.Delete(BasePath + CurrentFile.Path); } Client.DownloadFileAsync(new Uri(CurrentFile.Url), BasePath + CurrentFile.Path); Progress.Start(); }
/// <summary> /// Moves the files from the chunked archive to a flat file one /// </summary> /// <param name="archivename"></param> /// <param name="block"></param> private void DoFileSwap(string archivename, string block) { Directory.CreateDirectory("Temp"); string filename = Path.GetFileName(archivename); using (var mpq = MpqArchive.CreateNew(archivename, MpqArchiveVersion.Version3)) using (var tmp = new MpqArchive(block, FileAccess.Read, OpenArchiveFlags.BLOCK4)) { if (TryGetFileList(tmp, false, out var lf)) { _progressBar.Start(); string tempPath; for (int i = 0; i < lf.Count; i++) { using (var fs = tmp.OpenFile(lf[i])) { // incremental patch files can't be written directly with StormLib SFileCreateFile? if ((fs.TFileEntry.dwFlags & 0x100000u) != 0x100000u) { mpq.AddFileFromStream(fs); } else { tempPath = Path.Combine("Temp", lf[i]); Directory.CreateDirectory(Path.GetDirectoryName(tempPath)); tmp.ExtractFile(lf[i], tempPath); mpq.AddFileFromDisk(tempPath, lf[i], fs.TFileEntry.dwFlags); } } _progressBar.Update(filename, i / (float)lf.Count); if (i % 10000 == 0) { mpq.Flush(); } } } } Console.WriteLine("Emptying Temp Folder..."); DeleteDirectory("Temp"); _progressBar.Stop(); }
public void Pack(string path, string unpackPath, HFile hFile) { using (FileStream fileStream1 = new FileStream(path + ".~p", FileMode.Create)) { ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar(hFile.Header.FileCount); try { consoleProgressBar.Start(); for (int index = 0; index < hFile.Files.Length; ++index) { consoleProgressBar.PerformStep(); using (MemoryStream memoryStream = new MemoryStream()) { string path1 = unpackPath + "\\" + hFile.Files[index].GetFilePath(); if (File.Exists(path1 + ".header")) { using (FileStream fileStream2 = new FileStream(path1 + ".header", FileMode.Open)) this.CopyStream((Stream)fileStream2, (Stream)memoryStream); } using (FileStream fileStream2 = new FileStream(path1, FileMode.Open)) { hFile.Files[index].ContentSize = (int)fileStream2.Length; if (hFile.Files[index].FileType.HasSizeHeader) { memoryStream.Write(BitConverter.GetBytes(hFile.Files[index].ContentSize), 0, 4); hFile.Files[index].ContentSize += 4; } this.CopyStream((Stream)fileStream2, (Stream)memoryStream); } memoryStream.Seek(0L, SeekOrigin.Begin); hFile.Files[index].Offset = fileStream1.Position; if (hFile.Files[index].IsCompressed()) { int num = this.compress((Stream)memoryStream, (Stream)fileStream1, 9); if (num < 0) { throw new Exception(); } hFile.Files[index].Size = num; } else { hFile.Files[index].Size = (int)memoryStream.Length; this.CopyStream((Stream)memoryStream, (Stream)fileStream1); } if ((int)hFile.Header.Version[1] == 1) { if (fileStream1.Length % 4L != 0L) { fileStream1.Write(new byte[4L - fileStream1.Length % 4L], 0, (int)(4L - fileStream1.Length % 4L)); } } else if (fileStream1.Length % 2048L != 0L) { int count = (int)(2048L * (fileStream1.Length / 2048L + 1L) - fileStream1.Length); fileStream1.Write(new byte[count], 0, count); } } } hFile.Header.DataFooterOffset = (ulong)fileStream1.Position; int num1 = (int)ushort.MaxValue - (int)(fileStream1.Length & (long)ushort.MaxValue); for (int index = 0; index <= num1; ++index) { fileStream1.WriteByte((byte)122); } } finally { consoleProgressBar.End(); } } }