• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#用DotNetZip来解压/压缩文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//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;
            }
        }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap