C#对byte数组压缩和解压
来源:互联网 发布:centos和redhat哪个好 编辑:程序博客网 时间:2024/06/06 02:02
直接上代码
public class ByteHelper { public const ushort COMPRESSION_FORMAT_LZNT1 = 2; public const ushort COMPRESSION_ENGINE_MAXIMUM = 0x100; [DllImport("ntdll.dll")] public static extern uint RtlGetCompressionWorkSpaceSize(ushort dCompressionFormat, out uint dNeededBufferSize, out uint dUnknown); [DllImport("ntdll.dll")] public static extern uint RtlCompressBuffer(ushort dCompressionFormat, byte[] dSourceBuffer, int dSourceBufferLength, byte[] dDestinationBuffer, int dDestinationBufferLength, uint dUnknown, out int dDestinationSize, IntPtr dWorkspaceBuffer); [DllImport("ntdll.dll")] public static extern uint RtlDecompressBuffer(ushort dCompressionFormat, byte[] dDestinationBuffer, int dDestinationBufferLength, byte[] dSourceBuffer, int dSourceBufferLength, out uint dDestinationSize); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr LocalAlloc(int uFlags, IntPtr sizetdwBytes); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr LocalFree(IntPtr hMem); public static byte[] Decompress(byte[] buffer) { var outBuf = new byte[buffer.Length * 6]; uint dwSize = 0, dwRet = 0; uint ret = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, out dwSize, out dwRet); if (ret != 0) return null; ret = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, outBuf, outBuf.Length, buffer, buffer.Length, out dwRet); if (ret != 0) return null; Array.Resize(ref outBuf, (Int32)dwRet); return outBuf; } public static byte[] Compress(byte[] buffer) { var outBuf = new byte[buffer.Length * 6]; uint dwSize = 0, dwRet = 0; uint ret = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, out dwSize, out dwRet); if (ret != 0) return null; int dstSize = 0; IntPtr hWork = LocalAlloc(0, new IntPtr(dwSize)); ret = RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buffer, buffer.Length, outBuf, outBuf.Length, 0, out dstSize, hWork); if (ret != 0) return null; LocalFree(hWork); Array.Resize(ref outBuf, dstSize); return outBuf; } }
0 0
- C#对byte数组压缩和解压
- C# 对字符串的压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- C#压缩和解压
- c#实现压缩和解压
- C# 压缩和解压文件
- C#字符串压缩和解压
- 【Java】java和android网络编程 - 对byte数组压缩和解压缩(zip,gzip,bzip2,jzlib)
- .net 实现对文件对流对byte数组的压缩
- PHP 对字符串进行压缩和解压
- java对jar的解压和压缩
- GZip对字符串压缩和解压
- 对文件的压缩和解压
- java对文件压缩和解压
- JAVA对数据进行压缩和解压
- java对zip进行压缩和解压
- 如何执行hbase 的mapreduce job
- 1:java基本数据类型
- magento产品数据库表结构分析[magento二次开发]
- oracle 下操作blob字段是否会产生大量redo
- cocos2dx --- Widget 加载 CCNode
- C#对byte数组压缩和解压
- ubuntu搭建Hadoop-2.2.0(单机)
- SVN迁移到Git的过程(+ 一些技巧)
- android camera hal(omap)
- Selenium WebDriver使用经验杂记
- 双核处理器ARM+DSP如何实现协同工作
- Unity3D教程:静态调用C#的dll
- JDBC 直接调用存储过程
- 单例模式中,懒汉式和饿汉式的区别