public byte[] Compress(byte[] input) { // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.SetLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.SetInput(input); compressor.Finish(); /* * Create an expandable byte array to hold the compressed data. * You cannot use an array that's the same size as the orginal because * there is no guarantee that the compressed data will be smaller than * the uncompressed data. */ MemoryStream bos = new MemoryStream(input.Length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.IsFinished) { int count = compressor.Deflate(buf); bos.Write(buf, 0, count); } // Get the compressed data return bos.ToArray(); }
/// <summary>Compresses the specified byte range using the /// specified compressionLevel (constants are defined in /// java.util.zip.Deflater). /// </summary> public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel) { /* Create an expandable byte array to hold the compressed data. * You cannot use an array that's the same size as the orginal because * there is no guarantee that the compressed data will be smaller than * the uncompressed data. */ System.IO.MemoryStream bos = new System.IO.MemoryStream(length); Deflater compressor = new Deflater(); try { compressor.SetLevel(compressionLevel); compressor.SetInput(value_Renamed, offset, length); compressor.Finish(); // Compress the data byte[] buf = new byte[1024]; while (!compressor.IsFinished) { int count = compressor.Deflate(buf); bos.Write(buf, 0, count); } } finally { } return bos.ToArray(); }
public static byte[] Compress(byte[] content) { //return content; Deflater compressor = new Deflater(); compressor.SetLevel(Deflater.BEST_COMPRESSION); compressor.SetInput(content); compressor.Finish(); using (MemoryStream bos = new MemoryStream(content.Length)) { var buf = new byte[1024]; while (!compressor.IsFinished) { int n = compressor.Deflate(buf); bos.Write(buf, 0, n); } return bos.ToArray(); } }
public static byte[] ZipBytes(byte[] input) { MemoryStream ms = new MemoryStream(); Deflater zipper = new Deflater(); zipper.SetLevel(5); Stream st = new DeflaterOutputStream(ms, zipper); st.Write(input, 0, input.Length); st.Flush(); st.Close(); byte[] result = (byte[])ms.ToArray(); return result; }