先从网上下载ICSharpCode.SharpZipLib.dll类库
将文件或文件夹压缩为zip,函数如下
1 /// <summary> 2 /// 压缩文件 3 /// </summary> 4 /// <param name="fileName">压缩文件路径</param> 5 /// <param name="zipName">压缩的文件名称</param> 6 /// <param name="error">返回的错误信息</param> 7 /// <returns></returns> 8 public bool FileToZip(string fileName, string zipName, out string error) 9 { 10 error = string.Empty; 11 try 12 { 13 ZipOutputStream s = new ZipOutputStream(File.Create(zipName)); 14 s.SetLevel(6); // 0 - store only to 9 - means best compression 15 zip(fileName, s); 16 s.Finish(); 17 s.Close(); 18 return true; 19 } 20 catch (Exception ex) 21 { 22 error = ex.Message; 23 return false; 24 } 25 } 26 27 28 private void zip(string fileName, ZipOutputStream s) 29 { 30 if (fileName[fileName.Length - 1] != Path.DirectorySeparatorChar) 31 fileName += Path.DirectorySeparatorChar; 32 Crc32 crc = new Crc32(); 33 string[] filenames = Directory.GetFileSystemEntries(fileName); 34 foreach (string file in filenames) 35 { 36 if (Directory.Exists(file)) 37 { 38 zip(file, s); 39 } 40 else // 否则直接压缩文件 41 { 42 //打开压缩文件 43 FileStream fs = File.OpenRead(file); 44 45 byte[] buffer = new byte[fs.Length]; 46 fs.Read(buffer, 0, buffer.Length); 47 string tempfile = Path.GetFileName(file); 48 ZipEntry entry = new ZipEntry(tempfile); 49 50 entry.DateTime = DateTime.Now; 51 entry.Size = fs.Length; 52 fs.Close(); 53 crc.Reset(); 54 crc.Update(buffer); 55 entry.Crc = crc.Value; 56 s.PutNextEntry(entry); 57 58 s.Write(buffer, 0, buffer.Length); 59 } 60 } 61 }
将zip解压为文件或文件夹,函数代码如下
1 /// <summary> 2 /// 解压文件 3 /// </summary> 4 /// <param name="zipName">解压文件路径</param> 5 /// <param name="fileDirName">解压到文件夹的名称</param> 6 /// <param name="error">返回的错误信息</param> 7 /// <returns></returns> 8 public bool ZipToFile(string zipName, string fileDirName, out string error) 9 { 10 try 11 { 12 error = string.Empty; 13 //读取压缩文件(zip文件),准备解压缩 14 ZipInputStream s = new ZipInputStream(File.Open(zipName.Trim(), FileMode.Open, FileAccess.Read)); 15 ZipEntry theEntry; 16 17 string rootDir = " "; 18 while ((theEntry = s.GetNextEntry()) != null) 19 { 20 string path = fileDirName; 21 //获取该文件在zip中目录 22 rootDir = Path.GetDirectoryName(theEntry.Name); 23 //获取文件名称 24 string fileName = Path.GetFileName(theEntry.Name); 25 if (string.IsNullOrEmpty(fileName)) 26 continue; 27 //判断是否为顶层文件,是,将文件直接放在fileDirName下,否,创建目录 28 if (string.IsNullOrEmpty(rootDir)) 29 { 30 if (!Directory.Exists(path)) 31 Directory.CreateDirectory(path); 32 } 33 else 34 { 35 path += "\\" + rootDir; 36 if (!Directory.Exists(path)) 37 Directory.CreateDirectory(path); 38 } 39 40 //将文件流写入对应目录下的文件中 41 if (fileName != String.Empty) 42 { 43 FileStream streamWriter = File.Create(path + "\\" + fileName); 44 45 int size = 2048; 46 byte[] data = new byte[2048]; 47 while (true) 48 { 49 if (theEntry.Size == 0) 50 break; 51 52 size = s.Read(data, 0, data.Length); 53 if (size > 0) 54 { 55 streamWriter.Write(data, 0, size); 56 } 57 else 58 { 59 break; 60 } 61 } 62 streamWriter.Close(); 63 } 64 } 65 s.Close(); 66 return true; 67 } 68 catch (Exception ex) 69 { 70 error = ex.Message; 71 return false; 72 } 73 }
调用示例
1 string error; 2 if (FileToZip(@"E:\文档", "文档.zip", out error)) 3 MessageBox.Show("Succee"); 4 else 5 MessageBox.Show(error);
压缩示例
1 string error; 2 if (ZipToFile(@"E:\文档.zip", "文档", out error)) 3 MessageBox.Show("Succee"); 4 else 5 MessageBox.Show(error);
解压示例
时间: 2024-11-08 14:20:24