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 {
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 if (entry.Archive.Mode == ZipArchiveMode.Update && !entry.wasWritten)
100 // Replace the read-only stream with a writeable memory stream.
102 entry.wasWritten = true;
105 stream.Write(buffer, offset, count);
108 internal void SetWriteable()
110 var archive = entry.Archive;
112 var internalEntry = entry.entry;
113 var newEntry = archive.CreateEntryInternal(internalEntry.Key);
114 var newStream = newEntry.OpenEntryStream();
116 var openStream = stream;
117 var position = openStream.Position;
119 entry.openStream = null;
122 openStream.CopyTo(newStream);
123 newStream.Position = position;
124 openStream.Dispose();
126 archive.zipFile.RemoveEntry(internalEntry);
127 entry.entry = newEntry;
131 public new void Dispose()
134 GC.SuppressFinalize(this);
138 protected override void Dispose(bool disposing)
142 entry.openStream = null;
148 public class ZipArchiveEntry
150 internal SharpCompress.Archive.Zip.ZipArchiveEntry entry;
151 internal ZipArchiveEntryStream openStream;
152 internal bool wasWritten;
153 private bool wasDeleted;
155 internal ZipArchiveEntry(ZipArchive archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
158 throw new ArgumentNullException("archive");
161 throw new ArgumentNullException("entry");
167 public ZipArchive Archive {
172 public long CompressedLength {
174 if (Archive.Mode == ZipArchiveMode.Create)
175 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
177 return entry.CompressedSize;
181 public string FullName {
182 get { return entry.Key; }
185 public DateTimeOffset LastWriteTime {
186 get { return entry.LastModifiedTime.GetValueOrDefault(); }
187 set { entry.LastModifiedTime = value.DateTime; }
192 if (Archive.Mode == ZipArchiveMode.Create)
193 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
200 get { return Path.GetFileName(entry.Key); }
205 if (Archive.disposed)
206 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
208 if (Archive.Mode != ZipArchiveMode.Update)
209 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
211 if (openStream != null)
212 throw new IOException("The entry is already open for reading or writing.");
215 Archive.zipFile.RemoveEntry(entry);
220 if (Archive.disposed)
221 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
223 if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
224 throw new IOException("The entry is already currently open for writing.");
227 throw new IOException("The entry has been deleted from the archive.");
229 if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
230 throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
232 var entryStream = entry.OpenEntryStream();
233 openStream = new ZipArchiveEntryStream(this, entryStream);
238 public override string ToString()