5 // João Matos <joao.matos@xamarin.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 namespace System.IO.Compression
32 public static class ZipFile
34 public static void CreateFromDirectory (
35 string sourceDirectoryName, string destinationArchiveFileName)
37 CreateFromDirectory (sourceDirectoryName, destinationArchiveFileName,
38 CompressionLevel.Fastest, includeBaseDirectory: false);
41 public static void CreateFromDirectory (
42 string sourceDirectoryName, string destinationArchiveFileName,
43 CompressionLevel compressionLevel, bool includeBaseDirectory)
45 CreateFromDirectory (sourceDirectoryName, destinationArchiveFileName,
46 compressionLevel, includeBaseDirectory, Encoding.UTF8);
49 public static void CreateFromDirectory (
50 string sourceDirectoryName,
51 string destinationArchiveFileName,
52 CompressionLevel compressionLevel,
53 bool includeBaseDirectory,
54 Encoding entryNameEncoding)
56 if (sourceDirectoryName == null)
57 throw new ArgumentNullException ("sourceDirectoryName");
59 if (destinationArchiveFileName == null)
60 throw new ArgumentNullException ("destinationArchiveFileName");
62 if (string.IsNullOrWhiteSpace (sourceDirectoryName))
63 throw new ArgumentException ("sourceDirectoryName");
65 if (string.IsNullOrWhiteSpace (destinationArchiveFileName))
66 throw new ArgumentException ("destinationArchiveFileName");
68 if (entryNameEncoding == Encoding.Unicode ||
69 entryNameEncoding == Encoding.UTF32 ||
70 entryNameEncoding == Encoding.UTF7)
71 throw new ArgumentException ("entryNameEncoding");
73 if (entryNameEncoding == null)
74 entryNameEncoding = Encoding.UTF8;
76 if (!Directory.Exists (sourceDirectoryName))
77 throw new DirectoryNotFoundException ("sourceDirectoryName is invalid or does not exist");
79 var sourceDir = new DirectoryInfo (Path.GetFullPath (sourceDirectoryName));
81 string fullBaseName = sourceDir.FullName;
82 if (includeBaseDirectory && sourceDir.Parent != null)
83 fullBaseName = sourceDir.Parent.FullName;
85 bool hasEntries = false;
86 char[] separators = new char[] {
87 Path.DirectorySeparatorChar,
88 Path.AltDirectorySeparatorChar
91 using (var zipFile = ZipFile.Open (destinationArchiveFileName, ZipArchiveMode.Create,
93 var entries = sourceDir.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories);
94 foreach (var entry in entries) {
97 int length = entry.FullName.Length - fullBaseName.Length;
98 string entryName = entry.FullName.Substring(fullBaseName.Length, length);
100 entryName = entryName.TrimStart(separators);
102 if (entry is FileInfo)
103 zipFile.CreateEntryFromFile (entry.FullName, entryName, compressionLevel);
105 zipFile.CreateEntry (entryName + Path.DirectorySeparatorChar);
108 // Create the base directory even if we had no entries
109 if (includeBaseDirectory && !hasEntries)
110 zipFile.CreateEntry(sourceDir.Name + Path.DirectorySeparatorChar);
114 public static void ExtractToDirectory (
115 string sourceArchiveFileName, string destinationDirectoryName)
117 ExtractToDirectory (sourceArchiveFileName, destinationDirectoryName,
121 public static void ExtractToDirectory (
122 string sourceArchiveFileName, string destinationDirectoryName,
123 Encoding entryNameEncoding)
125 if (sourceArchiveFileName == null)
126 throw new ArgumentNullException ("sourceArchiveFileName");
128 using (ZipArchive zipArchive = ZipFile.Open(sourceArchiveFileName,
129 ZipArchiveMode.Read, entryNameEncoding))
131 zipArchive.ExtractToDirectory(destinationDirectoryName);
135 public static ZipArchive Open (
136 string archiveFileName, ZipArchiveMode mode)
138 return Open (archiveFileName, mode);
141 public static ZipArchive Open (
142 string archiveFileName, ZipArchiveMode mode,
143 Encoding entryNameEncoding)
145 if (archiveFileName == null)
146 throw new ArgumentNullException ("archiveFileName");
148 if (string.IsNullOrWhiteSpace (archiveFileName))
149 throw new ArgumentException ("archiveFileName");
154 case ZipArchiveMode.Read:
155 if (!File.Exists (archiveFileName))
156 throw new FileNotFoundException ();
157 stream = new FileStream (archiveFileName, FileMode.Open, FileAccess.Read,
160 case ZipArchiveMode.Create:
161 if (File.Exists (archiveFileName))
162 throw new IOException ("mode is set to Create but the file already exists");
163 stream = new FileStream (archiveFileName, FileMode.CreateNew, FileAccess.Write);
165 case ZipArchiveMode.Update:
166 stream = new FileStream (archiveFileName, FileMode.OpenOrCreate,
167 FileAccess.ReadWrite);
170 throw new ArgumentOutOfRangeException ();
173 return new ZipArchive (stream, mode, false, entryNameEncoding);
176 public static ZipArchive OpenRead (string archiveFileName)
178 return ZipFile.Open (archiveFileName, ZipArchiveMode.Read);