I have a zip file which contains inner zip file (Ex:ZipFile1.zip->ZipFile2.zip->file.txt). I want to read the data of inner archive file content (file.txt) using ICSharpCode.SharpZipLib library without extracting to disk. Is it possible? If it is possible, Let me know how to get this.
Asked
Active
Viewed 866 times
0
-
Does this answer your question? [Unzip files programmatically in .net](https://stackoverflow.com/questions/836736/unzip-files-programmatically-in-net) – Sinatr Apr 22 '21 at 08:27
-
1Perhaps this answer can help you with this: https://stackoverflow.com/a/328353/3181933 – ProgrammingLlama Apr 22 '21 at 08:29
-
1You're already extracting the files. Just because you don't write them to disk doesn't mean you aren't extracting them to read them. Open a stream to the entry you want and read from it using SharpZibLib – Panagiotis Kanavos Apr 22 '21 at 08:29
-
@Llama. Thanks for your update. I already tried this but it is not working to read nested archive file content. – MOHANRAJ G Apr 22 '21 at 08:35
-
It works fine for reading the nested archive, assuming you accept that the nested archive is just another file from the containing archive's perspective. I've added an answer demonstrating that it works. – ProgrammingLlama Apr 22 '21 at 08:43
1 Answers
0
Based on this answer, you can open a file within the zip as a Stream
. You can also open a ZipFile
from a Stream
. I'm sure you can see where this is heading.
using (var zip = new ZipFile("ZipFile1.zip"))
{
var nestedZipEntry = zip.GetEntry("ZipFile2.zip");
using (var nestedZipStream = zip.GetInputStream(nestedZipEntry))
using (var nestedZip = new ZipFile(nestedZipStream))
{
var fileEntry = nestedZip.GetEntry("file.txt");
using (var fileStream = nestedZip.GetInputStream(fileEntry))
using (var reader = new StreamReader(fileStream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
What we're doing here:
- Open ZipFile1.zip
- Find the entry for ZipFile2.zip
- Open ZipFile2.zip as a
Stream
- Create a new
ZipFile
object aroundnestedZipStream
. - Find the entry for file.txt
- Create a
StreamReader
aroundfileStream
to read the text file. - Read the contents of file.txt and output it to the console.
Try it online - in this sample, the base64 data is the binary data of a zip file which contains "test.zip", which in turn contains "file.txt". The contents of that text file is "hello".
P.S. If an entry isn't found then GetEntry
will return null. You'll want to check for that in any code you write. It works here because I'm sure that these entries exist in their respective archives.

ProgrammingLlama
- 36,677
- 7
- 67
- 86
-
Thanks for your immediate response. I resolved my problem. Problem: When i try this same approach i am getting following error 'Stream is not seekable Parameter name: nestedZipStream'. Due to nestedZipStream.CanSeek value as false. Solution: Copy the nestedZipStream to MemoryStream than i can create a ZipFile for nestedZipStream and read the zipfile entries. – MOHANRAJ G Apr 22 '21 at 09:18
-
Ah, yes. You can check `.CanSeek` on the source stream to see if it's seekable or not, and only copy when it isn't. – ProgrammingLlama Apr 22 '21 at 09:20