PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/ZipEntryFile.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 214 lines | 137 code | 39 blank | 38 comment | 7 complexity | e44c07220d71b2a25265a2698976496a MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2007, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 18 Apr 07 Andy Frank Creation
  7. //
  8. using ICSharpCode.SharpZipLib.Zip;
  9. namespace Fan.Sys
  10. {
  11. /// <summary>
  12. /// ZipEntryFile represents a file entry inside a zip file.
  13. /// </summary>
  14. public class ZipEntryFile : File
  15. {
  16. //////////////////////////////////////////////////////////////////////////
  17. // Construction
  18. //////////////////////////////////////////////////////////////////////////
  19. public ZipEntryFile(ZipFile parent, ZipEntry entry, Uri uri)
  20. : base(uri)
  21. {
  22. this.m_parent = parent;
  23. this.m_entry = entry;
  24. }
  25. public ZipEntryFile(ZipFile parent, ZipEntry entry)
  26. : base(Uri.fromStr("/" + LocalFile.fileNameToUriName(entry.Name)))
  27. {
  28. this.m_parent = parent;
  29. this.m_entry = entry;
  30. }
  31. public ZipEntryFile(Zip parent, ZipEntry entry)
  32. : base(Uri.fromStr("/" + LocalFile.fileNameToUriName(entry.Name)))
  33. {
  34. this.m_parent = parent;
  35. this.m_entry = entry;
  36. }
  37. //////////////////////////////////////////////////////////////////////////
  38. // Obj
  39. //////////////////////////////////////////////////////////////////////////
  40. public override Type @typeof() { return Sys.ZipEntryFileType; }
  41. //////////////////////////////////////////////////////////////////////////
  42. // File
  43. //////////////////////////////////////////////////////////////////////////
  44. public override bool exists()
  45. {
  46. return true;
  47. }
  48. public override Long size()
  49. {
  50. if (isDir()) return null;
  51. long size = m_entry.Size;
  52. if (size < 0) return null;
  53. return Long.valueOf(size);
  54. }
  55. public override DateTime modified()
  56. {
  57. return DateTime.dotnet(m_entry.DateTime.Ticks);
  58. }
  59. public override void modified(DateTime time)
  60. {
  61. throw IOErr.make("ZipEntryFile is readonly").val;
  62. }
  63. public override string osPath()
  64. {
  65. return null;
  66. }
  67. public override File parent()
  68. {
  69. return null;
  70. }
  71. public override List list()
  72. {
  73. return new List(Sys.FileType, 0);
  74. }
  75. public override File normalize()
  76. {
  77. return this;
  78. }
  79. public override File plus(Uri uri, bool checkSlash)
  80. {
  81. // TODO
  82. throw UnsupportedErr.make("ZipEntryFile.plus").val;
  83. }
  84. //////////////////////////////////////////////////////////////////////////
  85. // File Management
  86. //////////////////////////////////////////////////////////////////////////
  87. public override File create()
  88. {
  89. throw IOErr.make("ZipEntryFile is readonly").val;
  90. }
  91. public override File moveTo(File to)
  92. {
  93. throw IOErr.make("ZipEntryFile is readonly").val;
  94. }
  95. public override void delete()
  96. {
  97. throw IOErr.make("ZipEntryFile is readonly").val;
  98. }
  99. public override File deleteOnExit()
  100. {
  101. throw IOErr.make("ZipEntryFile is readonly").val;
  102. }
  103. //////////////////////////////////////////////////////////////////////////
  104. // IO
  105. //////////////////////////////////////////////////////////////////////////
  106. public override Buf open(string mode)
  107. {
  108. throw IOErr.make("ZipEntryFile cannot be opened with random access").val;
  109. }
  110. public override Buf mmap(string mode, long pos, Long size)
  111. {
  112. throw UnsupportedErr.make("ZipEntryFile.mmap").val;
  113. }
  114. public override InStream @in(Long bufSize)
  115. {
  116. try
  117. {
  118. System.IO.Stream ins;
  119. if (m_parent is Zip)
  120. {
  121. // never buffer if using ZipInputStream
  122. ins = new ZipEntryInputStream((m_parent as Zip).m_zipIn);
  123. }
  124. else
  125. {
  126. ins = (m_parent as ZipFile).GetInputStream(m_entry);
  127. // buffer if specified
  128. if (bufSize != null && bufSize.longValue() != 0)
  129. ins = new System.IO.BufferedStream(ins, bufSize.intValue());
  130. }
  131. // return as fan stream
  132. return new SysInStream(ins);
  133. }
  134. catch (System.IO.IOException e)
  135. {
  136. throw IOErr.make(e).val;
  137. }
  138. }
  139. public override OutStream @out(bool append, Long bufSize)
  140. {
  141. throw IOErr.make("ZipEntryFile is readonly").val;
  142. }
  143. //////////////////////////////////////////////////////////////////////////
  144. // ZipEntryInputStream
  145. //////////////////////////////////////////////////////////////////////////
  146. internal class ZipEntryInputStream : System.IO.Stream
  147. {
  148. public ZipEntryInputStream(System.IO.Stream parent) { this.parent = parent; }
  149. // Properties
  150. public override bool CanRead { get { return parent.CanRead; } }
  151. public override bool CanWrite { get { return parent.CanWrite; } }
  152. public override bool CanSeek { get { return parent.CanSeek; } }
  153. public override long Length { get { return parent.Length; } }
  154. public override long Position
  155. {
  156. get { return parent.Position; }
  157. set { parent.Position = value; }
  158. }
  159. // Methods
  160. public override int Read(byte[] buf, int off, int count) { return parent.Read(buf, off, count); }
  161. public override void Write(byte[] buf, int off, int count) { parent.Write(buf, off, count); }
  162. public override long Seek(long off, System.IO.SeekOrigin origin) { return parent.Seek(off, origin); }
  163. public override void SetLength(long val) { parent.SetLength(val); }
  164. public override void Close() {} // don't do anything
  165. public override void Flush() { parent.Flush(); }
  166. // Fields
  167. private System.IO.Stream parent;
  168. }
  169. //////////////////////////////////////////////////////////////////////////
  170. // Fields
  171. //////////////////////////////////////////////////////////////////////////
  172. internal readonly object m_parent;
  173. internal readonly ZipEntry m_entry;
  174. }
  175. }