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.Password = "paw";//如果需要密码的话可以加上这句 zip.ExtractAll(newDir, ExtractExistingFileAction.OverwriteSilently); } File.Delete(path); } catch(Exception ex) { System.Console.Error.WriteLine("exception: " + ex); } } }
博客展示了使用DoNetZip类库进行文件解压和压缩的代码。定义了ZipHelper类,其中ZipSingleFile方法用于压缩文件,可添加多个文件并保存;UnZipSingleFile方法用于解压文件,可指定解压目录,还能设置密码,解压后可删除原文件。
7291

被折叠的 条评论
为什么被折叠?



