码迷,mamicode.com
首页 > Web开发 > 详细

DoNetZip类库解压和压缩文件

时间:2018-08-30 12:55:32      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:files   static   var   with   ges   extension   sys   stat   file   

using Ionic.Zip;


public class ZipHelper
    {
        public static void ZipSingleFile(string zipPath)
        {
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
                    zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
                    zip.AddFile("ReadMe.txt");
                    zip.Save(zipPath);
                }
            }
            catch (Exception ex)
            {
                System.Console.Error.WriteLine("exception: " + ex);
            }
        }
        public static void UnZipSingleFile(string path)
        {
            try
            {
                using (ZipFile zip = new ZipFile(path))
                {
                    var baseDir = Path.GetDirectoryName(path);
                    var fileName = Path.GetFileNameWithoutExtension(path);
                    var newDir = Path.Combine(baseDir, fileName);
                    Directory.CreateDirectory(newDir);
                    zip.ExtractAll(newDir, ExtractExistingFileAction.OverwriteSilently);
                }
                File.Delete(path);
            }
            catch(Exception ex)
            {
                System.Console.Error.WriteLine("exception: " + ex);
            }
        }
    }

 

DoNetZip类库解压和压缩文件

标签:files   static   var   with   ges   extension   sys   stat   file   

原文地址:https://www.cnblogs.com/dayang12525/p/9559471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!