zoukankan      html  css  js  c++  java
  • 关于SharpZipLib使用说明

    对于多目录压缩在实际编码中,发现暂时并没可以对多层目录进行压缩 ,对于这个问题解决办法,大致可分为两类:

    1.修改文件名,文件名包括目录路径和当前文件名

    2.文件夹为一级,并生成唯一性标识,然后把别名已经相关信息,写到配置文件。

    以上思路仅供参考,征集好的思路,欢迎拍砖

    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Checksums;

    /********************************************************* * 开发人员:yucl * 创建时间:07/27/2013 11:29:06 * 描述说明:调用ICSharpCode.SharpZipLib.dll进行压缩和解压文件 * * 更改历史: * * *******************************************************/ /// /// 调用ICSharpCode.SharpZipLib.dll进行压缩和解压文件 /// public class ZipHelper { public string cutStr = ""; #region 加压解压方法 /// /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// ///被压缩的文件夹路径(相对路径) ///生成压缩文件名及路径(相对路径),为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip ///创建zip文件时,给zip文件加的密码 ///zip文件描述 ///level等级,1到9 ///出错信息 ///是否压缩成功 public bool ZipFile(string dirPath, string zipFilePath, string password, string comment, int level, out string err) { err = ""; if (dirPath == string.Empty) { err = "要压缩的文件夹不能为空!"; return false; } dirPath = System.Web.HttpContext.Current.Server.MapPath("~/" + dirPath); //使用正则表达式-判断压缩文件路径 //System.Text.RegularExpressions.Regex newRegex = new System.Text. // RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\{2}w+)$?)(\(w[w ]*.*))"); //if (!newRegex.Match(Zipedfiledname).Success) //{ // File.Delete(Zipedfiledname); // return "压缩文件的路径有误!"; //} if (!Directory.Exists(dirPath)) { //new FileHelper().CreateFolder(dirPath); err = "要压缩的文件夹不存在!"; return false; } //压缩文件名为空时使用文件夹名+.zip if (string.IsNullOrEmpty(zipFilePath)) { if (dirPath.EndsWith("\")) { dirPath = dirPath.Substring(0, dirPath.Length - 1); } zipFilePath = dirPath + ".zip"; } else { string tmpZipPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf('\')); new FileHelper().CreateFolder(tmpZipPath); } try { string[] filenames = Directory.GetFiles(dirPath); using (ZipOutputStream s = new ZipOutputStream(File.Create(System.Web.HttpContext.Current.Server.MapPath("~/" + zipFilePath)))) { //判断Password if (!string.IsNullOrEmpty(password)) { s.Password = password; } if (!string.IsNullOrEmpty(comment)) { s.SetComment(comment); } s.SetLevel(level); byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } } catch (Exception ex) { err = ex.Message; return false; } return true; } /// /// 功能:解压zip格式的文件。 /// ///压缩文件路径(相对路径) ///解压文件存放路径(相对路径),为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 ///出错信息 ///解压是否成功 public bool UnZipFile(string zipFilePath, string unZipDir, out string err) { err = ""; if (zipFilePath == string.Empty) { err = "压缩文件不能为空!"; return false; } if (!File.Exists(zipFilePath)) { err = "压缩文件不存在!"; return false; } //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 if (unZipDir == string.Empty) unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); if (!unZipDir.EndsWith("\")) unZipDir += "\"; if (!Directory.Exists(unZipDir)) Directory.CreateDirectory(unZipDir); try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName.Length > 0) { Directory.CreateDirectory(unZipDir + directoryName); } if (!directoryName.EndsWith("\")) directoryName += "\"; if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(unZipDir + 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; } } } } }//while } } catch (Exception ex) { err = ex.Message; return false; } return true; }//解压结束 #endregion /// /// 压缩单个文件 /// ///要压缩的文件 ///压缩后的文件 ///压缩等级 ///每次写入大小 ///加压时的密码 ///zip描述 ///错误消息 public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize, string password, string comment, out string err) { //如果文件没有找到,则报错 if (!System.IO.File.Exists(fileToZip)) { err = "指定要压缩的文件: " + fileToZip + " 不存在!"; } try { using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) { if (!string.IsNullOrEmpty(password)) ZipStream.Password = password; if (!string.IsNullOrEmpty(comment)) ZipStream.SetComment(comment); using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\") + 1); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int sizeRead = 0; try { do { sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); } while (sizeRead > 0); } catch (System.Exception ex) { throw ex; } StreamToZip.Close(); } ZipStream.Finish(); ZipStream.Close(); } ZipFile.Close(); err = ""; } } catch (Exception ex) { err = ex.Message.ToString(); } } /// /// 压缩单个文件 /// ///要进行压缩的文件名 ///压缩后生成的压缩文件名 public void ZipFile(string fileToZip, string zipedFile) { //如果文件没有找到,则报错 if (!File.Exists(fileToZip)) { throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); } using (FileStream fs = File.OpenRead(fileToZip)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); using (FileStream ZipFile = File.Create(zipedFile)) { using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\") + 1); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(5); ZipStream.Write(buffer, 0, buffer.Length); ZipStream.Finish(); ZipStream.Close(); } } } } /// /// 压缩多层目录 /// ///The directory. ///The ziped file. public void ZipFileDirectory(string strDirectory, string zipedFile, string password) { using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { // s.Password = "1"; s.SetLevel(9); ZipSetp(strDirectory, s, ""); } } } /// /// 递归遍历目录 /// ///The directory. ///The ZipOutputStream Object. ///The parent path. private void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录 { if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf("\") + 1); pPath += "\"; ZipSetp(file, s, pPath); } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\") + 1); ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } } /// /// 解压缩一个 zip 文件。 /// ///The ziped file. ///The STR directory. ///zip 文件的密码。 ///是否覆盖已存在的文件。 public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite) { if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\")) strDirectory = strDirectory + "\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(System.Web.HttpContext.Current.Server.MapPath("~/" + zipedFile)))) { if (string.IsNullOrEmpty(password)) s.Password = password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "") { if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName))) { using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName)) { 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(); } } }

  • 相关阅读:
    线性表顺序表模板 纯本人手工创造
    娘的,自己的求逆序对模板又不好使了。。。。。。。。
    杜教筛学习总结
    2019 年百度之星·程序设计大赛
    2019中国大学生程序设计竞赛(CCPC)
    2019 年百度之星·程序设计大赛
    2019 Multi-University Training Contest 7
    2019 Multi-University Training Contest 9
    2019牛客暑期多校训练营(第十场)
    2019 Multi-University Training Contest 8
  • 原文地址:https://www.cnblogs.com/yclnet/p/3237664.html
Copyright © 2011-2022 走看看