[sgen] Exclusive write on binary protocol file
[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
60                                 using (Stream entryStream = zipArchiveEntry.Open ())
61                                         stream.CopyTo (entryStream);
62
63                                 entry = zipArchiveEntry;
64                         }
65
66                         return entry;
67                 }
68
69                 public static void ExtractToDirectory (
70                         this ZipArchive source,
71                         string destinationDirectoryName)
72                 {
73                         if (source == null)
74                                 throw new ArgumentNullException ("source");
75
76                         if (destinationDirectoryName == null)
77                                 throw new ArgumentNullException ("destinationDirectoryName");
78
79                         var destDirInfo = Directory.CreateDirectory (destinationDirectoryName);
80                         string fullName = destDirInfo.FullName;
81
82                         foreach (var zipEntry in source.Entries)
83                         {
84                                 var fullPath = Path.GetFullPath (Path.Combine (fullName, zipEntry.FullName));
85
86                                 var isFileName = Path.GetFileName (fullPath).Length != 0;
87                                 var dirPath = isFileName ? Path.GetDirectoryName (fullPath) : fullPath;
88                                 Directory.CreateDirectory (dirPath);
89
90                                 if (isFileName)
91                                         zipEntry.ExtractToFile (fullPath, false);
92                         }
93                 }
94
95                 public static void ExtractToFile (
96                         this ZipArchiveEntry source,
97                         string destinationFileName)
98                 {
99                         ExtractToFile (source, destinationFileName, overwrite: false);
100                 }
101
102                 public static void ExtractToFile (
103                         this ZipArchiveEntry source, string destinationFileName,
104                         bool overwrite)
105                 {
106                         if (source == null)
107                                 throw new ArgumentNullException ("source");
108
109                         if (destinationFileName == null)
110                                 throw new ArgumentNullException ("destinationFileName");
111                                 
112                         var mode = overwrite ? FileMode.Create : FileMode.CreateNew;
113                         using (var stream = File.Open (destinationFileName, mode, FileAccess.Write))
114                         {
115                                 using (var stream2 = source.Open ())
116                                         stream2.CopyTo(stream);
117                         }
118
119                         File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime);
120                 }
121         }
122 }
123