压缩/解压缩

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.IO;
  5
  6using ICSharpCode.SharpZipLib.Zip;
  7
  8namespace Zipper
  9{
 10    public class Zipper
 11    {
 12        private string m_FolderPath = null;
 13
 14        /// <summary>
 15        /// 压缩文件夹
 16        /// </summary>
 17        /// <param name="FolderPath"></param>
 18        /// <param name="outstream"></param>

 19        protected void ZipFolderUtility(string FolderPath, ZipOutputStream outstream)
 20        {
 21            DirectoryInfo rootfolder = new DirectoryInfo(FolderPath);
 22            //压缩当前文件夹下的文件
 23            foreach (FileInfo f in rootfolder.GetFiles())
 24                ZipFileUtility(f.FullName, outstream);
 25            //压缩子文件夹
 26            foreach (DirectoryInfo childfolder in rootfolder.GetDirectories())
 27                ZipFolderUtility(childfolder.FullName, outstream);
 28
 29        }

 30
 31        /// <summary>
 32        /// 压缩文件
 33        /// </summary>
 34        /// <param name="InFilePath"></param>
 35        /// <param name="outstream"></param>

 36        protected void ZipFileUtility(string InFilePath, ZipOutputStream outstream)
 37        {
 38            FileInfo fi = new FileInfo(InFilePath);
 39            
 40            FileStream fs = fi.OpenRead();
 41            //
 42            ZipEntry entry = null;
 43            
 44            if (fi.DirectoryName == m_FolderPath)
 45                entry = new ZipEntry(fi.Name);
 46            else
 47                entry = new ZipEntry(fi.DirectoryName.Replace(m_FolderPath+"\\",string.Empty) + "\\" + fi.Name);
 48
 49            entry.Size = fs.Length;
 50            entry.DateTime = DateTime.Now;
 51            //
 52            outstream.PutNextEntry(entry);
 53            //
 54            int index = 0;
 55            int count = 1048576;
 56
 57            byte[] buffer = new byte[count];
 58            //
 59            while ((count = fs.Read(buffer, 0, count)) != 0)
 60            {
 61                outstream.Write(buffer, 0, count);
 62                index += count;
 63
 64                if (count != 1048576)
 65                {
 66                    break;
 67                }

 68            }

 69
 70            fs.Close();
 71        }

 72
 73        /// <summary>
 74        /// 压缩文件夹
 75        /// </summary>
 76        /// <param name="FolderPath">文件夹路径</param>
 77        /// <param name="ZipOutputFileName">压缩文件路径</param>

 78        public void ZipFolder(string FolderPath, string ZipOutputFileName)
 79        {
 80            try
 81            {
 82                if (!Directory.Exists(FolderPath))
 83                    throw new System.IO.DirectoryNotFoundException("Folder:" + FolderPath + " Not Fount.");
 84
 85                if (string.IsNullOrEmpty(ZipOutputFileName))
 86                    ZipOutputFileName = FolderPath + ".zip";
 87
 88                Stream stm = new FileStream(ZipOutputFileName, FileMode.Create);
 89
 90                try
 91                {
 92                    m_FolderPath = FolderPath;
 93
 94                    ZipOutputStream zipout = new ZipOutputStream(stm);
 95
 96                    ZipFolderUtility(FolderPath, zipout);
 97
 98                    zipout.CloseEntry();
 99                    zipout.Close();
100                    zipout.Finish();
101                }
 
102                finally
103                {
104                    stm.Close();
105                }

106            }

107            catch (Exception ex)
108            {
109                System.Diagnostics.Trace.WriteLine(ex.Message);
110            }

111        }

112
113        /// <summary>
114        /// 压缩文件
115        /// </summary>
116        /// <param name="FilePath">文件路径</param>
117        /// <param name="ZipOutputFilePath">压缩文件路径</param>

118        public void ZipFile(string FilePath, string ZipOutputFilePath)
119        {
120            try
121            {
122                if (!File.Exists(FilePath))
123                    throw new System.IO.FileNotFoundException("File:" + FilePath + " Not Found.");
124
125                if (string.IsNullOrEmpty(ZipOutputFilePath))
126                    ZipOutputFilePath = ZipOutputFilePath.Replace(
127                        System.IO.Path.GetExtension(ZipOutputFilePath), ".zip");
128                
129                Stream stm = new FileStream(ZipOutputFilePath, FileMode.Create);
130
131                try
132                {
133                    ZipOutputStream zipout = new ZipOutputStream(stm);
134
135                    ZipFileUtility(FilePath, zipout);
136
137                    zipout.CloseEntry();
138                    zipout.Close();
139                    zipout.Finish();
140                }

141                finally
142                {
143                    stm.Close();
144                }
 
145            }

146            catch (Exception ex)
147            {
148                System.Diagnostics.Trace.WriteLine(ex.Message);
149            }

150        }

151
152        /// <summary>
153        /// 解压缩
154        /// </summary>
155        /// <param name="FilePath"></param>

156        public void UnZip(string FilePath,string OutputFolder)
157        {
158            if (!File.Exists(FilePath))
159                throw new FileNotFoundException("File:" + FilePath + " Not Found.");
160
161            FileInfo fi = new FileInfo(FilePath);
162            using (ZipInputStream stream = new ZipInputStream(fi.OpenRead()))
163            {
164                string foldername = null;
165
166                if (string.IsNullOrEmpty(OutputFolder))
167                    foldername = fi.FullName.Replace(fi.Extension, string.Empty);
168                else
169                    foldername = OutputFolder + fi.Name;
170
171                //首先为该文件创建一个解压缩到的目录
172                Directory.CreateDirectory(foldername);
173
174                ZipEntry ze = null;
175                while ((ze = stream.GetNextEntry()) != null)
176                {
177                    int size = 2048;
178                    byte[] data = new byte[2048];
179
180
181                    string[] s = ze.Name.Split('\\');
182                    if (s.Length > 1)
183                    {
184                        StringBuilder sb = new StringBuilder(foldername);
185
186                        int i = 0;
187                        while (i < s.Length - 1)
188                        {
189                            sb.Append('\\');
190                            sb.Append(s[i++]);
191                        }

192                        
193                        Directory.CreateDirectory(sb.ToString());
194                    }

195
196                    string outfile = foldername +"\\" + ze.Name;
197
198                    using (FileStream fs = new FileStream(outfile, FileMode.Create))
199                    {
200
201                        while (true)
202                        {
203                            size = stream.Read(data, 0, data.Length);
204
205                            if (size > 0)
206                            {
207                                fs.Write(data, 0, size);
208                            }

209                            else
210                            {
211                                break;
212                            }

213                        }

214
215                        fs.Flush();
216                        fs.Close();
217                    }

218                }

219            }

220        }

221
222    }

223}

224
225
原文地址:https://www.cnblogs.com/sskset/p/563473.html