Use __cdecl rather than __stdcall for icalls on Windows 32-bit
[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 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 entry.Archive.Mode != ZipArchiveMode.Read;
46                         }
47                 }
48
49                 public override bool CanWrite {
50                         get {
51                                 return entry.Archive.Mode != ZipArchiveMode.Read;
52                         }
53                 }
54
55                 public override long Length {
56                         get {
57                                 return stream.CanWrite ? stream.Length : entry.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                 internal void EnsureWriteable()
102                 {
103                         if (entry.Archive.Mode == ZipArchiveMode.Update && !stream.CanWrite)
104                         {
105                                 // Replace the read-only stream with a writeable memory stream.
106                                 SetWriteable();
107                         }
108                 }
109
110                 internal void SetWriteable()
111                 {
112                         var archive = entry.Archive;
113
114                         var internalEntry = entry.entry;
115                         var newEntry = archive.CreateEntryInternal(internalEntry.Key);
116                         var newStream = newEntry.OpenEntryStream();
117
118                         var openStream = stream;
119                         openStream.CopyTo(newStream);
120                         openStream.Dispose();
121
122                         newStream.Position = 0;
123
124                         archive.zipFile.RemoveEntry(internalEntry);
125                         entry.entry = newEntry;
126                         stream = newStream;
127                 }
128
129                 public new void Dispose()
130                 {
131                         Dispose(true);
132                         GC.SuppressFinalize(this);
133                         base.Dispose();
134                 }
135
136                 protected override void Dispose(bool disposing)
137                 {
138                         if (disposing) 
139                         {
140                                 entry.openStream = null;
141                                 stream.Dispose();
142                         }
143                 }
144         }
145
146         public class ZipArchiveEntry
147         {
148                 internal SharpCompress.Archive.Zip.ZipArchiveEntry entry;
149                 internal ZipArchiveEntryStream openStream;
150         internal bool wasWritten;
151                 private bool wasDeleted;
152
153                 internal ZipArchiveEntry(ZipArchive     archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
154                 {
155                         if (archive == null)
156                                 throw new ArgumentNullException("archive");
157
158                         if (entry == null)
159                                 throw new ArgumentNullException("entry");
160
161                         Archive = archive;
162                         this.entry = entry;
163                 }
164
165                 public ZipArchive Archive {
166                         get;
167                         private set;
168                 }
169
170                 public long CompressedLength {
171                         get {
172                                 if (Archive.Mode == ZipArchiveMode.Create)
173                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
174
175                                 return entry.CompressedSize;
176                         }
177                 }
178
179                 public string FullName {
180                         get { return entry.Key; }
181                 }
182
183                 public DateTimeOffset LastWriteTime {
184                         get { return entry.LastModifiedTime.GetValueOrDefault(); }
185                         set { entry.LastModifiedTime = value.DateTime; }
186                 }
187
188                 public long Length {
189                         get {
190                                 if (Archive.Mode == ZipArchiveMode.Create)
191                                         throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
192
193                                 return entry.Size;
194                         }
195                 }
196
197                 public string Name {
198                         get { return Path.GetFileName(entry.Key); }
199                 }
200
201                 public void Delete()
202                 {
203                         if (Archive.disposed)
204                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
205
206                         if (Archive.Mode != ZipArchiveMode.Update)
207                                 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
208
209                         if (openStream != null)
210                                 throw new IOException("The entry is already open for reading or writing.");
211
212                         wasDeleted = true;
213                         Archive.RemoveEntryInternal(this);
214                 }
215
216                 public Stream Open()
217                 {
218                         if (Archive.disposed)
219                                 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
220
221                         if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
222                                 throw new IOException("The entry is already currently open for writing.");
223
224                         if (wasDeleted)
225                                 throw new IOException("The entry has been deleted from the archive.");
226
227                         if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
228                                 throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
229
230                         var entryStream = entry.OpenEntryStream();
231                         openStream = new ZipArchiveEntryStream(this, entryStream);
232                         openStream.EnsureWriteable();
233
234                         return openStream;
235                 }
236
237                 public override string ToString()
238                 {
239                         return FullName;
240                 }
241         }
242 }