需要下载引用ICSharpCode.SharpZipLib.dll这个文件
using ICSharpCode.SharpZipLib.Zip; using System.IO; ///////////////////////////////////调用//////////////////////////////////// string filePath = GetRootPath() + "UploadFiles\test\aaa.zip"; string destFilePath = GetRootPath() + "UploadFiles\test\"; UnZip(filePath, destFilePath, ""); ////////////////////////////////////////////////////////////////////////////// #region 解压 /// <summary> /// 解压 /// </summary> /// <param name="fileToUpZip">压缩文件路径</param> /// <param name="zipedFolder">解压文件存放路径,为空时默认创建文件夹</param> /// <param name="password">密码</param> public void UnZip(string fileToUpZip, string zipedFolder, string password) { if (!File.Exists(fileToUpZip)) { return; } if (!Directory.Exists(zipedFolder)) { Directory.CreateDirectory(zipedFolder); } ZipInputStream s = null; ZipEntry theEntry; string fileName; FileStream streamWriter = null; try { s = new ZipInputStream(File.OpenRead(fileToUpZip)); s.Password = password; while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { fileName = Path.Combine(zipedFolder, theEntry.Name); //判断文件路径是否是文件夹 if (fileName.EndsWith("/") || fileName.EndsWith("\")) { Directory.CreateDirectory(fileName); continue; } streamWriter = File.Create(fileName); int size; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } finally { if (streamWriter != null) { streamWriter.Close(); } if (s != null) { s.Close(); } GC.Collect(); GC.Collect(1); } } #endregion