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 readonly Stream stream;
37 public override bool CanRead {
39 return stream.CanRead;
43 public override bool CanSeek {
45 return stream.CanSeek;
49 public override bool CanWrite {
51 return stream.CanWrite;
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 stream.Write(buffer, offset, count);
101 public new void Dispose()
104 GC.SuppressFinalize(this);
108 protected override void Dispose(bool disposing)
112 entry.openStream = null;
118 public class ZipArchiveEntry
120 readonly SharpCompress.Archive.Zip.ZipArchiveEntry entry;
121 internal ZipArchiveEntryStream openStream;
122 private bool wasDeleted;
124 internal ZipArchiveEntry(ZipArchive archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
127 throw new ArgumentNullException("archive");
130 throw new ArgumentNullException("entry");
136 public ZipArchive Archive {
141 public long CompressedLength {
143 if (Archive.Mode == ZipArchiveMode.Create)
144 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
146 return entry.CompressedSize;
150 public string FullName {
151 get { return entry.Key; }
154 public DateTimeOffset LastWriteTime {
155 get { return entry.LastModifiedTime.GetValueOrDefault(); }
156 set { entry.LastModifiedTime = value.DateTime; }
161 if (Archive.Mode == ZipArchiveMode.Create)
162 throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
169 get { return Path.GetFileName(entry.Key); }
174 if (Archive.disposed)
175 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
177 if (Archive.Mode != ZipArchiveMode.Update)
178 throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
180 if (openStream != null)
181 throw new IOException("The entry is already open for reading or writing.");
184 Archive.zipFile.RemoveEntry(entry);
189 if (Archive.disposed)
190 throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
192 if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
193 throw new IOException("The entry is already currently open for writing.");
196 throw new IOException("The entry has been deleted from the archive.");
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.");
201 openStream = new ZipArchiveEntryStream(this, entry.OpenEntryStream());