Merge pull request #3059 from lateralusX/jlorenss/win-x64-dyncall-gsharedvt-support
[mono.git] / mcs / class / System.IO.Compression.FileSystem / ZipFileExtensions.cs
1 //
2 // ZipFileExtensions.cs
3 //
4 // Author:
5 //       João Matos <joao.matos@xamarin.com>
6 //       Martin Baulig <martin.baulig@xamarin.com>
7 //
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
9 //
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:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
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
26 // THE SOFTWARE.
27 using System;
28
29 namespace System.IO.Compression
30 {
31         public static class ZipFileExtensions
32         {
33                 public static ZipArchiveEntry CreateEntryFromFile (
34                         this ZipArchive destination, string sourceFileName,
35                         string entryName)
36                 {
37                         return CreateEntryFromFile (destination, sourceFileName, entryName,
38                                 CompressionLevel.Fastest);
39                 }
40
41                 public static ZipArchiveEntry CreateEntryFromFile (
42                         this ZipArchive destination, string sourceFileName,
43                         string entryName, CompressionLevel compressionLevel)
44                 {
45                         if (destination == null)
46                                 throw new ArgumentNullException ("destination");
47
48                         if (sourceFileName == null)
49                                 throw new ArgumentNullException ("sourceFileName");
50
51                         if (entryName == null)
52                                 throw new ArgumentNullException ("entryName");
53
54                         ZipArchiveEntry entry;
55                         using (Stream stream = File.Open (sourceFileName, FileMode.Open,
56                                 FileAccess.Read, FileShare.Read))
57                         {
58                                 var zipArchiveEntry = destination.CreateEntry (entryName, compressionLevel);
59                                 zipArchiveEntry.LastWriteTime = File.GetLastWriteTimeUtc(sourceFileName);
60
61                                 using (Stream entryStream = zipArchiveEntry.Open ())
62                                         stream.CopyTo (entryStream);
63
64                                 entry = zipArchiveEntry;
65                         }
66
67                         return entry;
68                 }
69
70                 public static void ExtractToDirectory (
71                         this ZipArchive source,
72                         string destinationDirectoryName)
73                 {
74                         if (source == null)
75                                 throw new ArgumentNullException ("source");
76
77                         if (destinationDirectoryName == null)
78                                 throw new ArgumentNullException ("destinationDirectoryName");
79
80                         var destDirInfo = Directory.CreateDirectory (destinationDirectoryName);
81                         string fullName = destDirInfo.FullName;
82
83                         foreach (var zipEntry in source.Entries)
84                         {
85                                 var fullPath = Path.GetFullPath (Path.Combine (fullName, zipEntry.FullName));
86
87                                 var isFileName = Path.GetFileName (fullPath).Length != 0;
88                                 var dirPath = isFileName ? Path.GetDirectoryName (fullPath) : fullPath;
89                                 Directory.CreateDirectory (dirPath);
90
91                                 if (isFileName)
92                                         zipEntry.ExtractToFile (fullPath, false);
93                         }
94                 }
95
96                 public static void ExtractToFile (
97                         this ZipArchiveEntry source,
98                         string destinationFileName)
99                 {
100                         ExtractToFile (source, destinationFileName, overwrite: false);
101                 }
102
103                 public static void ExtractToFile (
104                         this ZipArchiveEntry source, string destinationFileName,
105                         bool overwrite)
106                 {
107                         if (source == null)
108                                 throw new ArgumentNullException ("source");
109
110                         if (destinationFileName == null)
111                                 throw new ArgumentNullException ("destinationFileName");
112                                 
113                         var mode = overwrite ? FileMode.Create : FileMode.CreateNew;
114                         using (var stream = File.Open (destinationFileName, mode, FileAccess.Write))
115                         {
116                                 using (var stream2 = source.Open ())
117                                         stream2.CopyTo(stream);
118                         }
119
120                         File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime);
121                 }
122         }
123 }
124