The TarArchive class implements the concept of a 'Tape Archive'. A tar archive is a series of entries, each of which represents a file system object. Each entry in the archive consists of a header block followed by 0 or more data blocks. Directory entries consist only of the header block, and are followed by entries for the directory's contents. File entries consist of a header followed by the number of blocks needed to contain the file's contents. All entries are written on block boundaries. Blocks are 512 bytes long. TarArchives are instantiated in either read or write mode, based upon whether they are instantiated with an InputStream or an OutputStream. Once instantiated TarArchives read/write mode can not be changed. There is currently no support for random access to tar archives. However, it seems that subclassing TarArchive, and using the TarBuffer.CurrentRecord and TarBuffer.CurrentBlock properties, this would be rather trivial.
Inheritance: IDisposable
Exemplo n.º 1
1
 public static void MyLister(TarArchive ta, TarEntry te, string msg)
 {
     if (te.Size > 0)
     {
         Console.WriteLine(te.Name + " " + te.Size);
         totalsize = totalsize + 1;
     }
 }
Exemplo n.º 2
1
		/// <summary>
		/// Recursively adds folders and files to archive
		/// </summary>
		/// <param name="tarArchive"></param>
		/// <param name="sourceDirectory"></param>
		/// <param name="recurse"></param>
		public static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
		{
			// Recursively add sub-folders
			if (recurse)
			{
				string[] directories = Directory.GetDirectories(sourceDirectory);
				foreach (string directory in directories)
					AddDirectoryFilesToTar(tarArchive, directory, recurse);
			}

			// Add files
			string[] filenames = Directory.GetFiles(sourceDirectory);
			foreach (string filename in filenames)
			{
				TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
				tarArchive.WriteEntry(tarEntry, true);
			}
		}
Exemplo n.º 3
0
        private static void AddDirectoryFilesToTar(ICSharpCode.SharpZipLib.Tar.TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);

            tarArchive.WriteEntry(tarEntry, false);

            string[] filenames = System.IO.Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = System.IO.Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
                }
            }
        }
Exemplo n.º 4
0
        internal static void Create(Models.FileTransportInfo fileTransferInfo)
        {
            if (fileTransferInfo.SourceIsDirectory == false)
            {
                throw new InvalidOperationException("File archive is not supported. Need to support folder only");
            }
            using (System.IO.Stream outStream = System.IO.File.Create(fileTransferInfo.DestinationFullName))
            {
                using (ICSharpCode.SharpZipLib.Tar.TarArchive tarArchive = ICSharpCode.SharpZipLib.Tar.TarArchive.CreateOutputTarArchive(outStream))
                {
                    tarArchive.RootPath = fileTransferInfo.BasePath.Replace(@"\", "/");
                    if (tarArchive.RootPath.EndsWith("/"))
                    {
                        tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1);
                    }

                    AddDirectoryFilesToTar(tarArchive, fileTransferInfo.SourceFullNameWithBasePath, true);

                    tarArchive.Close();
                }
            }
        }
Exemplo n.º 5
0
        static private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);
            tarArchive.WriteEntry(tarEntry, false);
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
            }
        }
        /// <summary>
        /// Recursively adds folders and files to archive
        /// </summary>
        // <param name="root"></param>
        /// <param name="tarArchive"></param>
        /// <param name="sourceDirectory"></param>
        /// <param name="recurse"></param>
        private static void AddDirectoryFilesToTar(string root,TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            // Recursively add sub-folders
            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                    AddDirectoryFilesToTar(root,tarArchive, directory, recurse);
            }

            // Add files
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);

                string name = tarEntry.Name;

                if (name.StartsWith("./"))
                {
                    name = name.Substring(2, name.Length-2);
                }

                if (!String.IsNullOrEmpty(root))
                {
                    name = root + "/" + name;
                }

                tarEntry.Name = name;

                Console.WriteLine("{0} -> {1}", tarEntry.File, tarEntry.Name);

                tarArchive.WriteEntry(tarEntry, true);
            }
        }
Exemplo n.º 7
0
 public void MyNotifier(TarArchive ta, TarEntry te, string msg)
 {
     if (te.Size > 0)
     {
         Invoke(new Action(() =>
         {
             label1.Text = "Descomprimiendo: " + te.Name;
             totalDess = totalDess + 1;
             progressBar1.Value = totalDess;
         }));
     }
 }
Exemplo n.º 8
0
 void tar_ProgressMessageEvent(TarArchive archive, TarEntry entry, string message)
 {
     Invoke(new delegateUpdateLabel((string s) => { this.label2.Text = s; }), new Object[] { entry.Name + " extracting ..." });
 }
Exemplo n.º 9
0
        private void WriteEntryCore(TarEntry sourceEntry, bool recurse)
        {
            string   text     = null;
            string   text2    = sourceEntry.File;
            TarEntry tarEntry = (TarEntry)sourceEntry.Clone();

            if (this.applyUserInfoOverrides)
            {
                tarEntry.GroupId   = this.groupId;
                tarEntry.GroupName = this.groupName;
                tarEntry.UserId    = this.userId;
                tarEntry.UserName  = this.userName;
            }
            this.OnProgressMessageEvent(tarEntry, null);
            if (this.asciiTranslate && !tarEntry.IsDirectory)
            {
                bool flag = !TarArchive.IsBinary(text2);
                if (flag)
                {
                    text = Path.GetTempFileName();
                    using (StreamReader streamReader = File.OpenText(text2))
                    {
                        using (Stream stream = File.Create(text))
                        {
                            for (;;)
                            {
                                string text3 = streamReader.ReadLine();
                                if (text3 == null)
                                {
                                    break;
                                }
                                byte[] bytes = Encoding.ASCII.GetBytes(text3);
                                stream.Write(bytes, 0, bytes.Length);
                                stream.WriteByte(10);
                            }
                            stream.Flush();
                        }
                    }
                    tarEntry.Size = new FileInfo(text).Length;
                    text2         = text;
                }
            }
            string text4 = null;

            if (this.rootPath != null && tarEntry.Name.StartsWith(this.rootPath))
            {
                text4 = tarEntry.Name.Substring(this.rootPath.Length + 1);
            }
            if (this.pathPrefix != null)
            {
                text4 = ((text4 == null) ? (this.pathPrefix + "/" + tarEntry.Name) : (this.pathPrefix + "/" + text4));
            }
            if (text4 != null)
            {
                tarEntry.Name = text4;
            }
            this.tarOut.PutNextEntry(tarEntry);
            if (tarEntry.IsDirectory)
            {
                if (recurse)
                {
                    TarEntry[] directoryEntries = tarEntry.GetDirectoryEntries();
                    for (int i = 0; i < directoryEntries.Length; i++)
                    {
                        this.WriteEntryCore(directoryEntries[i], recurse);
                    }
                    return;
                }
            }
            else
            {
                using (Stream stream2 = File.OpenRead(text2))
                {
                    int    num   = 0;
                    byte[] array = new byte[32768];
                    for (;;)
                    {
                        int num2 = stream2.Read(array, 0, array.Length);
                        if (num2 <= 0)
                        {
                            break;
                        }
                        this.tarOut.Write(array, 0, num2);
                        num += num2;
                    }
                }
                if (text != null && text.Length > 0)
                {
                    File.Delete(text);
                }
                this.tarOut.CloseEntry();
            }
        }
Exemplo n.º 10
0
        private void ExtractEntry(string destDir, TarEntry entry)
        {
            this.OnProgressMessageEvent(entry, null);
            string text = entry.Name;

            if (Path.IsPathRooted(text))
            {
                text = text.Substring(Path.GetPathRoot(text).Length);
            }
            text = text.Replace('/', Path.DirectorySeparatorChar);
            string text2 = Path.Combine(destDir, text);

            if (entry.IsDirectory)
            {
                TarArchive.EnsureDirectoryExists(text2);
                return;
            }
            string directoryName = Path.GetDirectoryName(text2);

            TarArchive.EnsureDirectoryExists(directoryName);
            bool     flag     = true;
            FileInfo fileInfo = new FileInfo(text2);

            if (fileInfo.Exists)
            {
                if (this.keepOldFiles)
                {
                    this.OnProgressMessageEvent(entry, "Destination file already exists");
                    flag = false;
                }
                else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != (FileAttributes)0)
                {
                    this.OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
                    flag = false;
                }
            }
            if (flag)
            {
                bool   flag2  = false;
                Stream stream = File.Create(text2);
                if (this.asciiTranslate)
                {
                    flag2 = !TarArchive.IsBinary(text2);
                }
                StreamWriter streamWriter = null;
                if (flag2)
                {
                    streamWriter = new StreamWriter(stream);
                }
                byte[] array = new byte[32768];
                for (;;)
                {
                    int num = this.tarIn.Read(array, 0, array.Length);
                    if (num <= 0)
                    {
                        break;
                    }
                    if (flag2)
                    {
                        int num2 = 0;
                        for (int i = 0; i < num; i++)
                        {
                            if (array[i] == 10)
                            {
                                string @string = Encoding.ASCII.GetString(array, num2, i - num2);
                                streamWriter.WriteLine(@string);
                                num2 = i + 1;
                            }
                        }
                    }
                    else
                    {
                        stream.Write(array, 0, num);
                    }
                }
                if (flag2)
                {
                    streamWriter.Close();
                    return;
                }
                stream.Close();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// The InputStream based constructors create a TarArchive for the
        /// purposes of extracting or listing a tar archive. Thus, use
        /// these constructors when you wish to extract files from or list
        /// the contents of an existing tar archive.
        /// </summary>
        /// <param name="inputStream">The stream to retrieve archive data from.</param>
        /// <returns>Returns a new <see cref="TarArchive"/> suitable for reading from.</returns>
        public static TarArchive CreateInputTarArchive(Stream inputStream)
        {
            if (inputStream == null) {
                throw new ArgumentNullException("inputStream");
            }

            var tarStream = inputStream as TarInputStream;

            TarArchive result;
            if (tarStream != null) {
                result = new TarArchive(tarStream);
            } else {
                result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor);
            }
            return result;
        }
Exemplo n.º 12
0
		/// <summary>
		/// Create a TarArchive for writing to, using the default blocking factor
		/// </summary>
		/// <param name="outputStream">The <see cref="Stream"/> to write to</param>
		/// <returns>Returns a <see cref="TarArchive"/> suitable for writing.</returns>
		public static TarArchive CreateOutputTarArchive(Stream outputStream)
		{
			if ( outputStream == null ) {
				throw new ArgumentNullException("outputStream");
			}
			
            TarOutputStream tarStream = outputStream as TarOutputStream;

		    TarArchive result;
			if ( tarStream != null ) {
				result = new TarArchive(tarStream);
			}
			else {
				result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor);
			}
		    return result;
		}
Exemplo n.º 13
0
        private void CreateDirectoryEntry(TarArchive archive, string entryName, TarFileSet fileset)
        {
            // skip directories that were already added before
            if (_addedDirs.ContainsKey(entryName)) {
                return;
            }

            // create directory entry
            TarEntry entry = TarEntry.CreateTarEntry(entryName);
            entry.GroupId = fileset.Gid;
            entry.GroupName = fileset.GroupName;
            entry.UserId = fileset.Uid;
            entry.UserName = fileset.UserName;
            entry.TarHeader.Mode = fileset.DirMode;

            // write directory to tar file
            archive.WriteEntry(entry, false);

            // remember that directory entry was added
            _addedDirs[entryName] = null;
        }
Exemplo n.º 14
0
		void EntryCounter(TarArchive archive, TarEntry entry, string message)
		{
			entryCount++;
		}
Exemplo n.º 15
0
		public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor)
		{
			TarArchive archive = new TarArchive();
			archive.tarOut = new TarOutputStream(outputStream, blockFactor);
			archive.Initialize(blockFactor * TarBuffer.BlockSize);
			return archive;
		}
Exemplo n.º 16
0
 void EntryCounter(TarArchive archive, TarEntry entry, string message)
 {
     entryCount++;
 }
Exemplo n.º 17
0
        private static void addDirectory(TarArchive tarArchive, string directory)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(directory);
            tarArchive.WriteEntry(tarEntry, false);

            string[] filenames = Directory.GetFiles(directory);
            foreach (string filename in filenames) {
                addFile(tarArchive, filename);
            }

            string[] directories = Directory.GetDirectories(directory);
            foreach (string dir in directories)
                addDirectory(tarArchive, dir);
        }
Exemplo n.º 18
0
 private static void addFile(TarArchive tarArchive, string filename)
 {
     var tarEntry = TarEntry.CreateEntryFromFile(filename);
     tarArchive.WriteEntry(tarEntry, true);
 }
Exemplo n.º 19
0
    /// <summary>
    /// Display progress information on console
    /// </summary>
    public void ShowTarProgressMessage(TarArchive archive, TarEntry entry, string message)
    {
        if (entry.TarHeader.TypeFlag != TarHeader.LF_NORMAL && entry.TarHeader.TypeFlag != TarHeader.LF_OLDNORM) {
            Console.WriteLine("Entry type " + (char)entry.TarHeader.TypeFlag + " found!");
        }

        if (message != null)
            Console.Write(entry.Name + " " + message);
        else {
            if (this.verbose) {
                string modeString = DecodeType(entry.TarHeader.TypeFlag, entry.Name.EndsWith("/")) + DecodeMode(entry.TarHeader.Mode);
                string userString = (entry.UserName == null || entry.UserName.Length == 0) ? entry.UserId.ToString() : entry.UserName;
                string groupString = (entry.GroupName == null || entry.GroupName.Length == 0) ? entry.GroupId.ToString() : entry.GroupName;

                Console.WriteLine(string.Format("{0} {1}/{2} {3,8} {4:yyyy-MM-dd HH:mm:ss} {5}", modeString, userString, groupString, entry.Size, entry.ModTime.ToLocalTime(), entry.Name));
            } else {
                Console.WriteLine(entry.Name);
            }
        }
    }
Exemplo n.º 20
-1
        private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {

            // Optionally, write an entry for the directory itself.
            // Specify false for recursion here if we will add the directory's files individually.
            //
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);
            tarArchive.WriteEntry(tarEntry, false);

            // Write each file to the tar.
            //
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
            }
        }