Created
August 24, 2013 14:22
-
-
Save yemrekeskin/6328427 to your computer and use it in GitHub Desktop.
ZipHelper with Ionic.Zip ( http://www.nuget.org/packages/DotNetZip )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class ZipHelper | |
| { | |
| public readonly static string ZipPassword = ConfigurationManager.AppSettings["ZipPassword"].ToString(); | |
| public static bool Zip(string filePath,string TargetDirectory) | |
| { | |
| if (File.Exists(filePath)) | |
| throw new ApplicationException(""); | |
| if (Directory.Exists(TargetDirectory)) | |
| throw new ApplicationException(""); | |
| bool rv = false; | |
| try | |
| { | |
| using (ZipFile zip = new ZipFile()) | |
| { | |
| zip.Password = ZipPassword; | |
| zip.Comment = ""; | |
| zip.AddFile(filePath, "Files"); | |
| zip.Save(TargetDirectory); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| rv = false; | |
| } | |
| return rv; | |
| } | |
| public static bool Zip(string[] fileNames,string TargetDirectory) | |
| { | |
| bool rv = false; | |
| try | |
| { | |
| for (int i = 0; i < fileNames.Count(); i++) | |
| { | |
| Zip(fileNames[i], TargetDirectory); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| rv = false; | |
| } | |
| return rv; | |
| } | |
| public static bool UnZip(string TargetDirectory,string filePath) | |
| { | |
| bool rv = false; | |
| try | |
| { | |
| using (ZipFile zip=ZipFile.Read(filePath)) | |
| { | |
| foreach (ZipEntry e in zip) | |
| { | |
| e.ExtractWithPassword(TargetDirectory, ZipPassword); | |
| } | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| rv = false; | |
| } | |
| return rv; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment