static void Main() { FileStream source = File.OpenRead(@"D:\test.txt"); FileStream destination = File.Create(@"D:\archive.dfl"); DeflateStream compressor = new DeflateStream(destination, CompressionMode.Compress); int theByte = source.ReadByte(); while (theByte != -1) { compressor.WriteByte((byte)theByte); theByte = source.ReadByte(); } compressor.Close(); }
static Patch processChunk(byte[] orig, byte[] mod, int chunkStart, int chunkEnd) { int patchSize = chunkEnd - chunkStart; Debug.Assert(patchSize < Patch.MaxPatchSize); int matchAreaSize = Patch.MatchAreaSize(patchSize); int matchBefore = (matchAreaSize - patchSize) / 2; int matchAfter = matchAreaSize - (patchSize + matchBefore); Debug.Assert(matchBefore <= chunkStart); Debug.Assert(chunkEnd + matchAfter <= orig.Length); byte[] origBytes = new byte[matchAreaSize]; byte[] patchBytes = new byte[patchSize]; Array.ConstrainedCopy(orig, chunkStart - matchBefore, origBytes, 0, matchAreaSize); Array.ConstrainedCopy(mod, chunkStart, patchBytes, 0, patchSize); MemoryStream ms = new MemoryStream(); DeflateStream comp = new DeflateStream(ms, CompressionMode.Compress); comp.WriteByte(0); comp.Flush(); long base_len = ms.Length; comp.Write(origBytes, 0, origBytes.Length); comp.Flush(); return new Patch(origBytes, patchBytes, matchBefore, Convert.ToInt32(ms.Length - base_len)); }
static void Main(string[] args) { var filestream = File.Create("file.txt"); var writer = new StreamWriter(filestream); writer.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); Console.WriteLine("Init string:\n\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\""); writer.Close(); filestream = File.Open("file.txt", FileMode.Open); var memStream = new MemoryStream(); var copressWriter = new DeflateStream(memStream, CompressionMode.Compress); int readbyte = filestream.ReadByte(); while (readbyte != -1) { copressWriter.WriteByte((byte)readbyte); readbyte = filestream.ReadByte(); } copressWriter.Close(); filestream.Close(); File.Delete("file.txt"); filestream = File.Open("file.txt", FileMode.Create); filestream.Write(memStream.ToArray(), 0, memStream.ToArray().Length); filestream.Close(); var reader = File.OpenText("file.txt"); string readStr = reader.ReadToEnd(); reader.Close(); Console.WriteLine("\nString after archivation:"); Console.WriteLine(readStr); Console.ReadKey(); }
public void JunkAtTheEnd () { // Write a deflated stream, then some additional data... using (MemoryStream ms = new MemoryStream()) { // The compressed stream using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true)) { stream.WriteByte(1); stream.Flush(); } // Junk ms.WriteByte(2); ms.Position = 0; // Reading: this should not hang using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress)) { byte[] buffer = new byte[512]; int len = stream.Read(buffer, 0, buffer.Length); Console.WriteLine(len == 1); } } }