Ejemplo n.º 1
0
 public static void Compress(byte[] dest, ref int destLength, byte[] source)
 {
     using (MemoryStream stream = new MemoryStream(dest, true))
     {
         using (ZLIBStream ds = new ZLIBStream(stream, CompressionMode.Compress, true))
         {
             ds.Write(source, 0, source.Length);
             ds.Flush();
         }
         destLength = (int)stream.Position;
     }
 }
Ejemplo n.º 2
0
        public static void ZLibDecompress(Stream stream, Stream tragetStream)
        {
            using (var zs = new ZLIBStream(stream, CompressionMode.Decompress, true)) {
                int    bytesLeidos = 0;
                byte[] buffer      = new byte[1024];

                while ((bytesLeidos = zs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    tragetStream.Write(buffer, 0, bytesLeidos);
                }
            }
        }
Ejemplo n.º 3
0
 public static void Decompress(byte[] source, int sourceStart, int sourceLength, int offset, byte[] dest, int length)
 {
     using (MemoryStream stream = new MemoryStream(source, sourceStart, sourceLength - offset, true))
     {
         using (ZLIBStream ds = new ZLIBStream(stream, CompressionMode.Decompress))
         {
             for (int i = 0, b = ds.ReadByte(); i < length && b >= 0; i++, b = ds.ReadByte())
             {
                 dest[i] = (byte)b;
             }
         }
     }
 }
Ejemplo n.º 4
0
 public static unsafe void Decompress(IntPtr source, int sourceLength, int offset, IntPtr dest, int length)
 {
     using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte *)source.ToPointer(), sourceLength - offset))
     {
         using (ZLIBStream ds = new ZLIBStream(stream, CompressionMode.Decompress))
         {
             byte *dstPtr = (byte *)dest.ToPointer();
             for (int i = 0, b = ds.ReadByte(); i < length && b >= 0; i++, b = ds.ReadByte())
             {
                 dstPtr[i] = (byte)b;
             }
         }
     }
 }
Ejemplo n.º 5
0
        public static void ZLibCompress(Stream stream, Stream targetStream, CompressionLevel?level = null)
        {
            using (var zs = new ZLIBStream(targetStream, level ?? CompressionLevel.Optimal, true)) {
                int    bytesLeidos = 0;
                byte[] buffer      = new byte[1024];

                stream.Seek(0, SeekOrigin.Begin);

                while ((bytesLeidos = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    zs.Write(buffer, 0, bytesLeidos);
                }
            }

            using (var bw = new BinaryWriter(targetStream))
                bw.Write((uint)stream.Length);
        }