Merge pull request #1805 from martincostello/master
[mono.git] / mcs / class / System.IO.Compression / ZipArchiveEntry.cs
index a92de5aff846d6e0264a579385a521449b2b5b13..cd783b8eb0c84b33b3f5a7492afb2496b28afde6 100644 (file)
@@ -2,6 +2,7 @@
 // ZipArchiveEntry.cs
 //
 // Author:
+//       Joao Matos <joao.matos@xamarin.com>
 //       Martin Baulig <martin.baulig@xamarin.com>
 //
 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
-using System;
+
+using SharpCompress.Archive;
 
 namespace System.IO.Compression
 {
-       [MonoTODO]
        public class ZipArchiveEntry
        {
+               readonly SharpCompress.Archive.Zip.ZipArchiveEntry entry;
+               private Stream openStream;
+               private bool wasDeleted;
+
+               internal ZipArchiveEntry(ZipArchive     archive, SharpCompress.Archive.Zip.ZipArchiveEntry entry)
+               {
+                       if (archive == null)
+                               throw new ArgumentNullException("archive");
+
+                       if (entry == null)
+                               throw new ArgumentNullException("entry");
+
+                       Archive = archive;
+                       this.entry = entry;
+               }
+
                public ZipArchive Archive {
                        get;
                        private set;
                }
 
                public long CompressedLength {
-                       get;
-                       private set;
+                       get {
+                               if (Archive.Mode == ZipArchiveMode.Create)
+                                       throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
+
+                               return entry.CompressedSize;
+                       }
                }
 
                public string FullName {
-                       get;
-                       private set;
+                       get { return entry.Key; }
                }
 
                public DateTimeOffset LastWriteTime {
-                       get; set;
+                       get { return entry.LastModifiedTime.GetValueOrDefault(); }
+                       set { entry.LastModifiedTime = value.DateTime; }
                }
 
                public long Length {
-                       get;
-                       private set;
+                       get {
+                               if (Archive.Mode == ZipArchiveMode.Create)
+                                       throw new InvalidOperationException("Property cannot be retrieved when the mode is set to Create");
+
+                               return entry.Size;
+                       }
                }
 
                public string Name {
-                       get;
-                       private set;
+                       get { return Path.GetFileName(entry.Key); }
                }
 
-               public void Delete ()
+               public void Delete()
                {
-                       throw new NotImplementedException ();
+                       if (Archive.disposed)
+                               throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
+
+                       if (Archive.Mode !=     ZipArchiveMode.Update)
+                               throw new NotSupportedException("The zip archive for this entry was opened in a mode other than Update.");
+
+                       if (openStream != null)
+                               throw new IOException("The entry is already open for reading or writing.");
+
+                       wasDeleted = true;
+                       Archive.zipFile.RemoveEntry(entry);
                }
 
-               public Stream Open ()
+               public Stream Open()
                {
-                       throw new NotImplementedException ();
+                       if (Archive.disposed)
+                               throw new ObjectDisposedException("The zip archive for this entry has been disposed.");
+
+                       if (openStream != null && Archive.Mode == ZipArchiveMode.Update)
+                               throw new IOException("The entry is already currently open for writing.");
+
+                       if (wasDeleted)
+                               throw new IOException("The entry has been deleted from the archive.");
+
+                       if (Archive.Mode == ZipArchiveMode.Create && openStream != null)
+                               throw new IOException("The archive for this entry was opened with the Create mode, and this entry has already been written to.");
+
+                       openStream = entry.OpenEntryStream();
+
+                       return openStream;
                }
        }
 }