Sharpcompress multi rar提取进度

时间:2018-03-26 17:49:28

标签: c# sharpcompress

我正在构建一个从tar和rar档案中提取的应用程序。我可以根据目标中包含的rar数量报告目标进度,并提取每个进度。在rars中有一个文件跨越多个卷。我已经使用了单元测试示例中的代码

 var streams = testArchives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)).Select(File.OpenRead).ToList();
 using (var reader = RarReader.Open(streams))
 {
     while (reader.MoveToNextEntry())
    {
         reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
         {
           ExtractFullPath = true,
           Overwrite = true
         });
    }
 }

问题是在当前条目被提取之前,流程不会报告。

1 个答案:

答案 0 :(得分:-1)

不要使用 WriteEntryToDirectory 保存文件,因为它不包含回调进度,请改用 FileStream ,然后获取文件的完整大小未压缩的文件并切片保存进度好吗?

这是一个简单的例子:

thread = new Thread(
            new ThreadStart(() =>
            {
                using (Archive = RarArchive.Open(streams, new ReaderOptions() { Password = password, LookForHeader = true }))
                {
                    Archive.EntryExtractionBegin += EntryExtractionBeginEvet;
                    Archive.CompressedBytesRead += CompressedBytesReadEvent;
                    FilesTotalCount = Archive.Entries.Count();
                    TotalSize = Archive.TotalSize;
                    foreach (IArchiveEntry ArchiveEntry in Archive.Entries.Where(entry => !entry.IsDirectory))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(@"" + path + "\\" + ArchiveEntry.Key));
                        using (Stream archiveStream = ArchiveEntry.OpenEntryStream())
                        using (FileStream fileStream = new FileStream(@"" + path + "\\" + ArchiveEntry.Key, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            int byteSizes = 0;
                            byte[] buffer = new byte[bufferLenght];
                            while (ThreadState == ThreadState.Running && (byteSizes = archiveStream.Read(buffer, 0, buffer.Length)) > 0)
                                fileStream.Write(buffer, 0, byteSizes);
                        }
                    }
                }
                IO.CloseStreams(streams);
            }
        ));
        thread.Start();