欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#压缩解压缩bzip2、tar、zip、gzip、deflate、ntdll

程序员文章站 2024-03-14 09:30:46
...

在压缩解压缩前需要将图片、文字、字符串等数据转化为字节数组byte[],
压缩解压缩以后再将字节数组byte[]转化为图片、文字、字符串。
转化过程请参考
http://blog.csdn.net/luanpeng825485697/article/details/77632483

本章只介绍字节数组的压缩解压缩。

以下压缩解压需要引用的空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using System.IO.Compression;
using System.Runtime.InteropServices;

bzip2压缩

public static byte[] bzip2Compress(byte[] data)
{
    MemoryStream ms = new MemoryStream();
    Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
    try
    {
        stream.Write(data, 0, data.Length);
    }
    finally
    {
        stream.Close();
        ms.Close();
    }
    return ms.ToArray();
}

bzip2解压

public static byte[] bzip2Decompress(byte[] buffer)
{
    MemoryStream ms = new MemoryStream(buffer);
    Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
    byte[] bytes = new byte[sm.Length];
    sm.Read(bytes, 0, bytes.Length);
    sm.Close();
    ms.Close();
    return bytes;
}

tar压缩

public static byte[] tarCompress(byte[] data)
{
    MemoryStream ms = new MemoryStream();
    Stream stream = new ICSharpCode.SharpZipLib.Tar.TarOutputStream(ms);
    try
    {
        stream.Write(data, 0, data.Length);
    }
    finally
    {
        stream.Close();
        ms.Close();
    }
    return ms.ToArray();
}

tar解压

public static byte[] tarDecompress(byte[] buffer)
{
    MemoryStream ms = new MemoryStream(buffer);
    Stream sm = new TarInputStream(ms);
    byte[] bytes = new byte[sm.Length];
    sm.Read(bytes, 0, bytes.Length);
    sm.Close();
    ms.Close();
    return bytes;
}

zip压缩数据流成文件,同时返回数据流

public static byte[] zipCompress(string filename, byte[] filecontent)
{
    byte[] b = new byte[] { };
    if (filecontent == null || filecontent.Length == 0)
        return b;
    MemoryStream ms = new MemoryStream();
    ZipOutputStream zipOS = new ZipOutputStream(ms);
    zipOS.SetLevel(6);
    ZipEntry entry = new ZipEntry(filename);
    entry.DateTime = DateTime.Now;
    entry.Size = filecontent.Length;
    //crc.Reset();
    //crc.Update(filecontent);
    //entry.Crc = crc.Value;
    zipOS.PutNextEntry(entry);
    zipOS.Write(filecontent, 0, filecontent.Length);
    if (zipOS != null)
    {
        zipOS.Finish();
        b = ms.ToArray();
        zipOS.Close();
        ms.Close();
    }
    return b;
}

zip压缩数据流

public static byte[] zipCompress(byte[] data)
{
    byte[] back = new byte[] { };
    MemoryStream ms = new MemoryStream();
    ZipOutputStream zipOS = new ZipOutputStream(ms);
    zipOS.SetLevel(6);
    zipOS.Write(data, 0, data.Length);
    if (zipOS != null)
    {
        zipOS.Finish();
        back = ms.ToArray();
        zipOS.Close();
        ms.Close();
    }
    return back;
}

zip解压数据流,没有测试

public static byte[] zipDecompress(byte[] data)
{
    MemoryStream ms = new MemoryStream(data);
    ZipInputStream zipStream = new ZipInputStream(ms);
    byte[] back = new byte[zipStream.Length];
    zipStream.Read(back, 0, back.Length);
    zipStream.Close();
    zipStream.Dispose();
    return back;
}

gzip字节数组压缩

public static byte[] gzipCompress(byte[] data)
{
    try
    {
        MemoryStream ms = new MemoryStream();
        GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
        zip.Write(data, 0, data.Length);
        zip.Close();
        byte[] buffer = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(buffer, 0, buffer.Length);
        ms.Close();
        return buffer;

    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }
}

gzip字节数组解压缩

 public static byte[] gzipDecompress(byte[] data)
 {
     try
     {
         MemoryStream ms = new MemoryStream(data);
         GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
         MemoryStream msreader = new MemoryStream();
         byte[] buffer = new byte[1024];
         while (true)
         {
             int reader = zip.Read(buffer, 0, buffer.Length);
             if (reader <= 0)
             {
                 break;
             }
             msreader.Write(buffer, 0, reader);
         }
         zip.Close();
         ms.Close();
         msreader.Position = 0;
         buffer = msreader.ToArray();
         msreader.Close();
         return buffer;
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }

deflate压缩字节数组

public static byte[] deflateCompress(byte[] sourceByte)
{
    //压缩到内存流中
    MemoryStream memoryStream = new MemoryStream();
    DeflateStream zipStream = new DeflateStream(memoryStream, CompressionMode.Compress, true);
    zipStream.Write(sourceByte, 0, sourceByte.Length);
    zipStream.Close();
    byte[] compressByte = memoryStream.ToArray();
    memoryStream.Close();
    return compressByte;
}

deflate解压字节数组

public static byte[] deflateDeCompress(byte[] sourceByte)
{
    MemoryStream memStream = new MemoryStream(sourceByte);
    memStream.Position = 0;
    DeflateStream zipStream = new DeflateStream(memStream, CompressionMode.Decompress);
    memStream = new MemoryStream();
    byte[] buff = new byte[1024];
    while (true)
    {
        int size = zipStream.Read(buff, 0, 512);
        memStream.Write(buff, 0, size);
        if (size < 512)
            break;
    }

    byte[] tempByte = memStream.ToArray();
    memStream.Close();
    return tempByte;
}

ntdll压缩

ntdll.dd在win系统中自带,所以不需要额外引用其他文件,但是需要在代码中引入函数

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[] ntdllCompress(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;
}

ntdll解压

public static byte[] ntdllDecompress(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;
}