首页 > 其他 > 详细

C#压缩解压文件及文件夹Helper

时间:2014-03-14 15:21:55      阅读:390      评论:0      收藏:0      [点我收藏+]

通过C#原生的ZipFile、GZipStream进行文件夹和文件的压缩和解压处理.

using System.IO;
using System.IO.Compression;


namespace LinqXml
{
    public class CompressionHelper
    {
        #region ------------------------------------------------------------------1、解压文件为一个文件或文件夹
        /// <summary>
        /// 解压文件为一个文件或文件夹
        /// </summary>
        /// <param name="sourceFile">源文件:准备解压的压缩文件</param>
        /// <param name="destinationDirectory">目标目录:解压缩之后的文件存放位置</param>
        public static void DeCompressToDirectory(string sourceFile, string destinationDirectory)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException();
            ZipFile.ExtractToDirectory(sourceFile, destinationDirectory);            
        }
        #endregion


        #region ------------------------------------------------------------------2、压缩文件夹或目录为一个文件
        /// <summary>
        /// 压缩一个文件夹或目录为一个文件
        /// </summary>    
        /// <param name="sourceDirectory">源文件:准备压缩的文件夹</param>
        /// <param name="destinationFile">目标文件:压缩后保存的文件</param>
        public static void CompressFromDirectory(string sourceDirectory, string destinationFile)
        {
            if (!Directory.Exists(sourceDirectory)) throw new DirectoryNotFoundException();
            ZipFile.CreateFromDirectory(sourceDirectory, destinationFile);
        }
        #endregion




        #region ------------------------------------------------------------------3、压缩一个文件为一个文件
        /// <summary>
        /// 压缩一个文件为一个文件
        /// </summary>
        /// <param name="sourceFile">源文件:准备压缩的文件</param>
        /// <param name="destinationFile">目标文件:压缩后保存的文件</param>
        public static void CompressFromFile(string sourceFile, string destinationFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException();


            using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
            {
                byte[] quartetBuffer = new byte[4];
                const int bufferLength = 1024 * 64;
                /*压缩文件的流的最后四个字节保存的是文件未压缩前的长度信息,
                 * 把该字节数组转换成int型,可获取文件长度。
                 * int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);*/


                byte[] buffer = new byte[1024 * 64];
                using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Compress, true))
                {
                    using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
                    {
                        int bytesRead = 0;
                        while ((bytesRead = decompressedStream.Read(buffer, 0, bufferLength)) >= bufferLength)
                        {
                            destinationStream.Write(buffer, 0, bufferLength);
                        }
                        destinationStream.Write(buffer, 0, bytesRead);
                        destinationStream.Flush();
                    }
                }
            }
        }
        #endregion


        #region ------------------------------------------------------------------4、解压一个文件为一个文件
        /// <summary>
        /// 解压一个文件为一个文件
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="destinationFile">目标文件</param>
        public static void DeCompressToFile(string sourceFile, string destinationFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException();


            using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
            {
                byte[] quartetBuffer = new byte[4];
                const int bufferLength = 1024 * 64;
                /*压缩文件的流的最后四个字节保存的是文件未压缩前的长度信息,
                 * 把该字节数组转换成int型,可获取文件长度。
                 * int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);*/


                byte[] buffer = new byte[1024 * 64];
                using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
                {
                    using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
                    {
                        int bytesRead = 0;
                        while ((bytesRead = decompressedStream.Read(buffer, 0, bufferLength)) >= bufferLength)
                        {
                            destinationStream.Write(buffer, 0, bufferLength);
                        }
                        destinationStream.Write(buffer, 0, bytesRead);
                        destinationStream.Flush();
                    }
                }
            }
        }
        #endregion




        #region ------------------------------------------------------------------5、压缩一个文件为一个流
        /// <summary>
        /// 把一个文件压缩为一个流
        /// </summary>
        /// <param name="sourceFile">源文件的全部名称(路径+文件名)</param>
        /// <returns>压缩后的流</returns>
        public static Stream CompressStream(string sourceFile)
        {
            if (!File.Exists(sourceFile)) throw new FileNotFoundException();
            using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
            {
                GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Compress, true);
                return decompressedStream;
            }
        }
        #endregion


        #region ------------------------------------------------------------------6、压缩一个流
        /// <summary>
        /// 对一个流进行压缩
        /// </summary>
        /// <param name="sourceStream">源流</param>
        /// <returns>压缩后的流</returns>
        public static Stream CompressStream(Stream sourceStream)
        {
            GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Compress, true);
            return decompressedStream;
        }
        #endregion


        #region ------------------------------------------------------------------7、解压一个流
        /// <summary>
        /// 对一个流进行解压
        /// </summary>
        /// <param name="sourceStream">源流</param>
        /// <returns>解压后的流</returns>
        public static Stream DeCompressStream(Stream sourceStream)
        {
            GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
            return decompressedStream;
        }
        #endregion
    }
}


C#压缩解压文件及文件夹Helper,布布扣,bubuko.com

C#压缩解压文件及文件夹Helper

原文:http://blog.csdn.net/lxhjh/article/details/21228959

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!