MemoryStream Deflate(byte[] data, int level, bool zlib) { MemoryStream memoryStream = new MemoryStream(); Deflater deflater = new Deflater(level, !zlib); using ( DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater) ) { outStream.IsStreamOwner = false; outStream.Write(data, 0, data.Length); outStream.Flush(); outStream.Finish(); } return memoryStream; }
public void DeflatorStreamOwnership() { MemoryStreamEx memStream = new MemoryStreamEx(); DeflaterOutputStream s = new DeflaterOutputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.Close(); Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new MemoryStreamEx(); s = new DeflaterOutputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.IsStreamOwner = false; s.Close(); Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); }
public void CloseInflatorWithNestedUsing() { string tempFile = null; try { tempFile = Path.GetTempPath(); } catch (SecurityException) { } Assert.IsNotNull(tempFile, "No permission to execute this test?"); if (tempFile != null) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (FileStream diskFile = File.Create(tempFile)) using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) using (StreamWriter textWriter = new StreamWriter(deflator)) { textWriter.Write("Hello"); textWriter.Flush(); } using (FileStream diskFile = File.OpenRead(tempFile)) using (InflaterInputStream deflator = new InflaterInputStream(diskFile)) using (StreamReader textReader = new StreamReader(deflator)) { char[] buffer = new char[5]; int readCount = textReader.Read(buffer, 0, 5); Assert.AreEqual(5, readCount); StringBuilder b = new StringBuilder(); b.Append(buffer); Assert.AreEqual("Hello", b.ToString()); } File.Delete(tempFile); } }
public void CloseDeflatorWithNestedUsing() { string tempFile = null; try { tempFile = Path.GetTempPath(); } catch (SecurityException) { } Assert.IsNotNull(tempFile, "No permission to execute this test?"); if (tempFile != null) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (FileStream diskFile = File.Create(tempFile)) using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) using (StreamWriter txtFile = new StreamWriter(deflator)) { txtFile.Write("Hello"); txtFile.Flush(); } File.Delete(tempFile); } }