2 // ZipFileExtensions.cs
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
29 namespace System.IO.Compression
31 public static class ZipFileExtensions
33 public static ZipArchiveEntry CreateEntryFromFile (
34 this ZipArchive destination, string sourceFileName,
37 return CreateEntryFromFile (destination, sourceFileName, entryName,
38 CompressionLevel.Fastest);
41 public static ZipArchiveEntry CreateEntryFromFile (
42 this ZipArchive destination, string sourceFileName,
43 string entryName, CompressionLevel compressionLevel)
45 if (destination == null)
46 throw new ArgumentNullException ("destination");
48 if (sourceFileName == null)
49 throw new ArgumentNullException ("sourceFileName");
51 if (entryName == null)
52 throw new ArgumentNullException ("entryName");
54 ZipArchiveEntry entry;
55 using (Stream stream = File.Open (sourceFileName, FileMode.Open,
56 FileAccess.Read, FileShare.Read))
58 var zipArchiveEntry = destination.CreateEntry (entryName, compressionLevel);
60 using (Stream entryStream = zipArchiveEntry.Open ())
61 stream.CopyTo (entryStream);
63 entry = zipArchiveEntry;
69 public static void ExtractToDirectory (
70 this ZipArchive source,
71 string destinationDirectoryName)
74 throw new ArgumentNullException ("source");
76 if (destinationDirectoryName == null)
77 throw new ArgumentNullException ("destinationDirectoryName");
79 var destDirInfo = Directory.CreateDirectory (destinationDirectoryName);
80 string fullName = destDirInfo.FullName;
82 foreach (var zipEntry in source.Entries)
84 var fullPath = Path.GetFullPath (Path.Combine (fullName, zipEntry.FullName));
86 var isFileName = Path.GetFileName (fullPath).Length != 0;
87 var dirPath = isFileName ? Path.GetDirectoryName (fullPath) : fullPath;
88 Directory.CreateDirectory (dirPath);
91 zipEntry.ExtractToFile (fullPath, false);
95 public static void ExtractToFile (
96 this ZipArchiveEntry source,
97 string destinationFileName)
99 ExtractToFile (source, destinationFileName, overwrite: false);
102 public static void ExtractToFile (
103 this ZipArchiveEntry source, string destinationFileName,
107 throw new ArgumentNullException ("source");
109 if (destinationFileName == null)
110 throw new ArgumentNullException ("destinationFileName");
112 var mode = overwrite ? FileMode.Create : FileMode.CreateNew;
113 using (var stream = File.Open (destinationFileName, mode, FileAccess.Write))
115 using (var stream2 = source.Open ())
116 stream2.CopyTo(stream);
119 File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime);