CreateTarEntry() public static method

Construct an entry with only a name. This allows the programmer to construct the entry's header "by hand".
public static CreateTarEntry ( string name ) : TarEntry
name string The name to use for the entry
return TarEntry
コード例 #1
0
 /// <summary>
 /// Create a <see cref="TarEntry"/> based on named
 /// </summary>
 /// <param name="name">The name to use for the entry</param>
 /// <returns>A new <see cref="TarEntry"/></returns>
 public TarEntry CreateEntry(string name)
 {
     return(TarEntry.CreateTarEntry(name));
 }
コード例 #2
0
        /// <summary>
        /// Write an entry to the archive. This method will call the putNextEntry
        /// and then write the contents of the entry, and finally call closeEntry()
        /// for entries that are files. For directories, it will call putNextEntry(),
        /// and then, if the recurse flag is true, process each entry that is a
        /// child of the directory.
        /// </summary>
        /// <param name="entry">
        /// The TarEntry representing the entry to write to the archive.
        /// </param>
        /// <param name="recurse">
        /// If true, process the children of directory entries.
        /// </param>
        public void WriteEntry(TarEntry entry, bool recurse)
        {
            bool asciiTrans = false;

            string tempFileName = null;
            string eFile        = entry.File;

            // Work on a copy of the entry so we can manipulate it.
            // Note that we must distinguish how the entry was constructed.
            //
            if (eFile == null || eFile.Length == 0)
            {
                entry = TarEntry.CreateTarEntry(entry.Name);
            }
            else
            {
                //
                // The user may have explicitly set the entry's name to
                // something other than the file's path, so we must save
                // and restore it. This should work even when the name
                // was set from the File's name.
                //
                string saveName = entry.Name;
                entry      = TarEntry.CreateEntryFromFile(eFile);
                entry.Name = saveName;
            }

            if (this.verbose)
            {
                OnProgressMessageEvent(entry, null);
            }

            if (this.asciiTranslate && !entry.IsDirectory)
            {
                asciiTrans = !IsBinary(eFile);

// original java source :
//					MimeType mime = null;
//					string contentType = null;
//
//					try {
//						contentType = FileTypeMap.getDefaultFileTypeMap(). getContentType( eFile );
//
//						mime = new MimeType( contentType );
//
//						if ( mime.getPrimaryType().equalsIgnoreCase( "text" ) )
//						{
//							asciiTrans = true;
//						}
//						else if ( this.transTyper != null )
//						{
//							if ( this.transTyper.isAsciiFile( eFile ) )
//							{
//								asciiTrans = true;
//							}
//						}
//				} catch ( MimeTypeParseException ex )
//				{
//	//				 IGNORE THIS ERROR...
//				}
//
//			if (this.debug) {
//				Console.Error.WriteLine("CREATE TRANS? '" + asciiTrans + "'  ContentType='" + contentType + "'  PrimaryType='" + mime.getPrimaryType()+ "'" );
//			}

                if (asciiTrans)
                {
                    tempFileName = Path.GetTempFileName();

                    StreamReader inStream = File.OpenText(eFile);
// -jr- 22-Jun-2004 Was using BufferedStream but this is not available for compact framework
//					Stream       outStream = new BufferedStream(File.Create(tempFileName));
                    Stream outStream = File.Create(tempFileName);

                    while (true)
                    {
                        string line = inStream.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        byte[] data = Encoding.ASCII.GetBytes(line);
                        outStream.Write(data, 0, data.Length);
                        outStream.WriteByte((byte)'\n');
                    }

                    inStream.Close();

                    outStream.Flush();
                    outStream.Close();

                    entry.Size = new FileInfo(tempFileName).Length;

                    eFile = tempFileName;
                }
            }

            string newName = null;

            if (this.rootPath != null)
            {
                if (entry.Name.StartsWith(this.rootPath))
                {
                    newName = entry.Name.Substring(this.rootPath.Length + 1);
                }
            }

            if (this.pathPrefix != null)
            {
                newName = (newName == null) ? this.pathPrefix + "/" + entry.Name : this.pathPrefix + "/" + newName;
            }

            if (newName != null)
            {
                entry.Name = newName;
            }

            this.tarOut.PutNextEntry(entry);

            if (entry.IsDirectory)
            {
                if (recurse)
                {
                    TarEntry[] list = entry.GetDirectoryEntries();
                    for (int i = 0; i < list.Length; ++i)
                    {
                        this.WriteEntry(list[i], recurse);
                    }
                }
            }
            else
            {
                Stream inputStream = File.OpenRead(eFile);
                int    numWritten  = 0;
                byte[] eBuf        = new byte[32 * 1024];
                while (true)
                {
                    int numRead = inputStream.Read(eBuf, 0, eBuf.Length);

                    if (numRead <= 0)
                    {
                        break;
                    }

                    this.tarOut.Write(eBuf, 0, numRead);
                    numWritten += numRead;
                }

                inputStream.Close();

                if (tempFileName != null && tempFileName.Length > 0)
                {
                    File.Delete(tempFileName);
                }

                this.tarOut.CloseEntry();
            }
        }