I’m using SharpCompress and trying to read files which are archived in a 7z archive.
Now I got stuck at a point where my 7z archive contains another zip or 7z, which contains the file I want to read. Is there a way how the read the content of a file, which is archived multiple times without extracting the archives on disk?
Just for test purpose, I have my simple method which should write me all the entries from the archive.
namespace TestArchiv
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
frmReference = this;
try
{
processArchive(@"C:temptemp.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("rn" + text);
}
}
}
When I try to run this, I’m getting
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:Usersmartin.tomansourcereposTestArchivTestArchivMainWindow.xaml.cs:line 56
at TestArchiv.MainWindow..ctor() in C:Usersmartin.tomansourcereposTestArchivTestArchivMainWindow.xaml.cs:line 38
Just to clarify, I’m processing C:tempCaCCdm30_2.7z which contains another CaCCdm30.7z
I was able to solve this problem for Zip files using the ZipFile class from C#, but I need to make it work for other archive types, so I went for SharpCompress.
This worked for zip files:
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
.....
}
}
EDIT: I added the complete code
Martin Toman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5