压缩及解压缩文件

using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace Zip
{
   
    /// <summary>
    /// 压缩
    /// </summary>
    public class MyCompress
    {
        private static readonly object compressLock = new object();
        private static readonly object uncompressLock = new object();
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="FileToZip">要压缩的文件</param>
        /// <param name="ZipedPath">压缩后文件路径</param>
        /// <param name="CompressionLevel">压缩级别</param>
        /// <param name="BlockSize">文件块大小</param>
        public static void ZipFile(string FileToZip, string ZipedPath, int CompressionLevel, int BlockSize)
        {
            lock (compressLock)
            {
                //如果文件没有找到,则报错
                if (!System.IO.File.Exists(FileToZip))
                {
                    throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
                }
                #region 压缩后文件存放路径
                string fileName;
                string ZipedFile;
                fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1);
                ZipedFile = ZipedPath + "\\" + fileName + ".zip";
                if (!Directory.Exists(ZipedPath))
                    Directory.CreateDirectory(ZipedPath);
                #endregion
                using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile))
                    {
                        using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
                        {
                            ZipEntry ZipEntry = new ZipEntry(fileName);
                            ZipEntry.DateTime = (new System.IO.FileInfo(FileToZip)).LastWriteTime;
                            ZipStream.PutNextEntry(ZipEntry);
                            ZipStream.SetLevel(CompressionLevel);
                            byte[] buffer = new byte[BlockSize];
                            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
                            ZipStream.Write(buffer, 0, size);
                            try
                            {
                                while (size < StreamToZip.Length)
                                {
                                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                                    ZipStream.Write(buffer, 0, sizeRead);
                                    size += sizeRead;
                                }
                            }
                            catch (System.Exception ex)
                            {
                                throw ex;
                            }
                            ZipStream.Close();
                            ZipStream.Dispose();
                        }
                        ZipFile.Close();
                        ZipFile.Dispose();
                    }
                    StreamToZip.Close();
                    StreamToZip.Dispose();
                }
            }

        }
        /// <summary>
        /// 压缩某个目录下的所有文件(如果文件夹不存在将会抛出异常)
        /// </summary>
        /// <param name="dir"></param>
        public static  void ZipFile(string dir,string ZipedPath)
        {
            if (Directory.Exists(dir))
            {
                string[] Files = Directory.GetFiles(dir);
                foreach (string file in Files)
                {
                    try
                    {
                        ZipFile(file, ZipedPath, 7, 16384);
                    }
                    catch
                    {
                        GlobalRuntime.AddRunLog(file+"在压缩的过程中出错!");
                        continue;
                    }
                }
            }
            else
                throw new Exception("需要进行压缩的文件夹不存在!");
        }
        /// <summary>
        /// 解压指定路径下的所有压缩文件到(\\temp)下
        /// </summary>
        /// <param name="dir"></param>
        public static void UnZip(string dir)
        {
            lock (uncompressLock)
            {
                if (Directory.Exists(dir))
                {
                    string[] Files = Directory.GetFiles(dir);
                    foreach (string file in Files)
                    {
                        try
                        {
                            UnZip(file, (new FileInfo(file)).Directory + "\\temp");
                        }
                        catch
                        {
                            GlobalRuntime.AddRunLog(file + "在解压过程中出错!");
                            continue;
                        }
                    }
                }
                else
                    throw new Exception("需要进行解压的文件夹不存在!");
            }
        }
    /// <summary>
    /// 解压文件
    /// </summary>
    /// <param name="ZipFile">要解压的文件</param>
    /// <param name="unZipFilePath">解压后存放的路径</param>
        public static void UnZip(string zipFile, string unZipFilePath)
        {
            lock (uncompressLock)
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {

                        string directoryName = Path.GetDirectoryName(unZipFilePath);
                        string fileName = Path.GetFileName(theEntry.Name);

                        //生成解压目录
                        Directory.CreateDirectory(directoryName);
                        if (fileName != String.Empty)
                        {
                            //解压文件到指定的目录
                            using (FileStream streamWriter = File.Create(unZipFilePath + theEntry.Name))
                            {
                                int size = 16384;
                                byte[] data = new byte[16384];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                streamWriter.Close();
                                streamWriter.Dispose();
                            }
                        }
                    }
                    s.Close();
                    s.Dispose();
                }
            }
        }
    }
}

  

posted @ 2014-10-20 19:52  打倒挨踢者  阅读(165)  评论(0)    收藏  举报