public Stream CreateReadStream() { FileStream fs = null; try { fs = File.OpenRead(fpkPath); fs.Seek(offset, SeekOrigin.Begin); fpkSubfileFormat format = fpkSubfileFormat.GetFormat(fs, size); return(format.ReadStream(fs)); } catch (Exception e) { if (fs != null) { fs.Close(); } throw e; } }
void writeArchiveWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; string backup = null; string dirName = Path.GetDirectoryName(destPath); if (dirName == String.Empty) { dirName = @".\"; } DirectoryInfo dir = new DirectoryInfo(dirName); if (!dir.Exists) { dir.Create(); } if (File.Exists(destPath)) { backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(destPath)); File.Move(destPath, backup); if (destPath.Equals(this.FilePath, StringComparison.InvariantCultureIgnoreCase)) { for (int i = 0; i < Subfiles.Count; i++) { fpkSubfile subfile = Subfiles[i] as fpkSubfile; if ((subfile != null) && subfile.fpkPath.Equals(this.FilePath, StringComparison.InvariantCultureIgnoreCase)) { subfile.fpkPath = backup; } } } } try { using (BinaryWriter writer = new BinaryWriter(File.Create(destPath))) { this.DirFormat.WriteHeader(writer.BaseStream, this.Subfiles); int offset = (int)writer.BaseStream.Position; int[] sizes = new int[Subfiles.Count]; int[] CRCs = new int[Subfiles.Count]; for (int i = 0; i < Subfiles.Count; i++) { if (worker.CancellationPending) { e.Cancel = true; break; } worker.ReportProgress(i * 100 / Subfiles.Count); fpkSubfile subfile = Subfiles[i] as fpkSubfile; if (subfile != null) { using (BinaryReader reader = new BinaryReader(File.OpenRead(subfile.fpkPath))) { reader.BaseStream.Seek(subfile.offset, SeekOrigin.Begin); uint readSteps = subfile.size / Utility.BufSize; for (int j = 0; j < readSteps; j++) { writer.Write(reader.ReadBytes(Utility.BufSize)); } writer.Write(reader.ReadBytes(subfile.size % Utility.BufSize)); } CRCs[i] = this.DirFormat is fpkDirFormat_Style2 && subfile.crc == 0 ? fpkDirFormat_Style2.Hash(Utility.EncodingShiftJIS.GetBytes(Subfiles[i].Name)) : subfile.crc; } else { fpkSubfileFormat format = compress ? (fpkSubfileFormat) new fpkSubfileFormat_ZLC2() : (fpkSubfileFormat) new fpkSubfileFormat_Uncompressed(); Stream stream = format.WriteStream(writer.BaseStream); Subfiles[i].WriteTo(stream); format.FinishWriteTo(stream); CRCs[i] = this.DirFormat is fpkDirFormat_Style2? fpkDirFormat_Style2.Hash(Utility.EncodingShiftJIS.GetBytes(Subfiles[i].Name)) : 0; } int pos = (int)writer.BaseStream.Position; sizes[i] = pos - offset; offset = pos; } if (!e.Cancel) { this.DirFormat.WriteFooter(writer.BaseStream, this.Subfiles, sizes, CRCs); } } if (e.Cancel) { RestoreBackup(destPath, backup); } else { this.FilePath = destPath; if ((backup != null) && !keepBackup) { File.Delete(backup); } } } catch (Exception ex) { RestoreBackup(destPath, backup); Utility.ReportException(ex); } }