有一个一万个元素的数组,每个元素可能是0 3 8 21,如何压缩

解决方案 »

  1.   

    给你一个加密/解密的类:
    using System;
    using System.IO;
    using System.Text;
    using ICSharpCode.SharpZipLib.Zip;namespace MyClass
    {
    /// <summary>
    /// Summary description for Compression.
    /// </summary>
    public class Compression
    {
    public Compression()
    {
    } // The actual ZIP method
    public static string Zip(string stringToZip)
    {
    byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToZip);
    MemoryStream ms = new MemoryStream(); // Check the #ziplib docs for more information
    ZipOutputStream zipOut = new ZipOutputStream(ms);
    ZipEntry ZipEntry = new ZipEntry("ZippedFile");
    zipOut.PutNextEntry(ZipEntry);
    zipOut.SetLevel(9);
    zipOut.Write(inputByteArray, 0, inputByteArray.Length);
    zipOut.Finish();
    zipOut.Close(); // Return the zipped contents
    return Convert.ToBase64String(ms.ToArray());
    } // Actual Unzip logic
    public static string Unzip(string stringToUnzip)
    {
    // Decode the Base64 encoding
    byte[] inputByteArray = Convert.FromBase64String(stringToUnzip);
    MemoryStream ms = new MemoryStream(inputByteArray);
    MemoryStream ret = new MemoryStream(); // Refer to #ziplib documentation for more info on this
    ZipInputStream zipIn = new ZipInputStream(ms);
    ZipEntry theEntry = zipIn.GetNextEntry();
    Byte[] buffer = new Byte[2048];
    int size = 2048;
    while (true)
    {
    size = zipIn.Read(buffer, 0, buffer.Length);
    if (size > 0)
    {
    ret.Write(buffer, 0, size);
    }
    else
    {
    break;
    }
    }
    return Encoding.UTF8.GetString(ret.ToArray());
    }
    }
    }
      

  2.   

    不知道LZ 的 0 3 8 21 是什么,
    如果作为int看待的话话有一种不带扩充性的办法,
    是按照哈夫曼编码的实现思想来做的
    一个0作为int 看待 则有32个位来描述
    同样 3 8 21 也是
    则你可以这样表示 
    int  bit
    0 -- 00
    3 -- 01
    8 -- 10
    21-- 11
    也就是说用两个位就能表示一个LZ的数了,压缩率为 1/16 了
    当然这个方法实现是很简单的,但是扩充性就严重的差了
    当然你也可以用LS的SharpZip 进行压缩啦.
      

  3.   

    这个我已实现了
    http://blog.csdn.net/BlueDog/archive/2006/12/29/1466527.aspx