Don't ignore drives with type "aufs" or "overlay" (Xamarin-31021)
[mono.git] / mcs / class / System.IO.Compression / ZipArchiveEntry.cs
1 //
2 // ZipArchiveEntry.cs
3 //
4 // Author:
5 //       Joao 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
28 using SharpCompress.Archive;
29
30 namespace System.IO.Compression
31 {
32         public class ZipArchiveEntry
33         {
34                 readonly SharpCompress.Archive.Zip.ZipArchiveEntry entry;
35                 private Stream openStream;
36                 private bool wasDeleted;
37
38                 internal ZipArchiveEntry(ZipArchive     archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
39                 {
40                         if (archive == null)
41                                 throw new ArgumentNullException("archive");
42
43                         if (entry == null)
44                                 throw new ArgumentNullException("entry");
45
46                         Archive = archive;
47                         this.entry = entry;
48                 }
49
50                 public ZipArchive Archive {
51                         get;
52                         private set;
53                 }
54
55                 public long CompressedLength {
56                         get {
57                                 if (Archive.Mode == ZipArchiveMode.Create)
58                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
59
60                                 return entry.CompressedSize;
61                         }
62                 }
63
64                 public string FullName {
65                         get { return entry.Key; }
66                 }
67
68                 public DateTimeOffset LastWriteTime {
69                         get { return entry.LastModifiedTime.GetValueOrDefault(); }
70                         set { entry.LastModifiedTime = value.DateTime; }
71                 }
72
73                 public long Length {
74                         get {
75                                 if (Archive.Mode == ZipArchiveMode.Create)
76                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
77
78                                 return entry.Size;
79                         }
80                 }
81
82                 public string Name {
83                         get { return Path.GetFileName(entry.Key); }
84                 }
85
86                 public void Delete()
87                 {
88                         if (Archive.disposed)
89                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
90
91                         if (Archive.Mode !=     ZipArchiveMode.Update)
92                                 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
93
94                         if (openStream != null)
95                                 throw new IOException("The entry is already open for reading or writing.");
96
97                         wasDeleted = true;
98                         Archive.zipFile.RemoveEntry(entry);
99                 }
100
101                 public Stream Open()
102                 {
103                         if (Archive.disposed)
104                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
105
106                         if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
107                                 throw new IOException("The entry is already currently open for writing.");
108
109                         if (wasDeleted)
110                                 throw new IOException("The entry has been deleted from the archive.");
111
112                         if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
113                                 throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
114
115                         openStream = entry.OpenEntryStream();
116
117                         return openStream;
118                 }
119         }
120 }