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 internal bool wasWritten;
151 private bool wasDeleted;
153 internal ZipArchiveEntry(ZipArchive archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
156 throw new ArgumentNullException("archive");
159 throw new ArgumentNullException("entry");
165 public ZipArchive Archive {
170 public long CompressedLength {
172 if (Archive.Mode == ZipArchiveMode.Create)
173 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
175 return entry.CompressedSize;
179 public string FullName {
180 get { return entry.Key; }
183 public DateTimeOffset LastWriteTime {
184 get { return entry.LastModifiedTime.GetValueOrDefault(); }
185 set { entry.LastModifiedTime = value.DateTime; }
190 if (Archive.Mode == ZipArchiveMode.Create)
191 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
198 get { return Path.GetFileName(entry.Key); }
203 if (Archive.disposed)
204 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
206 if (Archive.Mode != ZipArchiveMode.Update)
207 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
209 if (openStream != null)
210 throw new IOException("The entry is already open for reading or writing.");
213 Archive.RemoveEntryInternal(this);
218 if (Archive.disposed)
219 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
221 if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
222 throw new IOException("The entry is already currently open for writing.");
225 throw new IOException("The entry has been deleted from the archive.");
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.");
230 var entryStream = entry.OpenEntryStream();
231 openStream = new ZipArchiveEntryStream(this, entryStream);
232 openStream.EnsureWriteable();
237 public override string ToString()