Exemplo n.º 1
0
        private void ExtractArchive(ArcArchive archive)
        {
            using var progressBar = CreateProgressBar();
            progressBar.Title     = "Extracting...";

            long totalLength = 0;

            foreach (var entry in archive.GetEntries())
            {
                totalLength += entry.Length;
            }
            progressBar?.SetMaxValue(totalLength);


            foreach (var entry in archive.GetEntries())
            {
                progressBar?.SetMessage(entry.Name);

                // Assuming what validated entry name is safe to use in path
                // (with combining).
                EntryNameUtilities.Validate(entry.Name);

                var outputPath = Path.Combine(OutputPath, entry.Name);

                var directoryPath = Path.GetDirectoryName(outputPath);
                Directory.CreateDirectory(directoryPath);

                using var inputStream = entry.Open();

                DateTimeOffset?lastWriteTime = SetLastWriteTime ? entry.LastWriteTime : (DateTimeOffset?)null;
                WriteFile(outputPath, inputStream, lastWriteTime, progressBar);
            }
        }
Exemplo n.º 2
0
        public int Run()
        {
            Check.True(InputPaths.Count > 0);

            var archiveOptions = new ArcArchiveOptions
            {
                // Mode
                Format           = Format,
                CompressionLevel = CompressionLevel,
                SafeWrite        = SafeWrite,
                HeaderAreaLength = HeaderAreaSize,
                ChunkLength      = ChunkSize,
                // TODO: UseLibDeflate = setup from global option
            };

            var archiveExist = File.Exists(ArchivePath);

            archiveOptions.Mode = archiveExist ? ArcArchiveMode.Update : ArcArchiveMode.Create;

            long totalInputLength = 0;
            var  inputFileInfos   = new List <InputFileInfo>();

            {
                using var progressBar = CreateProgressBar();
                progressBar.Title     = "Gathering input files...";

                var inputs = GetAllInputs();
                foreach (var inputFile in inputs)
                {
                    progressBar.Message = inputFile;

                    var fileInfo = new FileInfo(inputFile);

                    totalInputLength += fileInfo.Length;

                    var info = new InputFileInfo
                    {
                        FileName      = inputFile,
                        EntryName     = CreateEntryName(inputFile),
                        LastWriteTime = new DateTimeOffset(fileInfo.LastWriteTimeUtc),
                    };
                    inputFileInfos.Add(info);
                }

                if (!PreserveCase)
                {
                    var hashSet = new HashSet <string>(StringComparer.Ordinal);
                    foreach (var x in inputFileInfos)
                    {
                        if (!hashSet.Add(x.EntryName))
                        {
                            throw Error.InvalidOperation("Invalid casing. Final entry names must be unique.");
                        }
                    }
                }

                // Validate Entry Names
                foreach (var x in inputFileInfos)
                {
                    EntryNameUtilities.Validate(x.EntryName);
                }
            }

            {
                using var progressBar = CreateProgressBar();
                progressBar.Title     = GetProcessInputFilesTitle();

                using var archive = ArcArchive.Open(ArchivePath, archiveOptions);
                progressBar.SetMaxValue(totalInputLength);

                foreach (var inputFileInfo in inputFileInfos)
                {
                    progressBar.Message = inputFileInfo.FileName;

                    ProcessInputFile(archive, inputFileInfo, progressBar);
                }

                PostProcessArchive(archive, progressBar);
            }

            return(0);
        }