public void Write(string file, th_structure old_structure) { using (FileStream writer = new FileStream(file, FileMode.Create, FileAccess.Write)) { int offset_difference = 0; for (int i = 0; i < offsets.Count; i++) { offsets[i] += offset_difference; writer.Write(BitConverter.GetBytes(offsets[i]), 0, 8); writer.Write(BitConverter.GetBytes(lengths[i]), 0, 4); writer.Write(new byte[4] { 0, 0, 0, 0 }, 0, 4); writer.Write(BitConverter.GetBytes(lengths2[i]), 0, 4); writer.Write(new byte[4] { 0, 0, 0, 0 }, 0, 4); writer.Write(BitConverter.GetBytes(unknown[i]), 0, 1); writer.Write(new byte[7] { 0, 0, 0, 0, 0, 0, 0 }, 0, 7); offset_difference += lengths[i] - old_structure.lengths[i]; } writer.Close(); } }
public FileController(string directory) { data_0 = directory + Path.DirectorySeparatorChar + "DATA0.bin"; data_1 = directory + Path.DirectorySeparatorChar + "DATA1.bin"; if (File.Exists(data_0)) { Structure = new th_structure(data_0); } else { throw new ArgumentException("Couldn't find Data0.bin", "FileController"); } if (File.Exists(data_1)) { fs = new FileStream(directory + Path.DirectorySeparatorChar + "DATA1.bin", FileMode.Open, FileAccess.Read); } else { throw new ArgumentException("Couldn't find Data1.bin", "FileController"); } // Add Tables to list Main_Tables.Add(0, new TableController(fs, Structure.offsets[0])); Main_Tables.Add(1, new TableController(fs, Structure.offsets[1])); Main_Tables.Add(2, new TableController(fs, Structure.offsets[2])); Main_Tables.Add(3, new TableController(fs, Structure.offsets[3])); Main_Tables.Add(4, new TableController(fs, Structure.offsets[4])); Main_Tables.Add(11, new TableController(fs, Structure.offsets[11])); Main_Tables.Add(12, new TableController(fs, Structure.offsets[12])); fs.Close(); worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = false; worker.WorkerReportsProgress = true; worker.DoWork += DoWork; }
private void DoWork(object sender, DoWorkEventArgs e) { string data_0_new = writing_directory + Path.DirectorySeparatorChar + "DATA0.bin"; string data_1_new = writing_directory + Path.DirectorySeparatorChar + "DATA1.bin"; fs = new FileStream(data_1, FileMode.Open, FileAccess.Read); // Begin writing DATA1.bin using (FileStream writer = new FileStream(data_1_new, FileMode.Create, FileAccess.Write)) { // Write table data for (int i = 0; i < Main_Tables.Count; i++) { KeyValuePair <int, TableController> entry = Main_Tables.ElementAt(i); List <byte> current_table = entry.Value.Write(); writer.Write(current_table.ToArray(), 0, current_table.Count); write_nullbytes(entry.Key, writer); // Write chunks from original file if files are getting skipped if (!Main_Tables.ContainsKey(entry.Key + 1) && i < Main_Tables.Count - 1) { int current_entry = entry.Key; int next_entry = Main_Tables.ElementAt(i + 1).Key; int chunk_area = (int)(Structure.offsets[next_entry] - Structure.offsets[current_entry + 1]); byte[] chunks = read_seek(Structure.offsets[current_entry + 1], chunk_area); writer.Write(chunks, 0, chunk_area); } } // Begin appending old, unedited data to new file fs.Seek(Structure.offsets[Main_Tables.Last().Key + 1], SeekOrigin.Begin); // create a buffer to hold the bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int bytesRead = -1; long totalReads = 0; int prevPercent = 0; long totalBytes = fs.Length; // while the read method returns bytes // keep writing them to the output stream while ((bytesRead = fs.Read(buffer, 0, bufferSize)) > 0) { writer.Write(buffer, 0, bytesRead); totalReads += bytesRead; int percent = Convert.ToInt32(((decimal)totalReads / (decimal)totalBytes) * 100); if (percent != prevPercent) { worker.ReportProgress(percent); prevPercent = percent; } } writer.Close(); } // Begin writing DATA0.bin th_structure Structure_New = new th_structure(data_0); foreach (KeyValuePair <int, TableController> entry in Main_Tables) { Structure_New.lengths[entry.Key] = entry.Value.total_length; } Structure_New.Write(data_0_new, Structure); fs.Close(); }