/** * * @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 { /// /// 压缩 解压功能组件 /// public static class ZipHelper { #region Fields private const int BufferLength = 1024; #endregion Fields #region Methods #region Public Static Methods /// /// 压缩文件 /// /// 需要压缩的文件 /// 压缩后的文件 /// 参数为空异常 /// 需要压缩的文件不存在 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"); } } /// /// 压缩流 /// /// 需要压缩的流 /// 压缩后的流 /// 参数为空异常 /// 无法读取需要压缩的流以及无法写入压缩的流. 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(); } /// /// 压缩字节数组 /// /// 需要压缩的字节数组 /// 参数为空异常 /// 需要压缩的字节数组长度不正确 /// 压缩后的字节数组 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; } /// /// 解压文件 /// /// 解压的文件 /// 压缩的文件 /// 参数为空异常 /// 需要压缩的文件不存在 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"); } } /// /// 解压缩流 /// /// 需要压缩的流 /// 压缩的流 /// 参数为空异常 /// 无法读取压缩的流以及无法写入解压的流. 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(); } /// /// 解压缩字节数组 /// /// 需要解压缩的字节数组 /// 参数为空异常 /// 压缩的字节数组长度不正确 /// 解压缩后的字节数组 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 } }