| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /**
- *
- * @date 2014-10-23
- *
- * @author 邓尹平
- *
- * @par 成员列表类 说明
- * 主要负责管理成员 提供搜索\排序和操作的方法
- *
- * @par 版权声明
- * 深圳市多元世纪信息技术有限公司 版权所有
- *
- * @see 使用此类时参照一些其他类可以写在这里
- *
- * @todo 该类有待完成的任务 一条条列出 完成一条删除一条
- *
- * @bug 该类已知的Bug一条条列出 完成一条删除一条
- *
- */
- using System;
- using System.IO;
- using System.IO.Compression;
- namespace Muchinfo.PC.Common.Bosn
- {
- /// <summary>
- /// 压缩 解压功能组件
- /// </summary>
- public static class ZipHelper
- {
- #region Fields
- private const int BufferLength = 1024;
- #endregion Fields
- #region Methods
- #region Public Static Methods
- /// <summary>
- /// 压缩文件
- /// </summary>
- /// <param name="sourceFile">需要压缩的文件</param>
- /// <param name="zippedFile">压缩后的文件</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="FileNotFoundException">需要压缩的文件不存在</exception>
- public static void Compress(string sourceFile, string zippedFile)
- {
- if (string.IsNullOrEmpty(sourceFile))
- throw new ArgumentNullException("sourceFile");
- if (string.IsNullOrEmpty(zippedFile))
- throw new ArgumentNullException("zippedFile");
- if (File.Exists(sourceFile))
- {
- var streamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
- var streamZipped = new FileStream(zippedFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
- Compress(streamSource, streamZipped);
- streamSource.Close();
- streamZipped.Close();
- }
- else
- {
- throw new FileNotFoundException("需要压缩的文件不存在.", "sourceFile");
- }
- }
- /// <summary>
- /// 压缩流
- /// </summary>
- /// <param name="source">需要压缩的流</param>
- /// <param name="zipped">压缩后的流</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="ArgumentException">无法读取需要压缩的流以及无法写入压缩的流.</exception>
- public static void Compress(Stream source, Stream zipped)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- if (zipped == null)
- throw new ArgumentNullException("zipped");
- if (!source.CanRead)
- throw new ArgumentException("无法读取需要压缩的流.", "source");
- if (!zipped.CanWrite)
- throw new ArgumentException("无法写入压缩的流.", "zipped");
- var zipper = new GZipStream(zipped, CompressionMode.Compress);
- var readBuffer = new byte[BufferLength];
- int readCount = source.Read(readBuffer, 0, BufferLength);
- while (readCount != 0)
- {
- zipper.Write(readBuffer, 0, readCount);
- readCount = source.Read(readBuffer, 0, BufferLength);
- }
- zipper.Close();
- }
- /// <summary>
- /// 压缩字节数组
- /// </summary>
- /// <param name="source">需要压缩的字节数组</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="ArgumentException">需要压缩的字节数组长度不正确</exception>
- /// <returns>压缩后的字节数组</returns>
- public static byte[] Compress(byte[] source)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- if (source.Length == 0)
- throw new ArgumentException("需要压缩的字节数组长度不正确.", "source");
- var streamSource = new MemoryStream(source);
- var streamZipped = new MemoryStream();
- Compress(streamSource, streamZipped);
- streamSource.Close();
- streamZipped.Position = 0;
- byte[] zipped = streamZipped.ToArray();
- streamZipped.Close();
- return zipped;
- }
- /// <summary>
- /// 解压文件
- /// </summary>
- /// <param name="targetFile">解压的文件</param>
- /// <param name="zippedFile">压缩的文件</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="FileNotFoundException">需要压缩的文件不存在</exception>
- public static void Decompress(string zippedFile, string targetFile)
- {
- if (string.IsNullOrEmpty(targetFile))
- throw new ArgumentNullException("targetFile");
- if (string.IsNullOrEmpty(zippedFile))
- throw new ArgumentNullException("zippedFile");
- if (File.Exists(zippedFile))
- {
- var streamTarget = new FileStream(targetFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
- var streamZipped = new FileStream(zippedFile, FileMode.Open, FileAccess.Read, FileShare.Read);
- Decompress(streamZipped, streamTarget);
- streamTarget.Close();
- streamZipped.Close();
- }
- else
- {
- throw new FileNotFoundException("需要压缩的文件不存在.", "sourceFile");
- }
- }
- /// <summary>
- /// 解压缩流
- /// </summary>
- /// <param name="target">需要压缩的流</param>
- /// <param name="zipped">压缩的流</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="ArgumentException">无法读取压缩的流以及无法写入解压的流.</exception>
- public static void Decompress(Stream zipped, Stream target)
- {
- if (target == null)
- throw new ArgumentNullException("target");
- if (zipped == null)
- throw new ArgumentNullException("zipped");
- if (!zipped.CanRead)
- throw new ArgumentException("无法读取压缩的流.", "zipped");
- if (!target.CanWrite)
- throw new ArgumentException("无法写入解压的流.", "target");
- var zipper = new GZipStream(zipped, CompressionMode.Decompress);
- var readBuffer = new byte[BufferLength];
- int readCount = zipper.Read(readBuffer, 0, BufferLength);
- while (readCount != 0)
- {
- target.Write(readBuffer, 0, readCount);
- readCount = zipper.Read(readBuffer, 0, BufferLength);
- }
- target.Position = 0;
- zipper.Close();
- }
- /// <summary>
- /// 解压缩字节数组
- /// </summary>
- /// <param name="zipped">需要解压缩的字节数组</param>
- /// <exception cref="ArgumentNullException">参数为空异常</exception>
- /// <exception cref="ArgumentException">压缩的字节数组长度不正确</exception>
- /// <returns>解压缩后的字节数组</returns>
- public static byte[] Decompress(byte[] zipped)
- {
- if (zipped == null)
- throw new ArgumentNullException("zipped");
- if (zipped.Length == 0)
- throw new ArgumentException("压缩的字节数组长度不正确.", "zipped");
- var streamTarget = new MemoryStream();
- var streamZipped = new MemoryStream(zipped);
- Decompress(streamZipped, streamTarget);
- streamZipped.Close();
- streamTarget.Position = 0;
- byte[] target = streamTarget.ToArray();
- streamTarget.Close();
- return target;
- }
- #endregion Public Static Methods
- #endregion Methods
- }
- }
|