You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SharpCompress读取嵌套压缩包时遇NotSupportedException问题求助

如何用SharpCompress读取多层嵌套压缩包内的文件(无需提取到磁盘)

我正在使用SharpCompress读取7z压缩包中的文件,目前遇到一个问题:我的7z压缩包中包含另一个zip或7z压缩包,目标文件就在这个嵌套压缩包内。请问是否有办法无需将压缩包提取到磁盘,就能读取多层嵌套压缩包内的文件?

为了测试,我编写了一个简单方法,用于输出压缩包中的所有条目:

namespace TestArchiv
{
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
            frmReference = this;
            try
            {
                processArchive(@"C:\\temp\\temp.zip");
            }
            catch (Exception ex)
            {
                WriteToBox(ex.ToString());
            }
        }

        public void processArchive(string archivePath)
        {
            using (var archive = SevenZipArchive.Open(archivePath))
            {
                foreach (var entry in archive.Entries)
                {

                    if (entry.ToString().Contains("7z"))
                    {
                        using (SevenZipArchive inner2 = SevenZipArchive.Open(entry.OpenEntryStream()))
                        {

                            foreach (var entry2 in inner2.Entries)
                            {
                                WriteToBox(entry2.ToString());
                            }
                        }
                    }
                    WriteToBox(entry.ToString());
                    
                }
            }
        }

        public void WriteToBox(string text)
        {

            //frmReference.TextBox1.Clear();
            frmReference.TextBox1.AppendText("\\r\\n" + text);

        }
    }
}

运行这段代码时,我收到如下错误:

System.NotSupportedException: Specified method is not supported.
   at SharpCompress.IO.ReadOnlySubStream.get_Length()
   at SharpCompress.IO.SourceStream.Seek(Int64 offset, SeekOrigin origin)
   at SharpCompress.IO.SourceStream.set_Position(Int64 value)
   at SharpCompress.IO.NonDisposingStream.set_Position(Int64 value)
   at SharpCompress.Archives.SevenZip.SevenZipArchive.LoadFactory(Stream stream)
   at SharpCompress.Archives.SevenZip.SevenZipArchive.LoadEntries(IEnumerable`1 volumes)
   at SharpCompress.Archives.AbstractArchive`2..ctor(ArchiveType type, SourceStream sourceStream)
   at SharpCompress.Archives.SevenZip.SevenZipArchive..ctor(SourceStream sourceStream)
   at SharpCompress.Archives.SevenZip.SevenZipArchive.Open(Stream stream, ReaderOptions readerOptions)
   at TestArchiv.MainWindow.processArchive(String archivePath) in C:\\Users\\martin.toman\\source\\repos\\TestArchiv\\TestArchiv\\MainWindow.xaml.cs:line 56
   at TestArchiv.MainWindow..ctor() in C:\\Users\\martin.toman\\source\\repos\\TestArchiv\\TestArchiv\\MainWindow.xaml.cs:line 38

需要说明的是,我实际处理的是C:\\temp\\CaCCdm30_2.7z,该压缩包内包含另一个CaCCdm30.7z。

我曾使用C#的ZipFile类解决了嵌套Zip文件的问题,代码如下:

public void processArchive(ZipArchive archive)
 {
     foreach (ZipArchiveEntry entry in archive.Entries)
     {
         string fileName = entry.ToString();
         //WriteToBox(fileName);
         if (fileName.ToLower().Contains(".zip"))
         {
             ZipArchive innerArchive = new ZipArchive(entry.Open());
             processArchive(innerArchive);
         }
         else 
         .....
      }
}

内容的提问来源于stack exchange,提问作者Martin Toman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:37:14