注意先添加对 ICSharpCode.SharpZipLib.dll 的引用 using System; using System.Collections.Generic; using System.Text; using System.IO; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Checksums; namespace XmlStudy { public class Zip { #region 压缩文件 /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">需要压缩的文件名称</param> /// <param name="ZipedFile">压缩后的文件名称</param> public void ZipFile(string FileToZip, string ZipedFile) { ZipFile(FileToZip, ZipedFile, 6, 1024); } /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">需要压缩的文件名称</param> /// <param name="ZipedFile">压缩后的文件名称</param> /// <param name="CompressionLevel">压缩程度0-9</param> /// <param name="BlockSize"></param> public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize) { //如果文件没有找到,则报错 if (!System.IO.File.Exists(FileToZip)) { throw new FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd"); } FileStream StreamToZip = new FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); FileStream ZipFile = File.Create(ZipedFile); ZipOutputStream ZipStream = new ZipOutputStream(ZipFile); ZipEntry ZipEntry = new ZipEntry(Path.GetFileName(FileToZip)); 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.Finish(); ZipStream.Close(); StreamToZip.Close(); } #endregion #region 压缩文件夹 /// <summary> /// 压缩文件夹 /// </summary> /// <param name="SourceDirectory">文件夹路径</param> /// <param name="ZipedFileName">压缩文件名称</param> public void ZipDirectory(string SourceDirectory, string ZipedFileName) { if (!Directory.Exists(SourceDirectory)) {//目录不存在 throw new DirectoryNotFoundException("The specified directory " + SourceDirectory + " could not be found. Zipping aborderd"); } string zipedFilePath = Path.GetDirectoryName(ZipedFileName); if (!Directory.Exists(zipedFilePath)) { Directory.CreateDirectory(zipedFilePath); } Crc32 crc = new Crc32(); ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFileName)); s.SetLevel(6); // 0 - store only to 9 - means best compression SourceDirectory = SourceDirectory.Replace('/','//').TrimEnd('//'); int nIndex = SourceDirectory.LastIndexOf('//'); string strParentPath = SourceDirectory.Substring(0, nIndex + 1); ZipDirectory(strParentPath, SourceDirectory, crc, s); s.Finish(); s.Close(); } private void ZipDirectory(string ParentPath, string SourceDirectory, Crc32 crc, ZipOutputStream OutPutStream) { DirectoryInfo info = new DirectoryInfo(SourceDirectory); FileSystemInfo[] fileSystemInfos = info.GetFileSystemInfos(); foreach (FileSystemInfo fileSystemInfo in fileSystemInfos) { string fullname = Path.Combine(SourceDirectory, fileSystemInfo.Name); if (Directory.Exists(fullname)) {//该名称为路径 ZipDirectory(ParentPath, fullname, crc, OutPutStream); } else {//该名称为文件 ZipFile(ParentPath, fullname, crc, OutPutStream); } } } private void ZipFile(string ParentPath, string FilePath, Crc32 crc, ZipOutputStream OutPutStream) { //打开压缩文件 FileStream fs = File.OpenRead(FilePath); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(FilePath.Replace(ParentPath, "")); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; OutPutStream.PutNextEntry(entry); OutPutStream.Write(buffer, 0, buffer.Length); } #endregion #region 解压缩文件 /// <summary> /// 解压缩文件到当前目录 /// </summary> /// <param name="SourceZipFile"></param> public void UnZip(string SourceZipFile) { //string strUnZipPath = SourceZipFile.Substring(0, SourceZipFile.LastIndexOf('.')); UnZip(SourceZipFile, Path.GetDirectoryName(SourceZipFile)); } /// <summary> /// 解压缩文件 /// </summary> /// <param name="SourceZipFile">压缩文件名称</param> /// <param name="UnZipPath">解压路径</param> public void UnZip(string SourceZipFile, string UnZipPath) { ZipInputStream s = new ZipInputStream(File.OpenRead(SourceZipFile)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string filePath = Path.Combine(UnZipPath,Path.GetDirectoryName(theEntry.Name)); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(Path.Combine(UnZipPath, theEntry.Name)); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); } #endregion } }