5 // Joao Matos <joao.matos@xamarin.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
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
28 using SharpCompress.Archive;
30 namespace System.IO.Compression
32 internal class ZipArchiveEntryStream : Stream, IDisposable
34 private readonly ZipArchiveEntry entry;
35 private Stream stream;
37 public override bool CanRead {
39 return stream.CanRead;
43 public override bool CanSeek {
45 return entry.Archive.Mode != ZipArchiveMode.Read;
49 public override bool CanWrite {
51 return entry.Archive.Mode != ZipArchiveMode.Read;
55 public override long Length {
57 return stream.CanWrite ? stream.Length : entry.Length;
61 public override long Position {
63 return stream.Position;
66 stream.Position = value;
70 public ZipArchiveEntryStream(ZipArchiveEntry entry, Stream stream)
76 public override void Flush ()
81 public override long Seek (long offset, SeekOrigin origin)
83 return stream.Seek(offset, origin);
86 public override void SetLength (long value)
88 stream.SetLength(value);
91 public override int Read (byte[] buffer, int offset, int count)
93 return stream.Read(buffer, offset, count);
96 public override void Write (byte[] buffer, int offset, int count)
98 stream.Write(buffer, offset, count);
101 internal void EnsureWriteable()
103 if (entry.Archive.Mode == ZipArchiveMode.Update && !stream.CanWrite)
105 // Replace the read-only stream with a writeable memory stream.
110 internal void SetWriteable()
112 var archive = entry.Archive;
114 var internalEntry = entry.entry;
115 var newEntry = archive.CreateEntryInternal(internalEntry.Key);
116 var newStream = newEntry.OpenEntryStream();
118 var openStream = stream;
119 openStream.CopyTo(newStream);
120 openStream.Dispose();
122 newStream.Position = 0;
124 archive.zipFile.RemoveEntry(internalEntry);
125 entry.entry = newEntry;
129 public new void Dispose()
132 GC.SuppressFinalize(this);
136 protected override void Dispose(bool disposing)
140 entry.openStream = null;
146 public class ZipArchiveEntry
148 internal SharpCompress.Archive.Zip.ZipArchiveEntry entry;
149 internal ZipArchiveEntryStream openStream;
150 private bool wasDeleted;
152 internal ZipArchiveEntry(ZipArchive archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
155 throw new ArgumentNullException("archive");
158 throw new ArgumentNullException("entry");
164 public ZipArchive Archive {
169 public long CompressedLength {
171 if (Archive.Mode == ZipArchiveMode.Create)
172 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
174 return entry.CompressedSize;
178 public string FullName {
179 get { return entry.Key; }
182 public DateTimeOffset LastWriteTime {
183 get { return entry.LastModifiedTime.GetValueOrDefault(); }
184 set { entry.LastModifiedTime = value.DateTime; }
189 if (Archive.Mode == ZipArchiveMode.Create)
190 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
197 get { return Path.GetFileName(entry.Key); }
202 if (Archive.disposed)
203 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
205 if (Archive.Mode != ZipArchiveMode.Update)
206 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
208 if (openStream != null)
209 throw new IOException("The entry is already open for reading or writing.");
212 Archive.RemoveEntryInternal(this);
217 if (Archive.disposed)
218 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
220 if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
221 throw new IOException("The entry is already currently open for writing.");
224 throw new IOException("The entry has been deleted from the archive.");
226 if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
227 throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
229 var entryStream = entry.OpenEntryStream();
230 openStream = new ZipArchiveEntryStream(this, entryStream);
231 openStream.EnsureWriteable();
236 public override string ToString()