您目前尚未登陆,请选择【登陆】或【注册
首页->博客论坛->X3BLOG 单用户1.0 build80707(ACCESS)源代码>>SharpZipLib/Tar/TarOutputStream.cs>>代码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:X3BLOG 单用户1.0 build80707(ACCESS)源代码


当前文件路径:x3blogAccessBuild80707/SharpZipLib/Tar/TarOutputStream.cs 文件类型
普通视图
		            
1// TarOutputStream.cs 2// 3// Copyright (C) 2001 Mike Krueger 4// 5// This program is free software; you can redistribute it and/or 6// modify it under the terms of the GNU General Public License 7// as published by the Free Software Foundation; either version 2 8// of the License, or (at your option) any later version. 9// 10// This program is distributed in the hope that it will be useful, 11// but WITHOUT ANY WARRANTY; without even the implied warranty of 12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13// GNU General Public License for more details. 14// 15// You should have received a copy of the GNU General Public License 16// along with this program; if not, write to the Free Software 17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18// 19// Linking this library statically or dynamically with other modules is 20// making a combined work based on this library. Thus, the terms and 21// conditions of the GNU General Public License cover the whole 22// combination. 23// 24// As a special exception, the copyright holders of this library give you 25// permission to link this library with independent modules to produce an 26// executable, regardless of the license terms of these independent 27// modules, and to copy and distribute the resulting executable under 28// terms of your choice, provided that you also meet, for each linked 29// independent module, the terms and conditions of the license of that 30// module. An independent module is a module which is not derived from 31// or based on this library. If you modify this library, you may extend 32// this exception to your version of the library, but you are not 33// obligated to do so. If you do not wish to do so, delete this 34// exception statement from your version. 35 36using System; 37using System.IO; 38using System.Text; 39 40namespace ICSharpCode.SharpZipLib.Tar 41{ 42 43 /// <summary> 44 /// The TarOutputStream writes a UNIX tar archive as an OutputStream. 45 /// Methods are provided to put entries, and then write their contents 46 /// by writing to this stream using write(). 47 /// </summary> 48 /// public 49 public class TarOutputStream : Stream 50 { 51 /// <summary> 52 /// flag indicating debugging code should be activated or not 53 /// </summary> 54 protected bool debug; 55 56 /// <summary> 57 /// Size for the current entry 58 /// </summary> 59 protected long currSize; 60 61 /// <summary> 62 /// bytes written for this entry so far 63 /// </summary> 64 protected long currBytes; 65 66 /// <summary> 67 /// single block working buffer 68 /// </summary> 69 protected byte[] blockBuf; 70 71 /// <summary> 72 /// current 'Assembly' buffer length 73 /// </summary> 74 protected int assemLen; 75 76 /// <summary> 77 /// 'Assembly' buffer used to assmble data before writing 78 /// </summary> 79 protected byte[] assemBuf; 80 81 /// <summary> 82 /// TarBuffer used to provide correct blocking factor 83 /// </summary> 84 protected TarBuffer buffer; 85 86 /// <summary> 87 /// the destination stream for the archive contents 88 /// </summary> 89 protected Stream outputStream; 90 91 /// <summary> 92 /// true if the stream supports reading; otherwise, false. 93 /// </summary> 94 public override bool CanRead { 95 get { 96 return outputStream.CanRead; 97 } 98 } 99 100 /// <summary> 101 /// true if the stream supports seeking; otherwise, false. 102 /// </summary> 103 public override bool CanSeek { 104 get { 105 return outputStream.CanSeek; 106 } 107 } 108 109 /// <summary> 110 /// true if stream supports writing; otherwise, false. 111 /// </summary> 112 public override bool CanWrite { 113 get { 114 return outputStream.CanWrite; 115 } 116 } 117 118 /// <summary> 119 /// length of stream in bytes 120 /// </summary> 121 public override long Length { 122 get { 123 return outputStream.Length; 124 } 125 } 126 127 /// <summary> 128 /// gets or sets the position within the current stream. 129 /// </summary> 130 public override long Position { 131 get { 132 return outputStream.Position; 133 } 134 set { 135 outputStream.Position = value; 136 } 137 } 138 139 /// <summary> 140 /// set the position within the current stream 141 /// </summary> 142 public override long Seek(long offset, SeekOrigin origin) 143 { 144 return outputStream.Seek(offset, origin); 145 } 146 147 /// <summary> 148 /// set the length of the current stream 149 /// </summary> 150 public override void SetLength(long val) 151 { 152 outputStream.SetLength(val); 153 } 154 155 /// <summary> 156 /// Read a byte from the stream and advance the position within the stream 157 /// by one byte or returns -1 if at the end of the stream. 158 /// </summary> 159 /// <returns>The byte value or -1 if at end of stream</returns> 160 public override int ReadByte() 161 { 162 return outputStream.ReadByte(); 163 } 164 165 /// <summary> 166 /// read bytes from the current stream and advance the position within the 167 /// stream by the number of bytes read. 168 /// </summary> 169 /// <returns>The total number of bytes read, or zero if at the end of the stream</returns> 170 public override int Read(byte[] b, int off, int len) 171 { 172 return outputStream.Read(b, off, len); 173 } 174 175 /// <summary> 176 /// All buffered data is written to destination 177 /// </summary> 178 public override void Flush() 179 { 180 outputStream.Flush(); 181 } 182 183 /// <summary> 184 /// Construct TarOutputStream using default block factor 185 /// </summary> 186 /// <param name="outputStream">stream to write to</param> 187 public TarOutputStream(Stream outputStream) : this(outputStream, TarBuffer.DefaultBlockFactor) 188 { 189 } 190 191 /// <summary> 192 /// Construct TarOutputStream with user specified block factor 193 /// </summary> 194 /// <param name="outputStream">stream to write to</param> 195 /// <param name="blockFactor">blocking factor</param> 196 public TarOutputStream(Stream outputStream, int blockFactor) 197 { 198 this.outputStream = outputStream; 199 this.buffer = TarBuffer.CreateOutputTarBuffer(outputStream, blockFactor); 200 201 this.debug = false; 202 this.assemLen = 0; 203 this.assemBuf = new byte[TarBuffer.BlockSize]; 204 this.blockBuf = new byte[TarBuffer.BlockSize]; 205 } 206 207 /// <summary> 208 /// Ends the TAR archive without closing the underlying OutputStream. 209 /// The result is that the EOF record of nulls is written. 210 /// </summary> 211 public void Finish() 212 { 213 this.WriteEOFRecord(); 214 } 215 216 /// <summary> 217 /// Ends the TAR archive and closes the underlying OutputStream. 218 /// This means that finish() is called followed by calling the 219 /// TarBuffer's close(). 220 /// </summary> 221 public override void Close() 222 { 223 this.Finish(); 224 this.buffer.Close(); 225 } 226 227 /// <summary> 228 /// Get the record size being used by this stream's TarBuffer. 229 /// </summary> 230 /// <returns> 231 /// The TarBuffer record size. 232 /// </returns> 233 public int GetRecordSize() 234 { 235 return this.buffer.GetRecordSize(); 236 } 237 238 /// <summary> 239 /// Put an entry on the output stream. This writes the entry's 240 /// header and positions the output stream for writing 241 /// the contents of the entry. Once this method is called, the 242 /// stream is ready for calls to write() to write the entry's 243 /// contents. Once the contents are written, closeEntry() 244 /// <B>MUST</B> be called to ensure that all buffered data 245 /// is completely written to the output stream. 246 /// </summary> 247 /// <param name="entry"> 248 /// The TarEntry to be written to the archive. 249 /// </param> 250 public void PutNextEntry(TarEntry entry) 251 { 252 if (entry.TarHeader.Name.Length >= TarHeader.NAMELEN) { 253 TarHeader longHeader = new TarHeader(); 254 longHeader.TypeFlag = TarHeader.LF_GNU_LONGNAME; 255 longHeader.Name = longHeader.Name + "././@LongLink"; 256 longHeader.UserId = 0; 257 longHeader.GroupId = 0; 258 longHeader.GroupName = ""; 259 longHeader.UserName = ""; 260 longHeader.LinkName = ""; 261 262 longHeader.Size = entry.TarHeader.Name.Length; 263 264 longHeader.WriteHeader(this.blockBuf); 265 this.buffer.WriteBlock(this.blockBuf); // Add special long filename header block 266 267 int nameCharIndex = 0; 268 269 while (nameCharIndex < entry.TarHeader.Name.Length) { 270 Array.Clear(blockBuf, 0, blockBuf.Length); 271 TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameCharIndex, this.blockBuf, 0, TarBuffer.BlockSize); 272 nameCharIndex += TarBuffer.BlockSize; 273 this.buffer.WriteBlock(this.blockBuf); 274 } 275 } 276 277 entry.WriteEntryHeader(this.blockBuf); 278 this.buffer.WriteBlock(this.blockBuf); 279 280 this.currBytes = 0; 281 282 this.currSize = entry.IsDirectory ? 0 : entry.Size; 283 } 284 285