zoukankan      html  css  js  c++  java
  • 压缩及解压缩文件

    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();
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    用友U8 | 【出纳管理】添加日记账时,为什么日期选不了之前的日期?
    用友U8 | 【总账】结账时提示:该凭证已被别的用户锁定,请稍候在试...
    用友U8 | 【实施导航】实施导航进度条一直显示没完成
    利用Action方法委托重构switch接口
    关于wcf序列化后的压缩示例
    sql常用的命令
    WebBrowser通过cookie自动登录网站
    SqlServer大数据的分区方案
    WebBrowser 登录windows集成验证的网站
    SQL大批量插入数据的方式(多表关联) .
  • 原文地址:https://www.cnblogs.com/gcr1314/p/4038463.html
Copyright © 2011-2022 走看看