在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//https://archive.codeplex.com/?p=dotnetzip //最新在Nuget 下载DotNetZip using Ionic.Zip; private void button1_Click(object sender, EventArgs e) { string msg = ""; ExtractFile("D:\\1.zip", "d:\\OK\\"); ExtractFile("D:\\1.zip", "d:\\OK\\", false, out msg); } public static bool ZipFolder( String sourceFilePath, String targetFileFullPath, Boolean isUsePassword, Int32 maxOutputSegmentSiez, out String errMessage) { try { using (ZipFile zip = new ZipFile(Encoding.Default)) { errMessage = String.Empty; zip.Comment = "压缩文件时间" + System.DateTime.Now.ToString("G"); zip.Name = Guid.NewGuid().ToString().ToUpper() + ".zip"; if (isUsePassword) zip.Password = "123"; zip.MaxOutputSegmentSize = maxOutputSegmentSiez * 1000; zip.BufferSize = 1024; zip.CaseSensitiveRetrieval = true; zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; zip.AddDirectory(sourceFilePath); zip.Save(targetFileFullPath); return true; } } catch (Exception ex) { errMessage = ex.Message; return false; } } public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath, Boolean isUsePassword, out String errMessage) { try { Encoding encoding = Encoding.UTF8; errMessage = String.Empty; var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding }; using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options)) { if (isUsePassword) { //假设有密码123 zip.Password = "123"; } zip.AlternateEncoding = encoding; zip.ExtractAll(targetFolderPath, ExtractExistingFileAction.OverwriteSilently);//一次批量解压 return true; } } catch (Exception ex) { errMessage = ex.Message; return false; } } public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath) { try { Encoding encoding = Encoding.UTF8; var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding }; using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options)) { zip.AlternateEncoding = encoding; foreach (var f in zip.Entries) {//一个个解压 f.Extract(targetFolderPath, ExtractExistingFileAction.OverwriteSilently); } return true; } } catch (Exception ex) { return false; } }
|
请发表评论