private void decodeButton_Click(object sender, EventArgs e) { openFileDialog1.Filter = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK && File.Exists(openFileDialog1.FileName)) { saveFileDialog1.FileName = new FileInfo(openFileDialog1.FileName).Name + ".decoded"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { var stream = new EncodedStream(File.ReadAllBytes(openFileDialog1.FileName)); stream.Seek(4, SeekOrigin.Current); byte[] buff = new byte[stream.Length]; buff[0] = 0x79; buff[1] = 0xCC; buff[2] = 0xB5; buff[3] = 0x6A; stream.Read(buff, 4, (int)stream.Length - 4); File.WriteAllBytes(saveFileDialog1.FileName, buff); stream.Close(); MessageBox.Show("File decoded!"); } } }
public void TestEncodedStream() { byte[] bytes = new byte[512]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = (byte)(i < 256 ? i : 511 - i); } MemoryStream ms = new MemoryStream(); using (EncodedStream stream = new EncodedStream(ms, new Base64Encoding(), false)) stream.Write(bytes, 0, bytes.Length); Assert.AreEqual((bytes.Length + 2) / 3 * 4, ms.Length); Assert.AreEqual(Convert.ToBase64String(bytes), SimpleEightBitEncoding.Instance.GetString(ms.ToArray())); ms.Position = 0; using (EncodedStream stream = new EncodedStream(ms, new Base64Encoding(), false)) { byte[] bytesRead = new byte[bytes.Length]; Assert.AreEqual(bytesRead.Length, stream.Read(bytesRead, 0, bytesRead.Length)); Assert.AreEqual(-1, stream.ReadByte()); TestHelpers.AssertArrayEquals(bytes, bytesRead); } }