static void Test(Compression compression, string originalFile, string report) { BinaryReader br = new BinaryReader(File.OpenRead(originalFile)); byte[] file = br.ReadBytes((int)br.BaseStream.Length); br.Close(); int[] offsetsToTest = compression.Scan(file, 0, file.Length, 4, 0, file.Length); int i; List<int> differentOffsets = new List<int>(50); for (i = 0; i < offsetsToTest.Length; i++) { bool success = true; byte[] data = compression.Compress(compression.Decompress(file, offsetsToTest[i])); byte[] data2 = new byte[compression.CompressedLength(file, offsetsToTest[i])]; Array.Copy(file, offsetsToTest[i], data2, 0, data2.Length); success = data.Length <= data2.Length; /*for (int j = 0; j < data.Length && success; j++) success = data[j] == data2[j];*/ if (!success) differentOffsets.Add(offsetsToTest[i]); } if (differentOffsets.Count == 0) { messagePoster("No differences were found."); } else { messagePoster("There is a difference."); StreamWriter sw = new StreamWriter(report, false); for (int j = 0; j < differentOffsets.Count; j++) { sw.WriteLine(Convert.ToString(differentOffsets[j], 16)); } sw.Close(); } }
static void Compress(Compression compression, string input, string output, int inputOffset, int length, int outputOffset) { if ((compression.supportedModes & CompressionOperation.Compress) == CompressionOperation.None) { messagePoster("Compression type doesn't support compressing."); return; } byte[] data; try { BinaryReader br = new BinaryReader(File.OpenRead(input)); br.BaseStream.Position = inputOffset; if (length == 0 || (br.BaseStream.Length - br.BaseStream.Position) < length) { length = (int)(br.BaseStream.Length - br.BaseStream.Position); } data = br.ReadBytes(length); br.Close(); } catch (IOException) { messagePoster("Error while opening the file."); return; } byte[] compData = compression.Compress(data); try { BinaryWriter bw = new BinaryWriter(File.Open(output, FileMode.OpenOrCreate)); bw.BaseStream.Position = outputOffset; bw.Write(compData); bw.Close(); } catch (IOException) { messagePoster("Error while opening output file."); return; } messagePoster("Finished"); }