6

I am attempting to recursively parse through a zip folder and any zip folders inside it to collect files using the following code:

    private IList<IFile> _getFilesFromZip(ZipArchive zipArchive)
    {
        var returnFiles = new List<IFile>();
        foreach (var zippedFile in zipArchive.Entries)
        {
            var name = zippedFile.Name;
            var type = Path.GetExtension(zippedFile.Name).Replace(".", "");

            if (type == "zip")
            {
                var innerZipArchive = new ZipArchive(zippedFile.Open(), ZipArchiveMode.Read);
                returnFiles.AddRange(_getConversionFilesFromZip(zipArchive));
            }
            else
            { ...

var innerZipArchive = new ZipArchive(zippedFile.Open(), ZipArchiveMode.Read);

Throws a StackOverflow exception no matter how small the files.

How can I create a zipArchive from the ZipArchiveEntry of the first ZipArchive?

jth41
  • 3,808
  • 9
  • 59
  • 109
  • 2
    did you mean tot pass `innerZipArchive` to the innermost `_getConversionFilesFromZip` ? – Marc Gravell Jul 12 '18 at 20:23
  • @MarcGravell yes. however I never make it there due to the exception – jth41 Jul 12 '18 at 20:26
  • 1
    and can we reasonably assume that the zip itself is not malicious? it isn't a [zip bomb](https://en.wikipedia.org/wiki/Zip_bomb), etc? – Marc Gravell Jul 12 '18 at 20:30
  • `top 0.01% overall` ain't going to contribute to make a zip bomb! – Ivan García Topete Jul 12 '18 at 20:36
  • Using the corrected code you have posted here (and assuming that `_getConversionFilesFromZip` is basically doing the same thing as `_getFilesFromZip`), the code works for me. I can load up a full list of files within a zip file including zipped files which are inside of another zip file. Can you post the rest of the code including the function `_getConversionFilesFromZip`? Have you executed the code after the correction to pass `innerZipArchive` to the inner function call? – pstrjds Jul 12 '18 at 20:39
  • @MarcGravell turns out I'm just silly. I didn't realize that the exception was being thrown on the first recursion rather than the first pass. changing to innerZipArchive fixed. Thanks Rubber Ducks! – jth41 Jul 13 '18 at 15:34
  • 1
    @jth41 thanks for getting back to me :) also... ***does happy dance at nailing it*** – Marc Gravell Jul 13 '18 at 19:51

0 Answers0