相关的ICSharpCode.SharpZipLib的DLL可以到这里下载:http://icsharpcode.github.io/SharpZipLib/
/// <summary> /// 文件加密压缩 /// </summary> /// <param name="FileToZip">需要压缩的文件路径</param> /// <param name="ZipedFile">压缩包路径(压缩包文件类型看自己需求)</param> /// <param name="password">加密密码</param> public void ZipFileMain(string FileToZip, string ZipedFile, string password) { ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); s.SetLevel(6); // 0 - store only to 9 - means best compression s.Password = password; //打开压缩文件 FileStream fs = File.OpenRead(FileToZip); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); Array arr = FileToZip.Split('\\'); string le = arr.GetValue(arr.Length - 1).ToString(); ZipEntry entry = new ZipEntry(le); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); s.Finish(); s.Close(); } //解压缩 public void UnZip(string directoryName, string ZipedFile, string password) { using (FileStream fileStreamIn = new FileStream(ZipedFile, FileMode.Open, FileAccess.Read)) { using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn)) { zipInStream.Password = password; ZipEntry entry = zipInStream.GetNextEntry(); //WebContext.SqlfilePath = directoryName + "\\" + entry.Name; do { using (FileStream fileStreamOut = new FileStream(directoryName + @"\" + entry.Name, FileMode.Create, FileAccess.Write)) { int size = 2048; byte[] buffer = new byte[2048]; do { size = zipInStream.Read(buffer, 0, buffer.Length); fileStreamOut.Write(buffer, 0, size); } while (size > 0); } } while ((entry = zipInStream.GetNextEntry()) != null); } } }
建议密码使用英文!
已有 4443 位网友参与,快来吐槽:
发表评论