zoukankan      html  css  js  c++  java
  • 利用ICSharpCode.SharpZipLib进行压缩

         #ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language).#ZipLib was ported from the GNU Classpath ZIP library for use with #Develop (http://www.icsharpcode.net/OpenSource/SD) which needed gzip/zip compression.  Later bzip2 compression and tar archiving was added due to popular demand.
    官方下载:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
       ZipLib组件与.net自带的Copression比较,在压缩方面更胜一筹,经过BZip2压缩要小很多,亲手测试,不信你也可以试一试。而且这个功能更加强大。下面就是个人做的一个小例子,具体的应用程序源码: /Files/yank/Compress.rar

     1using System;
     2using System.Data;
     3using System.IO;
     4using ICSharpCode.SharpZipLib.Zip.Compression;
     5using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
     6using ICSharpCode.SharpZipLib.GZip;
     7
     8/// <summary>
     9/// Summary description for ICSharp
    10/// </summary>

    11public class ICSharp
    12{
    13    public ICSharp()
    14    {
    15        //
    16        // TODO: Add constructor logic here
    17        //
    18    }

    19    /// <summary>
    20    /// 压缩
    21    /// </summary>
    22    /// <param name="param"></param>
    23    /// <returns></returns>

    24    public string Compress(string param)
    25    {
    26        byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
    27        //byte[] data = Convert.FromBase64String(param);
    28        MemoryStream ms = new MemoryStream();
    29        Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
    30        try
    31        {
    32            stream.Write(data, 0, data.Length);
    33        }

    34        finally 
    35        {
    36            stream.Close();
    37            ms.Close();
    38        }

    39        return Convert.ToBase64String(ms.ToArray());
    40    }

    41    /// <summary>
    42    /// 解压
    43    /// </summary>
    44    /// <param name="param"></param>
    45    /// <returns></returns>

    46    public string Decompress(string param)
    47    {
    48        string commonString="";
    49        byte[] buffer=Convert.FromBase64String(param);
    50        MemoryStream ms = new MemoryStream(buffer);
    51        Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
    52        //这里要指明要读入的格式,要不就有乱码
    53        StreamReader reader = new StreamReader(sm,System.Text.Encoding.UTF8);
    54        try
    55        {
    56            commonString=reader.ReadToEnd();
    57        }

    58        finally
    59        {
    60            sm.Close();
    61            ms.Close();
    62        }

    63        return commonString;
    64    }

    65}

    Encoding.UTF8与Convert.FromBase64String
          获取 UTF-8 格式的编码。
    Unicode 标准为所有支持脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码方式。Unicode 标准 3.2 版使用下列 UTF: 
          UTF-8,它将每个码位表示为一个由 1 至 4 个字节组成的序列。
          UTF-16,它将每个码位表示为一个由 1 至 2 个 16 位整数组成的序列。
          UTF-32,它将每个码位表示为一个 32 位整数。


    Convert.FromBase64String 方法
    将指定的 String(它将二进制数据编码为 base 64 数字)转换成等效的 8 位无符号整数数组。
    它的参数也又一定的要求:

    参数是 由基 64 数字、空白字符和尾随填充字符组成。从零开始以升序排列的以 64 为基的数字为大写字符“A”到“Z”、小写字符“a”到“z”、数字“0”到“9”以及符号“+”和“/”。空白字符为 Tab、空格、回车和换行。s 中可以出现任意数目的空白字符,因为所有空白字符都将被忽略。无值字符“=”用于尾部的空白。s 的末尾可以包含零个、一个或两个填充字符。
    异常:

    异常类型 条件
    ArgumentNullException s 为空引用(Visual Basic 中为 Nothing)。
    FormatException s 的长度(忽略空白字符)小于 4。

    - 或 -

    s 的长度(忽略空白字符)不是 4 的偶数倍。

    s 的长度(忽略空白字符)不是 4 的偶数倍。


  • 相关阅读:
    PP常用bapi
    sap 常用表
    MM常用BADI
    submit 后台运行代码
    SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0
    SAP 创建物料主数据分类视图特性
    去读物料的分类值
    SAP 常用函数
    SAP 禁止某个库位的货物移动
    SAP 打开账期
  • 原文地址:https://www.cnblogs.com/yank/p/967515.html
Copyright © 2011-2022 走看看