Merge pull request #2646 from ludovic-henry/fix-processwatch-dispose
[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         internal class ZipArchiveEntryStream : Stream, IDisposable
33         {
34                 private readonly ZipArchiveEntry entry;
35                 private readonly Stream stream;
36
37                 public override bool CanRead {
38                         get { 
39                                 return stream.CanRead;
40                         }
41                 }
42
43                 public override bool CanSeek {
44                         get {
45                                 return stream.CanSeek;
46                         }
47                 }
48
49                 public override bool CanWrite {
50                         get {
51                                 return stream.CanWrite;
52                         }
53                 }
54
55                 public override long Length {
56                         get {
57                                 return stream.Length;
58                         }
59                 }
60
61                 public override long Position {
62                         get {
63                                 return stream.Position;
64                         }
65                         set {
66                                 stream.Position = value;
67                         }
68                 }
69
70                 public ZipArchiveEntryStream(ZipArchiveEntry entry, Stream stream)
71                 {
72                         this.entry = entry;
73                         this.stream = stream;
74                 }
75
76                 public override void Flush ()
77                 {
78                         stream.Flush();
79                 }
80
81                 public override long Seek (long offset, SeekOrigin origin)
82                 {
83                         return stream.Seek(offset, origin);
84                 }
85
86                 public override void SetLength (long value)
87                 {
88                         stream.SetLength(value);
89                 }
90
91                 public override int Read (byte[] buffer, int offset, int count)
92                 {
93                         return stream.Read(buffer, offset, count);
94                 }
95
96                 public override void Write (byte[] buffer, int offset, int count)
97                 {
98                         stream.Write(buffer, offset, count);
99                 }
100
101                 public new void Dispose()
102                 {
103                         Dispose(true);
104                         GC.SuppressFinalize(this);
105                         base.Dispose();
106                 }
107
108                 protected override void Dispose(bool disposing)
109                 {
110                         if (disposing) 
111                         {
112                                 entry.openStream = null;
113                                 stream.Dispose();
114                         }
115                 }
116         }
117
118         public class ZipArchiveEntry
119         {
120                 readonly SharpCompress.Archive.Zip.ZipArchiveEntry entry;
121                 internal ZipArchiveEntryStream openStream;
122                 private bool wasDeleted;
123
124                 internal ZipArchiveEntry(ZipArchive     archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
125                 {
126                         if (archive == null)
127                                 throw new ArgumentNullException("archive");
128
129                         if (entry == null)
130                                 throw new ArgumentNullException("entry");
131
132                         Archive = archive;
133                         this.entry = entry;
134                 }
135
136                 public ZipArchive Archive {
137                         get;
138                         private set;
139                 }
140
141                 public long CompressedLength {
142                         get {
143                                 if (Archive.Mode == ZipArchiveMode.Create)
144                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
145
146                                 return entry.CompressedSize;
147                         }
148                 }
149
150                 public string FullName {
151                         get { return entry.Key; }
152                 }
153
154                 public DateTimeOffset LastWriteTime {
155                         get { return entry.LastModifiedTime.GetValueOrDefault(); }
156                         set { entry.LastModifiedTime = value.DateTime; }
157                 }
158
159                 public long Length {
160                         get {
161                                 if (Archive.Mode == ZipArchiveMode.Create)
162                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
163
164                                 return entry.Size;
165                         }
166                 }
167
168                 public string Name {
169                         get { return Path.GetFileName(entry.Key); }
170                 }
171
172                 public void Delete()
173                 {
174                         if (Archive.disposed)
175                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
176
177                         if (Archive.Mode !=     ZipArchiveMode.Update)
178                                 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
179
180                         if (openStream != null)
181                                 throw new IOException("The entry is already open for reading or writing.");
182
183                         wasDeleted = true;
184                         Archive.zipFile.RemoveEntry(entry);
185                 }
186
187                 public Stream Open()
188                 {
189                         if (Archive.disposed)
190                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
191
192                         if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
193                                 throw new IOException("The entry is already currently open for writing.");
194
195                         if (wasDeleted)
196                                 throw new IOException("The entry has been deleted from the archive.");
197
198                         if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
199                                 throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
200
201                         openStream = new ZipArchiveEntryStream(this, entry.OpenEntryStream());
202
203                         return openStream;
204                 }
205         }
206 }