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


当前文件路径:x3blogAccessBuild80707/SharpZipLib/Tar/TarArchive.cs 文件类型
普通视图
		            
1// TarArchive.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 /// <summary> 42 /// Used to advise clients of 'events' while processing archives 43 /// </summary> 44 public delegate void ProgressMessageHandler(TarArchive archive, TarEntry entry, string message); 45 46 /// <summary> 47 /// The TarArchive class implements the concept of a 48 /// 'Tape Archive'. A tar archive is a series of entries, each of 49 /// which represents a file system object. Each entry in 50 /// the archive consists of a header block followed by 0 or more data blocks. 51 /// Directory entries consist only of the header block, and are followed by entries 52 /// for the directory's contents. File entries consist of a 53 /// header followed by the number of blocks needed to 54 /// contain the file's contents. All entries are written on 55 /// block boundaries. Blocks are 512 bytes long. 56 /// 57 /// TarArchives are instantiated in either read or write mode, 58 /// based upon whether they are instantiated with an InputStream 59 /// or an OutputStream. Once instantiated TarArchives read/write 60 /// mode can not be changed. 61 /// 62 /// There is currently no support for random access to tar archives. 63 /// However, it seems that subclassing TarArchive, and using the 64 /// TarBuffer.getCurrentRecordNum() and TarBuffer.getCurrentBlockNum() 65 /// methods, this would be rather trvial. 66 /// </summary> 67 public class TarArchive 68 { 69 bool keepOldFiles; 70 bool asciiTranslate; 71 72 int userId; 73 string userName; 74 int groupId; 75 string groupName; 76 77 string rootPath; 78 string pathPrefix; 79 80 int recordSize; 81 byte[] recordBuf; 82 83 TarInputStream tarIn; 84 TarOutputStream tarOut; 85 86 /// <summary> 87 /// Client hook allowing detailed information to be reported during processing 88 /// </summary> 89 public event ProgressMessageHandler ProgressMessageEvent; 90 91 /// <summary> 92 /// Raises the ProgressMessage event 93 /// </summary> 94 /// <param name="entry">TarEntry for this event</param> 95 /// <param name="message">message for this event. Null is no message</param> 96 protected virtual void OnProgressMessageEvent(TarEntry entry, string message) 97 { 98 if (ProgressMessageEvent != null) { 99 ProgressMessageEvent(this, entry, message); 100 } 101 } 102 103 /// <summary> 104 /// Constructor for a TarArchive. 105 /// </summary> 106 protected TarArchive() 107 { 108 } 109 110 /// <summary> 111 /// The InputStream based constructors create a TarArchive for the 112 /// purposes of extracting or listing a tar archive. Thus, use 113 /// these constructors when you wish to extract files from or list 114 /// the contents of an existing tar archive. 115 /// </summary> 116 public static TarArchive CreateInputTarArchive(Stream inputStream) 117 { 118 return CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor); 119 } 120 121 /// <summary> 122 /// Create TarArchive for reading setting block factor 123 /// </summary> 124 /// <param name="inputStream">Stream for tar archive contents</param> 125 /// <param name="blockFactor">The blocking factor to apply</param> 126 /// <returns> 127 /// TarArchive 128 /// </returns> 129 public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor) 130 { 131 TarArchive archive = new TarArchive(); 132 archive.tarIn = new TarInputStream(inputStream, blockFactor); 133 archive.Initialize(blockFactor * TarBuffer.BlockSize); 134 return archive; 135 } 136 137 /// <summary> 138 /// Create a TarArchive for writing to, using the default blocking factor 139 /// </summary> 140 /// <param name="outputStream">Stream to write to</param> 141 public static TarArchive CreateOutputTarArchive(Stream outputStream) 142 { 143 return CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor); 144 } 145 146 /// <summary> 147 /// Create a TarArchive for writing to 148 /// </summary> 149 /// <param name="outputStream">The stream to write to</param> 150 /// <param name="blockFactor">The blocking factor to use for buffering.</param> 151 public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor) 152 { 153 TarArchive archive = new TarArchive(); 154 archive.tarOut = new TarOutputStream(outputStream, blockFactor); 155 archive.Initialize(blockFactor * TarBuffer.BlockSize); 156 return archive; 157 } 158 159 /// <summary> 160 /// Common constructor initialization code. 161 /// </summary> 162 void Initialize(int recordSize) 163 { 164 this.recordSize = recordSize; 165 this.rootPath = null; 166 this.pathPrefix = null; 167 168 this.userId = 0; 169 this.userName = String.Empty; 170 this.groupId = 0; 171 this.groupName = String.Empty; 172 173 this.keepOldFiles = false; 174 175 this.recordBuf = new byte[RecordSize]; 176 } 177 178 /// <summary> 179 /// Set the flag that determines whether existing files are 180 /// kept, or overwritten during extraction. 181 /// </summary> 182 /// <param name="keepOldFiles"> 183 /// If true, do not overwrite existing files. 184 /// </param> 185 public void SetKeepOldFiles(bool keepOldFiles) 186 { 187 this.keepOldFiles = keepOldFiles; 188 } 189 190 /// <summary> 191 /// Set the ascii file translation flag. If ascii file translation 192 /// is true, then the file is checked to see if it a binary file or not. 193 /// If the flag is true and the test indicates it is ascii text 194 /// file, it will be translated. The translation converts the local 195 /// operating system's concept of line ends into the UNIX line end, 196 /// '\n', which is the defacto standard for a TAR archive. This makes 197 /// text files compatible with UNIX. 198 /// </summary> 199 /// <param name= "asciiTranslate"> 200 /// If true, translate ascii text files. 201 /// </param> 202 public void SetAsciiTranslation(bool asciiTranslate) 203 { 204 this.asciiTranslate = asciiTranslate; 205 } 206 207 /// <summary> 208 /// PathPrefix is added to entry names as they are written if the value is not null. 209 /// A slash character is appended after PathPrefix 210 /// </summary> 211 public string PathPrefix 212 { 213 get { return pathPrefix; } 214 set { pathPrefix = value; } 215 216 } 217 218 /// <summary> 219 /// RootPath is removed from entry names if it is found at the 220 /// beginning of the name. 221 /// </summary> 222 public string RootPath 223 { 224 get { return rootPath; } 225 set { rootPath = value; } 226 } 227 228 /// <summary> 229 /// Set user and group information that will be used to fill in the 230 /// tar archive's entry headers. This information based on that available 231 /// for the linux operating system, which is not always available on other 232 /// operating systems. TarArchive allows the programmer to specify values 233 /// to be used in their place. 234 /// </summary> 235 /// <param name="userId"> 236 /// The user id to use in the headers. 237 /// </param> 238 /// <param name="userName"> 239 /// The user name to use in the headers. 240 /// </param> 241 /// <param name="groupId"> 242 /// The group id to use in the headers. 243 /// </param> 244 /// <param name="groupName"> 245 /// The group name to use in the headers. 246 /// </param> 247 public void SetUserInfo(int userId, string userName, int groupId, string groupName) 248 { 249 this.userId = userId; 250 this.userName = userName; 251 this.groupId = groupId; 252 this.groupName = groupName; 253 applyUserInfoOverrides = true; 254 } 255 256 bool applyUserInfoOverrides = false; 257 258 /// <summary> 259 /// Get or set a value indicating if overrides defined by <see cref="SetUserInfo">SetUserInfo</see> should be applied. 260 /// </summary> 261 /// <remarks>If overrides are not applied then the values as set in each header will be used.</remarks> 262 public bool ApplyUserInfoOverrides 263 { 264 get { return applyUserInfoOverrides; } 265 set { applyUserInfoOverrides = value; } 266 } 267 268 /// <summary> 269 /// Get the archive user id. 270 /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail 271 /// on how to allow setting values on a per entry basis. 272 /// </summary> 273 /// <returns> 274 /// The current user id. 275 /// </returns> 276 public int UserId { 277 get { 278 return this.userId; 279 } 280 } 281 282 /// <summary> 283 /// Get the archive user name. 284 /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail 285 /// on how to allow setting values on a per entry basis. 286 /// </summary> 287 /// <returns> 288 /// The current user name. 289 /// </returns> 290 public string UserName { 291 get { 292 return this.userName; 293 } 294 } 295 296 /// <summary> 297 /// Get the archive group id. 298