C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件相关的知识,希望对你有一定的参考价值。
转自:http://www.cnblogs.com/xuanye/archive/2011/10/19/2217211.html
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, BZip2 和Tar格式,其实没啥好说的直接上代码
1 /// <summary> 2 /// Zip压缩与解压缩 3 /// </summary> 4 public class ZipHelper 5 { 6 /// <summary> 7 /// 压缩单个文件 8 /// </summary> 9 /// <param name="fileToZip">要压缩的文件</param> 10 /// <param name="zipedFile">压缩后的文件</param> 11 /// <param name="compressionLevel">压缩等级</param> 12 /// <param name="blockSize">每次写入大小</param> 13 public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) 14 { 15 //如果文件没有找到,则报错 16 if (!System.IO.File.Exists(fileToZip)) 17 { 18 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); 19 } 20 21 using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) 22 { 23 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) 24 { 25 using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 26 { 27 string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\\\") + 1); 28 29 ZipEntry ZipEntry = new ZipEntry(fileName); 30 31 ZipStream.PutNextEntry(ZipEntry); 32 33 ZipStream.SetLevel(compressionLevel); 34 35 byte[] buffer = new byte[blockSize]; 36 37 int sizeRead = 0; 38 39 try 40 { 41 do 42 { 43 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); 44 ZipStream.Write(buffer, 0, sizeRead); 45 } 46 while (sizeRead > 0); 47 } 48 catch (System.Exception ex) 49 { 50 throw ex; 51 } 52 53 StreamToZip.Close(); 54 } 55 56 ZipStream.Finish(); 57 ZipStream.Close(); 58 } 59 60 ZipFile.Close(); 61 } 62 } 63 64 /// <summary> 65 /// 压缩单个文件 66 /// </summary> 67 /// <param name="fileToZip">要进行压缩的文件名</param> 68 /// <param name="zipedFile">压缩后生成的压缩文件名</param> 69 public static void ZipFile(string fileToZip, string zipedFile) 70 { 71 //如果文件没有找到,则报错 72 if (!File.Exists(fileToZip)) 73 { 74 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); 75 } 76 77 using (FileStream fs = File.OpenRead(fileToZip)) 78 { 79 byte[] buffer = new byte[fs.Length]; 80 fs.Read(buffer, 0, buffer.Length); 81 fs.Close(); 82 83 using (FileStream ZipFile = File.Create(zipedFile)) 84 { 85 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) 86 { 87 string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\\\") + 1); 88 ZipEntry ZipEntry = new ZipEntry(fileName); 89 ZipStream.PutNextEntry(ZipEntry); 90 ZipStream.SetLevel(5); 91 92 ZipStream.Write(buffer, 0, buffer.Length); 93 ZipStream.Finish(); 94 ZipStream.Close(); 95 } 96 } 97 } 98 } 99 100 /// <summary> 101 /// 压缩多层目录 102 /// </summary> 103 /// <param name="strDirectory">The directory.</param> 104 /// <param name="zipedFile">The ziped file.</param> 105 public static void ZipFileDirectory(string strDirectory, string zipedFile) 106 { 107 using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) 108 { 109 using (ZipOutputStream s = new ZipOutputStream(ZipFile)) 110 { 111 ZipSetp(strDirectory, s, ""); 112 } 113 } 114 } 115 116 /// <summary> 117 /// 递归遍历目录 118 /// </summary> 119 /// <param name="strDirectory">The directory.</param> 120 /// <param name="s">The ZipOutputStream Object.</param> 121 /// <param name="parentPath">The parent path.</param> 122 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) 123 { 124 if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) 125 { 126 strDirectory += Path.DirectorySeparatorChar; 127 } 128 Crc32 crc = new Crc32(); 129 130 string[] filenames = Directory.GetFileSystemEntries(strDirectory); 131 132 foreach (string file in filenames)// 遍历所有的文件和目录 133 { 134 135 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 136 { 137 string pPath = parentPath; 138 pPath += file.Substring(file.LastIndexOf("\\\\") + 1); 139 pPath += "\\\\"; 140 ZipSetp(file, s, pPath); 141 } 142 143 else // 否则直接压缩文件 144 { 145 //打开压缩文件 146 using (FileStream fs = File.OpenRead(file)) 147 { 148 149 byte[] buffer = new byte[fs.Length]; 150 fs.Read(buffer, 0, buffer.Length); 151 152 string fileName = parentPath + file.Substring(file.LastIndexOf("\\\\") + 1); 153 ZipEntry entry = new ZipEntry(fileName); 154 155 entry.DateTime = DateTime.Now; 156 entry.Size = fs.Length; 157 158 fs.Close(); 159 160 crc.Reset(); 161 crc.Update(buffer); 162 163 entry.Crc = crc.Value; 164 s.PutNextEntry(entry); 165 166 s.Write(buffer, 0, buffer.Length); 167 } 168 } 169 } 170 } 171 172 /// <summary> 173 /// 解压缩一个 zip 文件。 174 /// </summary> 175 /// <param name="zipedFile">The ziped file.</param> 176 /// <param name="strDirectory">The STR directory.</param> 177 /// <param name="password">zip 文件的密码。</param> 178 /// <param name="overWrite">是否覆盖已存在的文件。</param> 179 public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite) 180 { 181 182 if (strDirectory == "") 183 strDirectory = Directory.GetCurrentDirectory(); 184 if (!strDirectory.EndsWith("\\\\")) 185 strDirectory = strDirectory + "\\\\"; 186 187 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile))) 188 { 189 s.Password = password; 190 ZipEntry theEntry; 191 192 while ((theEntry = s.GetNextEntry()) != null) 193 { 194 string directoryName = ""; 195 string pathToZip = ""; 196 pathToZip = theEntry.Name; 197 198 if (pathToZip != "") 199 directoryName = Path.GetDirectoryName(pathToZip) + "\\\\"; 200 201 string fileName = Path.GetFileName(pathToZip); 202 203 Directory.CreateDirectory(strDirectory + directoryName); 204 205 if (fileName != "") 206 { 207 if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName))) 208 { 209 using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName)) 210 { 211 int size = 2048; 212 byte[] data = new byte[2048]; 213 while (true) 214 { 215 size = s.Read(data, 0, data.Length); 216 217 if (size > 0) 218 streamWriter.Write(data, 0, size); 219 else 220 break; 221 } 222 streamWriter.Close(); 223 } 224 } 225 } 226 } 227 228 s.Close(); 229 } 230 } 231 232 }
以上是关于C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 C# GUI 调用 C# 控制台应用程序 [重复]