Example #1
0
        private static 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);

            // if (rootDirectory != null && rootDirectory != "/")
            //     tarEntry.Name = tarEntry.Name.Replace(rootDirectory, ".");
            tarArchive.WriteEntry(tarEntry, false);

            // Write each file to the tar.
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                if (filename.EndsWith(".gz") || filename.EndsWith(".tar"))
                {
                    continue;
                }
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                // if (rootDirectory != null && rootDirectory != "/")
                //     tarEntry.Name = tarEntry.Name.Replace(rootDirectory, ".");
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    addDirectoryFilesToTar(tarArchive, directory, recurse);
                }
            }
        }
Example #2
0
        // Source: https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples#-create-a-tgz-targz
        private static void CreateTarRecursive(TarArchive tarArchive, DirectoryInfo directory, string relativePath, List <Regex> ignoredPathsRegex)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(directory.FullName);

            tarArchive.WriteEntry(tarEntry, false);

            FileInfo[] files = directory.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                string fileRelativePath = relativePath + files[i].Name;
                if (!ignoredPathsRegex.PathMatchesPattern(fileRelativePath))
                {
                    tarEntry = TarEntry.CreateEntryFromFile(files[i].FullName);
                    tarArchive.WriteEntry(tarEntry, true);
                }
            }

            DirectoryInfo[] subDirectories = directory.GetDirectories();
            for (int i = 0; i < subDirectories.Length; i++)
            {
                string directoryRelativePath = relativePath + subDirectories[i].Name + Path.DirectorySeparatorChar;
                if (!ignoredPathsRegex.PathMatchesPattern(directoryRelativePath))
                {
                    CreateTarRecursive(tarArchive, subDirectories[i], directoryRelativePath, ignoredPathsRegex);
                }
            }
        }
        private static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse, string directoryName)
        {
            // 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);

            tarEntry.Name = directoryName;
            tarArchive.WriteEntry(tarEntry, false);

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

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    string dirname = new DirectoryInfo(directory).Name;
                    if (dirname == ".git")
                    {
                        continue;
                    }
                    string newDirectory = directoryName + dirname + "/";
                    AddDirectoryFilesToTar(tarArchive, directory, recurse, newDirectory);
                }
            }
        }
        static 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)
            {
                if (Path.GetExtension(filename) == ".exe" || Path.GetExtension(filename) == ".dll")
                {
                    tarEntry = TarEntry.CreateEntryFromFile(filename);
                    tarArchive.WriteEntry(tarEntry, true);
                }
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
                }
            }
        }
Example #5
0
    public static 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.
        //
        Console.WriteLine("AddDirectoryFilestoTar");

        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)
        {
            FileInfo fi = new FileInfo(filename);
            if (fi.LastWriteTime < DateTime.Now.AddDays(-60))
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
            }
            tarArchive.WriteEntry(tarEntry, true);
            Console.WriteLine("Writing to tar file: " + filename);
        }

        if (recurse)
        {
            string[] directories = Directory.GetDirectories(sourceDirectory);
            foreach (string directory in directories)
            {
                AddDirectoryFilesToTar(tarArchive, directory, recurse);
            }
        }
    }
Example #6
0
        private static void AddDirectoryFiles(this 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.
            //
            var tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);

            tarArchive.WriteEntry(tarEntry, false);

            // Write each file to the tar.
            //
            var filenames = Directory.GetFiles(sourceDirectory);

            foreach (string filename in filenames)
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                var directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    tarArchive.AddDirectoryFiles(directory, recurse);
                }
            }
        }
Example #7
0
        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);
                if (tarEntry.TarHeader.TypeFlag == 76)
                {
                    Console.WriteLine("IDK...");
                }
                else
                {
                    tarArchive.WriteEntry(tarEntry, true);
                }
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
                }
            }
        }
Example #8
0
        private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse, bool isRoot)
        {
            // Optionally, write an entry for the directory itself.
            // Specify false for recursion here if we will add the directory's files individually.
            //
            TarEntry tarEntry;

            if (!isRoot)
            {
                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);
                Console.WriteLine(tarEntry.Name);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar(tarArchive, directory, recurse, false);
                }
            }
        }
        /// <inheritdoc />
        /// <exception cref="FileNotFoundException">when file/folder does not exist</exception>
        public async Task TransferTo(TarArchive tarArchive, string destinationPath, CancellationToken ct = default)
        {
            if (tarArchive == null)
            {
                throw new ArgumentNullException(nameof(tarArchive));
            }

            if (destinationPath == null)
            {
                throw new ArgumentNullException(nameof(destinationPath));
            }

            if (ct.IsCancellationRequested)
            {
                return;
            }

            // tar is a linux concept, so all paths should be in linux paths
            destinationPath = OS.NormalizePath(destinationPath, OS.LinuxDirectorySeparator);

            if (Directory.Exists(_path))
            {
                await Task.Run(() =>
                {
                    var canonicalPath = Path.GetFullPath(_path);
                    var tarEntry      = TarEntry.CreateEntryFromFile(canonicalPath);

                    // this is needed because SharpZipLib has a hack to remove current directory even
                    // if the path is an absolute path
                    var rootPath = canonicalPath;
                    if (canonicalPath.IndexOf(Directory.GetCurrentDirectory(), StringComparison.Ordinal) == 0)
                    {
                        rootPath = rootPath.Substring(Directory.GetCurrentDirectory().Length);
                    }

                    // there is an issue in SharpZipLib that trims the starting / from the
                    // tar entry, thus, any root path that starts with / will not match
                    tarArchive.RootPath   = rootPath.TrimStart(Path.DirectorySeparatorChar);
                    tarArchive.PathPrefix = destinationPath;

                    tarArchive.WriteEntry(tarEntry, true);

                    tarArchive.RootPath   = "";
                    tarArchive.PathPrefix = null;
                }, ct)
                .ConfigureAwait(false);
            }
            else
            {
                await Task.Run(() =>
                {
                    var canonicalPath = Path.GetFullPath(_path);
                    var tarEntry      = TarEntry.CreateEntryFromFile(canonicalPath);
                    tarEntry.Name     = destinationPath;
                    tarArchive.WriteEntry(tarEntry, true);
                }, ct)
                .ConfigureAwait(false);
            }
        }
Example #10
0
        private static void AddDirectoryFilesToTar2(TarArchive tarArchive, string sourceDirectory, bool recurse, string rootDirectory)
        {
            // Recursively add sub-folders
            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar2(tarArchive, directory, recurse, rootDirectory);
                }
            }

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

                if (tarEntry.Name.StartsWith(rootDirectory))
                {
                    tarEntry.Name = tarEntry.Name.Substring(rootDirectory.Length + 1);
                }


                tarArchive.WriteEntry(tarEntry, true);
            }
        }
Example #11
0
        public override void CreateTarFromLocalData(string outputPath)
        {
            lock (_pathsPreparedForRemote)
            {
                UpdatePathsPreparedForRemote();

                if (_pathsPreparedForRemote.Count == 0)
                {
                    throw new Exception("No data available.");
                }

                using (FileStream outputFile = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
                {
                    using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(outputFile))
                    {
                        foreach (string pathPreparedForRemote in _pathsPreparedForRemote)
                        {
                            using (FileStream filePreparedForRemote = File.OpenRead(pathPreparedForRemote))
                            {
                                TarEntry tarEntry = TarEntry.CreateEntryFromFile(pathPreparedForRemote);
                                tarEntry.Name = "data/" + Path.GetFileName(pathPreparedForRemote);
                                tarArchive.WriteEntry(tarEntry, false);
                                filePreparedForRemote.Close();
                            }
                        }

                        tarArchive.Close();
                    }

                    outputFile.Close();
                }
            }
        }
        /// <summary>
        /// 生成 ***.tar.gz 文件
        /// </summary>
        /// <param name="sourceFolderPath">待压缩的源文件夹或文件路径</param>
        /// <param name="tgzFileDir">生成的文件路径</param>
        /// <param name="tgzFileName">生成的文件名称,不带扩展名</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public bool CreateTarGzArchive(string sourceFolderPath, string tgzFileDir, string tgzFileName = "temp", string encoding = "utf-8")
        {
            if (!System.IO.Directory.Exists(tgzFileDir))
            {
                System.IO.Directory.CreateDirectory(tgzFileDir);
            }

            //string fileName;
            //if (File.Exists(sourceFolderPath))  // 如果是文件
            //{
            //    fileName = Path.GetFileNameWithoutExtension(sourceFolderPath);
            //}
            //else
            //{
            //    string tempStr = sourceFolderPath.Replace('\\', '/');
            //    Regex regex = new Regex("/(.+?)$", RegexOptions.RightToLeft);
            //    fileName = regex.Match(tempStr).Groups[1].Value;
            //}

            tgzFileName = Path.Combine(tgzFileDir, tgzFileName + ".tar.gz");

            using Stream outTmpStream = new FileStream(tgzFileName, FileMode.OpenOrCreate);

            //注意此处源文件大小大于4096KB
            using Stream outStream   = new GZipOutputStream(outTmpStream);
            using TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor, Encoding.GetEncoding(encoding));
            archive.RootPath         = Path.GetDirectoryName(sourceFolderPath);
            TarEntry entry = TarEntry.CreateEntryFromFile(sourceFolderPath);

            archive.WriteEntry(entry, true);

            return(true);
        }
Example #13
0
        private static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            TarEntry tarEntry;

            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarEntry.TarHeader.UserName  = string.Empty;
                tarEntry.TarHeader.GroupName = string.Empty;
                tarEntry.TarHeader.UserId    = 0;
                tarEntry.TarHeader.Mode      = 420;
                tarEntry.Name = filename.Remove(0, tarArchive.RootPath.Length + 1);
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                {
                    AddDirectoryFilesToTar(tarArchive, directory, true);
                }
            }
        }
Example #14
0
        private static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            var filenames = Directory.GetFiles(sourceDirectory);

            foreach (var filename in filenames)
            {
                var tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarEntry.TarHeader.UserName  = string.Empty;
                tarEntry.TarHeader.GroupName = string.Empty;
                tarEntry.TarHeader.UserId    = 0;
                tarEntry.TarHeader.Mode      = 33261;
                tarEntry.Name = filename.Remove(0, tarArchive.RootPath.Length + 1);
                tarEntry.Name = $"./{tarEntry.Name}";
                tarArchive.WriteEntry(tarEntry, true);
            }

            if (!recurse)
            {
                return;
            }

            var directories = Directory.GetDirectories(sourceDirectory);

            foreach (var directory in directories)
            {
                AddDirectoryFilesToTar(tarArchive, directory, true);
            }
        }
Example #15
0
        public static void Make_TAR(string What, string Where)
        {
            string        Out_TAR         = Path.GetFileNameWithoutExtension(Where) + ".tar";
            string        Out_MD5         = Path.GetFileNameWithoutExtension(Where) + ".tar.md5";
            Stream        fs              = new FileStream(Out_MD5, FileMode.Create);
            StringBuilder sb              = new StringBuilder();
            MD5           MD5_Hash_Engine = new MD5CryptoServiceProvider();

            Console.WriteLine("Creating: " + Out_MD5 + " .....");
            TarArchive TA = TarArchive.CreateOutputTarArchive(fs);
            TarEntry   TE = TarEntry.CreateEntryFromFile(What);

            TE.SetIds(0, 0);
            TE.SetNames(string.Empty, string.Empty);
            TE.TarHeader.Mode = 420;
            TA.WriteEntry(TE, true);
            TA.Close();
            fs.Close();
            Stream hash = new FileStream(Out_MD5, FileMode.Open);

            byte[] TAR_MD5_Hash = MD5_Hash_Engine.ComputeHash(hash);
            for (int i = 0; i < TAR_MD5_Hash.Length; i++)
            {
                sb.Append(TAR_MD5_Hash[i].ToString("X2"));
            }
            byte[] Final_Hash = Encoding.ASCII.GetBytes(sb.ToString().ToLower() + Convert.ToChar(0x20).ToString() + Convert.ToChar(0x20).ToString() + Out_TAR + Convert.ToChar("\n").ToString());
            Console.WriteLine("MD5 for " + Out_MD5 + " is " + sb.ToString().ToLower());
            hash.Write(Final_Hash, 0, Final_Hash.Length);
            hash.Close();
        }
Example #16
0
        /// <summary>
        /// 生成 ***.tar 文件
        /// </summary>
        /// <param name="strBasePath">文件基目录(源文件、生成文件所在目录)</param>
        /// <param name="strSourceFolderName">待压缩的源文件夹名</param>
        public bool CreatTarArchive(string strBasePath, string strSourceFolderName)
        {
            if (string.IsNullOrEmpty(strBasePath) ||
                string.IsNullOrEmpty(strSourceFolderName) ||
                !System.IO.Directory.Exists(strBasePath) ||
                !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))
            {
                return(false);
            }
            Environment.CurrentDirectory = strBasePath;                                              //要压缩的文件夹名称
            string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);
            string strOupFileAllPath      = Path.Combine(strBasePath, strSourceFolderName + ".tar"); //压缩文件名及路径

            Stream outStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);

            TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);
            TarEntry   entry   = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);

            archive.WriteEntry(entry, true);

            if (archive != null)
            {
                archive.Close();
            }

            outStream.Close();

            return(true);
        }
        static void FileBackup(string dbPath, string dbName)
        {
            var backup = Path.Combine(App.Current.DBDir, "backup");

            if (!Directory.Exists(backup))
            {
                Directory.CreateDirectory(backup);
            }

            string outputFilename = Path.Combine(backup, dbName + ".tar.gz");

            if (File.Exists(outputFilename))
            {
                File.Delete(outputFilename);
            }

            // storage backup in old format
            using (FileStream fs = new FileStream(outputFilename, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Stream gzipStream = new GZipOutputStream(fs)) {
                    using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream)) {
                        foreach (string n in new string [] { "", "-wal", "-shm" })
                        {
                            TarEntry tarEntry = TarEntry.CreateEntryFromFile(Path.Combine(dbPath, dbName + ".cblite" + n));
                            tarArchive.WriteEntry(tarEntry, true);
                        }
                        AddDirectoryFilesToTar(tarArchive, Path.Combine(dbPath, dbName + " attachments"), true);
                    }
                }
            }
        }
Example #18
0
        /// <summary>
        /// 打包成Tar包
        /// </summary>
        /// <param name="strBasePath">压缩文件夹路径</param>
        /// <param name="strSourceFolderName">生成tar文件路径</param>
        /// <param name="sTarName">生成tar文件名称</param>
        /// <returns></returns>
        public bool CreatTarArchive(string strBasePath, string strSourceFolderName, string sTarName)
        {
            if (!Directory.Exists(strSourceFolderName))
            {
                Directory.CreateDirectory(strSourceFolderName);//不存在生成Tar文件目录就创建
            }

            if (string.IsNullOrEmpty(strBasePath) ||
                string.IsNullOrEmpty(strSourceFolderName) ||
                !Directory.Exists(strBasePath))
            {
                return(false);
            }
            if (strBasePath.EndsWith("\\"))
            {
                strBasePath = strSourceFolderName.TrimEnd('\\');
            }
            Environment.CurrentDirectory = strBasePath;                                     //要压缩的文件夹名称
            string strSourceFolderAllPath = strBasePath;
            string strOupFileAllPath      = strSourceFolderName + "\\" + sTarName + ".tar"; //压缩文件名及路径

            Stream outStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);

            TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);
            TarEntry   entry   = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);

            archive.WriteEntry(entry, true);

            if (archive != null)
            {
                archive.Close();
            }
            outStream.Close();
            return(true);
        }
        private void CompressTGZ(string input, string archive, string output, CancellationToken token)
        {
            string sourceDirectory = Path.GetDirectoryName(input);



            string archive_name = archive.Replace(".tar.gz", "") + ".tar.gz";
            string outputFile   = Path.Combine(output, archive_name);

            using (Stream outStream = File.Create(outputFile))
                using (Stream gzoStream = new GZipOutputStream(outStream))
                    using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzoStream))
                    {
                        try
                        {
                            // Note that the RootPath is currently case sensitive and must be forward slashes e.g. "c:/temp"
                            // and must not end with a slash, otherwise cuts off first char of filename
                            // This is scheduled for fix in next release
                            tarArchive.RootPath = sourceDirectory.Replace('\\', '/');
                            if (tarArchive.RootPath.EndsWith("/"))
                            {
                                tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1);
                            }

                            TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);
                            tarArchive.WriteEntry(tarEntry, false);

                            // Write each file to the tar.
                            string[] files = Directory.GetFiles(sourceDirectory, Path.GetFileName(input), SearchOption.TopDirectoryOnly);
                            foreach (string file in files)
                            {
                                token.ThrowIfCancellationRequested();

                                tarEntry = TarEntry.CreateEntryFromFile(file);
                                tarArchive.WriteEntry(tarEntry, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

            FileInfo info = new FileInfo(outputFile);

            _logger.Information("Compressed to {File} ({Size} bytes)", info, info.Length.ToString());
        }
Example #20
0
        private void CreateEntry(string fileName, DirectoryInfo rootDirectory, TarArchive tarArchive, string fileNamePrefix,
                                 string filenameSuffix = null)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(fileName);

            tarEntry.Name = fileNamePrefix + RemoveRootDirectoryFromFilename(fileName, rootDirectory.FullName) + filenameSuffix;
            tarArchive.WriteEntry(tarEntry, false);
        }
Example #21
0
        private void TarCreation()
        {
            Stream outStream = File.Create(tarDest);

            TarArchive tar = TarArchive.CreateOutputTarArchive(outStream);

            System.IO.Directory.SetCurrentDirectory(System.IO.Path.GetTempPath());
            TarEntry xmlEntry = TarEntry.CreateEntryFromFile(miniDumpId + ".xml");

            tar.WriteEntry(xmlEntry, false);

            System.IO.Directory.SetCurrentDirectory(dumpDir);
            TarEntry dump = TarEntry.CreateEntryFromFile(miniDumpId + ".dmp");

            tar.WriteEntry(dump, false);
            tar.Close();
        }
Example #22
0
        private static void CreateEntry(TarArchive tarArchive, string file)
        {
            var entry = TarEntry.CreateEntryFromFile(file);

            entry.TarHeader.Mode = entry.IsDirectory ? _dirMode : _fileMode;

            tarArchive.WriteEntry(entry, false);
        }
Example #23
0
 static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory)
 {
     string[] filenames = Directory.GetFiles(sourceDirectory);
     foreach (string filename in filenames)
     {
         TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
         tarEntry.Name = Path.GetFileName(filename);
         tarArchive.WriteEntry(tarEntry, false);
     }
 }
Example #24
0
        // Source: https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples#-create-a-tgz-targz
        private static void CreateTarRecursive(TarArchive tarArchive, string inFolder)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(inFolder);

            tarArchive.WriteEntry(tarEntry, false);

            string[] files = Directory.GetFiles(inFolder);
            for (int i = 0; i < files.Length; i++)
            {
                tarEntry = TarEntry.CreateEntryFromFile(files[i]);
                tarArchive.WriteEntry(tarEntry, true);
            }

            string[] subDirectories = Directory.GetDirectories(inFolder);
            for (int i = 0; i < subDirectories.Length; i++)
            {
                CreateTarRecursive(tarArchive, subDirectories[i]);
            }
        }
Example #25
0
        private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            try
            {
                // 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);
                //removing archive.tar.gz from array

                foreach (string filename in filenames)
                {
                    if (filename.Contains("archive.tar.gz") || filename.Contains("Load_Effort_Optimisation.exe") || filename.Contains("ICSharpCode.SharpZipLib.dll") || filename.Contains("SnmpSharpNet.dll"))
                    {
                        //noaction
                    }
                    else
                    {
                        tarEntry = TarEntry.CreateEntryFromFile(filename);
                        tarArchive.WriteEntry(tarEntry, true);
                    }
                }

                if (recurse)
                {
                    string[] directories = Directory.GetDirectories(sourceDirectory);
                    foreach (string directory in directories)
                    {
                        AddDirectoryFilesToTar(tarArchive, directory, recurse);
                    }
                }
            }
            catch (SystemException se)
            {
                MessageBox.Show(se.Message);
            }
        }
Example #26
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);
                }
            }
        }
Example #27
0
        private static void AddDirectoryFilesToTar(TarArchive tarArchive, string rootDirectory, string filePath)
        {
            filePath = filePath.Replace("\\", "/");
            string absFilePath = filePath.Replace(rootDirectory, "").Remove(0, 1);

            // Debug.Log(absFilePath);
            string[] parentDirInfos = absFilePath.Split('/');
            string   parentDir      = "";
            string   sp             = "/";

            for (int i = 0; i < parentDirInfos.Length - 1; i++)
            {
                parentDir += sp + parentDirInfos[i];
                TarEntry tarEntryDic = TarEntry.CreateEntryFromFile(rootDirectory + parentDir);
                tarArchive.WriteEntry(tarEntryDic, false);
                // Debug.Log(rootDirectory + parentDir);
            }

            TarEntry tarEntry = TarEntry.CreateEntryFromFile(filePath);

            tarArchive.WriteEntry(tarEntry, true);
        }
Example #28
0
        private static void TarCompress(DirectoryInfo directory, TarArchive archive)
        {
            foreach (FileInfo fileToBeTarred in directory.GetFiles())
            {
                var entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
                entry.TarHeader.Name = entry.TarHeader.Name.Substring("Builds/".Length);
                archive.WriteEntry(entry, true);
            }

            foreach (var d in directory.GetDirectories())
            {
                TarCompress(d, archive);
            }
        }
Example #29
0
        static void CreateTarArchive(ProgressMonitor mon, string folder, Stream outStream)
        {
            TarArchive archive = null;

            try {
                archive = TarArchive.CreateOutputTarArchive(outStream);
                SetAsciiTranslate(archive);
                archive.RootPath              = folder;
                archive.ProgressMessageEvent += delegate(TarArchive ac, TarEntry e, string message) {
                    if (message != null)
                    {
                        mon.Log.WriteLine(message);
                    }
                };

                foreach (FilePath f in GetFilesRec(new DirectoryInfo(folder)))
                {
                    TarEntry entry = TarEntry.CreateEntryFromFile(f);
                    entry.Name = f.ToRelative(folder);
                    if (!Platform.IsWindows)
                    {
                        UnixFileInfo fi = new UnixFileInfo(f);
                        entry.TarHeader.Mode = (int)fi.Protection;
                    }
                    else
                    {
                        entry.Name = entry.Name.Replace('\\', '/');
                        FilePermissions p = FilePermissions.S_IFREG | FilePermissions.S_IROTH | FilePermissions.S_IRGRP | FilePermissions.S_IRUSR;
                        if (!new FileInfo(f).IsReadOnly)
                        {
                            p |= FilePermissions.S_IWUSR;
                        }
                        entry.TarHeader.Mode = (int)p;
                    }
                    archive.WriteEntry(entry, false);
                }

                // HACK: GNU tar expects to find a double zero record at the end of the archive. TarArchive only emits one.
                // This hack generates the second zero block.
                FieldInfo tarOutField = typeof(TarArchive).GetField("tarOut", BindingFlags.Instance | BindingFlags.NonPublic);
                if (tarOutField != null)
                {
                    TarOutputStream tarOut = (TarOutputStream)tarOutField.GetValue(archive);
                    tarOut.Finish();
                }
            } finally {
                CloseArchive(archive);
            }
        }
Example #30
0
 public static void AddDirectoryFilesToTar(TarArchive tarArchive, string path)
 {
     if (File.Exists(path))
     {
         TarEntry tarEntry = TarEntry.CreateEntryFromFile(path);
         tarArchive.WriteEntry(tarEntry, true);
     }
     else if (Directory.Exists(path))
     {
         Array.ForEach(Directory.GetFiles(path), file => AddDirectoryFilesToTar(tarArchive, file));
     }
     else
     {
     }
 }