コード例 #1
0
        /// <summary>
        /// Compress exported data
        /// </summary>
        /// <returns>Process results model</returns>
        public Models.Common.ProcessResultModel Compress()
        {
            var stopwatch = new System.Diagnostics.Stopwatch();

            if (this.Config.Format == "d" && this.Config.TarDir)
            {
                stopwatch.Start();

                // Compress
                using (var archive = TarArchive.Create())
                {
                    var mode = new SharpCompress.Writers.WriterOptions(SharpCompress.Common.CompressionType.None);
                    archive.AddAllFromDirectory(this.File);
                    archive.SaveTo(this.File + ".tar", mode);
                }

                // Delete
                Directory.Delete(this.File, true);
                this.File += ".tar";

                stopwatch.Stop();
            }

            return(new Models.Common.ProcessResultModel()
            {
                Duartion = stopwatch.Elapsed
            });
        }
コード例 #2
0
        public static IWritableArchive Create(ArchiveType type)
        {
            switch (type)
            {
            case ArchiveType.Zip:
            {
                return(ZipArchive.Create());
            }

            case ArchiveType.Tar:
            {
                return(TarArchive.Create());
            }

            case ArchiveType.GZip:
            {
                return(GZipArchive.Create());
            }

            default:
            {
                throw new NotSupportedException("Cannot create Archives of type: " + type);
            }
            }
        }
コード例 #3
0
 public static IArchive Create(ArchiveType type)
 {
     if (type != ArchiveType.Tar)
     {
         throw new NotSupportedException("Cannot create Archives of type: " + type);
     }
     return(TarArchive.Create());
 }
コード例 #4
0
 public static IWritableArchive Create(ArchiveType type)
 {
     return(type switch
     {
         ArchiveType.Zip => ZipArchive.Create(),
         ArchiveType.Tar => TarArchive.Create(),
         ArchiveType.GZip => GZipArchive.Create(),
         _ => throw new NotSupportedException("Cannot create Archives of type: " + type)
     });
コード例 #5
0
        public void Tar_Create_New()
        {
            string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Tar.tar");
            string unmodified  = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar");

            using (var archive = TarArchive.Create())
            {
                archive.AddAllFromDirectory(ORIGINAL_FILES_PATH);
                archive.SaveTo(scratchPath, CompressionType.None);
            }
            CompareArchivesByPath(unmodified, scratchPath);
        }
コード例 #6
0
        private static string CreateTarballFromDirectory(string direcotryPath)
        {
            var outputFile = Path.GetTempFileName();

            using (var archive = TarArchive.Create())
            {
                archive.AddAllFromDirectory(direcotryPath);
                archive.SaveTo(outputFile, new WriterOptions(CompressionType.GZip));
            }

            return(outputFile);
        }
コード例 #7
0
        string Repackage(string stagingDirectory, string fileName)
        {
            var supportedZipExtensions      = new[] { ".nupkg", ".zip" };
            var supportedJavaExtensions     = new[] { ".jar", ".war", ".ear", ".rar" };
            var supportedTarExtensions      = new[] { ".tar" };
            var supportedTarGZipExtensions  = new[] { ".tgz", ".tar.gz", ".tar.Z" };
            var supportedTarBZip2Extensions = new[] { "tar.bz", ".tar.bz2", ".tbz" };

            var lowercasedFileName = fileName.ToLower();

            using (var targetArchive = fileSystem.CreateTemporaryFile(string.Empty, out var targetArchivePath))
            {
                if (supportedZipExtensions.Any(lowercasedFileName.EndsWith) || supportedJavaExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = ZipArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.Deflate);
                    }
                }
                else if (supportedTarExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.None);
                    }
                }
                else if (supportedTarGZipExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.GZip);
                    }
                }
                else if (supportedTarBZip2Extensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.BZip2);
                    }
                }
                else
                {
                    throw new ArgumentException($"Unable to compress file {fileName.ToLower()}, the extension is unsupported.");
                }

                return(targetArchivePath);
            }
        }
コード例 #8
0
        /// <summary>
        /// Compress directory with Tar to file-system
        /// </summary>
        /// <param name="path">Path </param>
        /// <returns>Path to archive</returns>
        public static string Compress(string path)
        {
            if (Generics.StringExtensions.IsEmpty(path))
            {
                return(string.Empty);
            }

            using (var archive = TarArchive.Create())
            {
                var gzip = new SharpCompress.Writers.WriterOptions(SharpCompress.Common.CompressionType.GZip);
                path = path.Trim().TrimEnd('.');
                archive.AddAllFromDirectory(path);
                archive.SaveTo(path + ".tar.gz", gzip);
                return(path + ".tar.gz");
            }
        }
コード例 #9
0
        public void Tar_Create_New()
        {
            string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Tar.tar");
            string unmodified  = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar");

            // var aropt = new Ar

            using (var archive = TarArchive.Create())
            {
                archive.AddAllFromDirectory(ORIGINAL_FILES_PATH);
                var twopt = new TarWriterOptions(CompressionType.None, true);
                twopt.ArchiveEncoding = new ArchiveEncoding()
                {
                    Default = Encoding.GetEncoding(866)
                };
                archive.SaveTo(scratchPath, twopt);
            }
            CompareArchivesByPath(unmodified, scratchPath);
        }
コード例 #10
0
        public void TestMethod1()
        {
            var temp = Path.Combine(ThisLocation, Path.GetRandomFileName());

            CopyFiles(TheItems, temp);

            var newTarball = Path.Combine(ThisLocation, "MyTarBall.tgz");

            using (var archive = TarArchive.Create())
            {
                archive.AddAllFromDirectory(temp);
                archive.SaveTo(newTarball, CompressionType.GZip);
            }

            //An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code
            //Additional information: The process cannot access the file 'New Text Document.txt' because it is being
            //used by another process.
            Directory.Delete(temp, true);
        }
コード例 #11
0
        public IWritableArchive GetWritableArchive()
        {
            switch (Type)
            {
            case ArchiveType.Zip:
                return(ZipArchive.Create());

            case ArchiveType.Tar:
                return(TarArchive.Create());

            case ArchiveType.GZip:
                return(GZipArchive.Create());

            case ArchiveType.Rar:
            case ArchiveType.SevenZip:
                throw new NotImplementedException("Writing operation for Rar & SevenZip is not support yet. ");

            default:
                throw new ArgumentOutOfRangeException(nameof(Type), Type, null);
            }
        }
コード例 #12
0
        /// <summary>
        /// Creates a compressed file from this directory.
        /// </summary>
        /// <param name="destination">Path of the target zip file to generate.</param>
        public static void Compress(this DirectoryInfo @this, FileInfo destination,
                                    CompressionFormat format = CompressionFormat.Zip, bool overwrite = false)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (destination.Exists() && !overwrite)
            {
                throw new Exception("Destination file already exists: " + destination.FullName);
            }

            IWritableArchive archive;
            var type = CompressionType.Deflate;

            switch (format)
            {
            case CompressionFormat.Zip:
                archive = ZipArchive.Create();
                break;

            case CompressionFormat.Gzip:
                archive = GZipArchive.Create();
                break;

            case CompressionFormat.Tar:
                archive = TarArchive.Create();
                type    = CompressionType.None;
                break;

            default: throw new NotSupportedException();
            }

            using (archive)
            {
                archive.AddAllFromDirectory(@this.FullName);
                archive.SaveTo(destination.FullName, type);
            }
        }
コード例 #13
0
        /// <summary>
        /// Write a set of input files to a tape archive (assuming the same output archive name)
        /// </summary>
        /// <param name="inputFiles">Input files to be moved</param>
        /// <param name="outDir">Output directory to build to</param>
        /// <param name="rom">DatItem representing the new information</param>
        /// <returns>True if the archive was written properly, false otherwise</returns>
        public override bool Write(List <string> inputFiles, string outDir, List <Rom> roms)
        {
            bool   success  = false;
            string tempFile = Path.Combine(outDir, $"tmp{Guid.NewGuid()}");

            // If either list of roms is null or empty, return
            if (inputFiles == null || roms == null || inputFiles.Count == 0 || roms.Count == 0)
            {
                return(success);
            }

            // If the number of inputs is less than the number of available roms, return
            if (inputFiles.Count < roms.Count)
            {
                return(success);
            }

            // If one of the files doesn't exist, return
            foreach (string file in inputFiles)
            {
                if (!File.Exists(file))
                {
                    return(success);
                }
            }

            // Get the output archive name from the first rebuild rom
            string archiveFileName = Path.Combine(outDir, Sanitizer.RemovePathUnsafeCharacters(roms[0].Machine.Name) + (roms[0].Machine.Name.EndsWith(".tar") ? string.Empty : ".tar"));

            // Set internal variables
            TarArchive oldTarFile = TarArchive.Create();
            TarArchive tarFile    = TarArchive.Create();

            try
            {
                // If the full output path doesn't exist, create it
                if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
                }

                // If the archive doesn't exist, create it and put the single file
                if (!File.Exists(archiveFileName))
                {
                    // Map all inputs to index
                    Dictionary <string, int> inputIndexMap = new Dictionary <string, int>();
                    for (int i = 0; i < inputFiles.Count; i++)
                    {
                        inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i);
                    }

                    // Sort the keys in TZIP order
                    List <string> keys = inputIndexMap.Keys.ToList();
                    keys.Sort(ZipFile.TrrntZipStringCompare);

                    // Now add all of the files in order
                    foreach (string key in keys)
                    {
                        // Get the index mapped to the key
                        int index = inputIndexMap[key];

                        // Get temporary date-time if possible
                        DateTime?usableDate = null;
                        if (UseDates && !string.IsNullOrWhiteSpace(roms[index].Date) && DateTime.TryParse(roms[index].Date.Replace('\\', '/'), out DateTime dt))
                        {
                            usableDate = dt;
                        }

                        // Copy the input stream to the output
                        tarFile.AddEntry(roms[index].Name, FileExtensions.TryOpenRead(inputFiles[index]), size: roms[index].Size ?? 0, modified: usableDate);
                    }
                }

                // Otherwise, sort the input files and write out in the correct order
                else
                {
                    // Open the old archive for reading
                    oldTarFile = TarArchive.Open(archiveFileName);

                    // Get a list of all current entries
                    List <string> entries = oldTarFile.Entries.Select(i => i.Key).ToList();

                    // Map all inputs to index
                    Dictionary <string, int> inputIndexMap = new Dictionary <string, int>();
                    for (int i = 0; i < inputFiles.Count; i++)
                    {
                        // If the old one contains the new file, then just skip out
                        if (entries.Contains(roms[i].Name.Replace('\\', '/')))
                        {
                            continue;
                        }

                        inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1));
                    }

                    // Then add all of the old entries to it too
                    for (int i = 0; i < entries.Count; i++)
                    {
                        inputIndexMap.Add(entries[i], i);
                    }

                    // If the number of entries is the same as the old archive, skip out
                    if (inputIndexMap.Keys.Count <= entries.Count)
                    {
                        success = true;
                        return(success);
                    }

                    // Get the order for the entries with the new file
                    List <string> keys = inputIndexMap.Keys.ToList();
                    keys.Sort(ZipFile.TrrntZipStringCompare);

                    // Copy over all files to the new archive
                    foreach (string key in keys)
                    {
                        // Get the index mapped to the key
                        int index = inputIndexMap[key];

                        // If we have the input file, add it now
                        if (index < 0)
                        {
                            // Get temporary date-time if possible
                            DateTime?usableDate = null;
                            if (UseDates && !string.IsNullOrWhiteSpace(roms[-index - 1].Date) && DateTime.TryParse(roms[-index - 1].Date.Replace('\\', '/'), out DateTime dt))
                            {
                                usableDate = dt;
                            }

                            // Copy the input file to the output
                            tarFile.AddEntry(roms[-index - 1].Name, FileExtensions.TryOpenRead(inputFiles[-index - 1]), size: roms[-index - 1].Size ?? 0, modified: usableDate);
                        }

                        // Otherwise, copy the file from the old archive
                        else
                        {
                            // Get the stream from the original archive
                            TarArchiveEntry tae   = oldTarFile.Entries.ElementAt(index);
                            MemoryStream    entry = new MemoryStream();
                            tae.OpenEntryStream().CopyTo(entry);

                            // Copy the input stream to the output
                            tarFile.AddEntry(key, entry, size: tae.Size, modified: tae.LastModifiedTime);
                        }
                    }
                }

                // Close the output tar file
                tarFile.SaveTo(tempFile, new WriterOptions(CompressionType.None));

                success = true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                success = false;
            }
            finally
            {
                tarFile.Dispose();
                oldTarFile.Dispose();
            }

            // If the old file exists, delete it and replace
            if (File.Exists(archiveFileName))
            {
                FileExtensions.TryDelete(archiveFileName);
            }
            File.Move(tempFile, archiveFileName);

            return(true);
        }
コード例 #14
0
        /// <summary>
        /// Write an input stream to a tape archive
        /// </summary>
        /// <param name="inputStream">Input stream to be moved</param>
        /// <param name="outDir">Output directory to build to</param>
        /// <param name="rom">DatItem representing the new information</param>
        /// <param name="date">True if the date from the DAT should be used if available, false otherwise (default)</param>
        /// <param name="romba">True if files should be output in Romba depot folders, false otherwise</param>
        /// <returns>True if the archive was written properly, false otherwise</returns>
        public override bool Write(Stream inputStream, string outDir, Rom rom, bool date = false, bool romba = false)
        {
            bool   success  = false;
            string tempFile = Path.Combine(outDir, "tmp" + Guid.NewGuid().ToString());

            // If either input is null or empty, return
            if (inputStream == null || rom == null || rom.Name == null)
            {
                return(success);
            }

            // If the stream is not readable, return
            if (!inputStream.CanRead)
            {
                return(success);
            }

            // Get the output archive name from the first rebuild rom
            string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(rom.MachineName) + (rom.MachineName.EndsWith(".tar") ? "" : ".tar"));

            // Set internal variables
            TarArchive oldTarFile = TarArchive.Create();
            TarArchive tarFile    = TarArchive.Create();

            try
            {
                // If the full output path doesn't exist, create it
                if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
                }

                // If the archive doesn't exist, create it and put the single file
                if (!File.Exists(archiveFileName))
                {
                    // Get temporary date-time if possible
                    DateTime?usableDate = null;
                    if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt))
                    {
                        usableDate = dt;
                    }

                    // Copy the input stream to the output
                    inputStream.Seek(0, SeekOrigin.Begin);
                    tarFile.AddEntry(rom.Name, inputStream, size: rom.Size, modified: usableDate);
                }

                // Otherwise, sort the input files and write out in the correct order
                else
                {
                    // Open the old archive for reading
                    oldTarFile = TarArchive.Open(archiveFileName);

                    // Get a list of all current entries
                    List <string> entries = oldTarFile.Entries.Select(i => i.Key).ToList();

                    // Map all inputs to index
                    Dictionary <string, int> inputIndexMap = new Dictionary <string, int>();

                    // If the old one doesn't contain the new file, then add it
                    if (!entries.Contains(rom.Name.Replace('\\', '/')))
                    {
                        inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1);
                    }

                    // Then add all of the old entries to it too
                    for (int i = 0; i < entries.Count; i++)
                    {
                        inputIndexMap.Add(entries[i], i);
                    }

                    // If the number of entries is the same as the old archive, skip out
                    if (inputIndexMap.Keys.Count <= entries.Count)
                    {
                        success = true;
                        return(success);
                    }

                    // Get the order for the entries with the new file
                    List <string> keys = inputIndexMap.Keys.ToList();
                    keys.Sort(ZipFile.TorrentZipStringCompare);

                    // Copy over all files to the new archive
                    foreach (string key in keys)
                    {
                        // Get the index mapped to the key
                        int index = inputIndexMap[key];

                        // Get temporary date-time if possible
                        DateTime?usableDate = null;
                        if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out DateTime dt))
                        {
                            usableDate = dt;
                        }

                        // If we have the input file, add it now
                        if (index < 0)
                        {
                            // Copy the input file to the output
                            inputStream.Seek(0, SeekOrigin.Begin);
                            tarFile.AddEntry(rom.Name, inputStream, size: rom.Size, modified: usableDate);
                        }

                        // Otherwise, copy the file from the old archive
                        else
                        {
                            // Get the stream from the original archive
                            TarArchiveEntry tae   = oldTarFile.Entries.ElementAt(index);
                            MemoryStream    entry = new MemoryStream();
                            tae.OpenEntryStream().CopyTo(entry);

                            // Copy the input stream to the output
                            tarFile.AddEntry(key, entry, size: tae.Size, modified: tae.LastModifiedTime);
                        }
                    }
                }

                // Close the output tar file
                tarFile.SaveTo(tempFile, new WriterOptions(CompressionType.None));

                success = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                success = false;
            }
            finally
            {
                inputStream.Dispose();
                tarFile.Dispose();
                oldTarFile.Dispose();
            }

            // If the old file exists, delete it and replace
            if (File.Exists(archiveFileName))
            {
                Utilities.TryDeleteFile(archiveFileName);
            }
            File.Move(tempFile, archiveFileName);

            return(success);
        }