Directories in a zip file #95

Closed
opened 2026-01-29 22:06:30 +00:00 by claunia · 2 comments
Owner

Originally created by @glcjr on GitHub (Apr 2, 2016).

Is it possible with the reader to process a zip file that has images and then subdirectories with more images?

What I'm trying to do is to read a zipfile and put the images in a List and I want to open any subdirectories in the zip file and add those images to my list as well.

I'm not sure how to open a directory within the zip file for this.

Basically I'm trying to create a little picture viewer program where I click next to go through the List and see the pictures display in a picturebox. Seemed simple but the subdirectories have stymied me.

This is what I've come up with so far. Just in case what I think will work is wrong...

        using (Stream stream = File.OpenRead(filepath))
        {
            var reader = ReaderFactory.Open(stream);
            while (reader.MoveToNextEntry())
            {
                if (reader.Entry.IsDirectory)
                {

                }
                else
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(reader.OpenEntryStream());
                    book.addImage(img);
                }
            }
        }

book is just an object of a class I created with the List declared inside

Originally created by @glcjr on GitHub (Apr 2, 2016). Is it possible with the reader to process a zip file that has images and then subdirectories with more images? What I'm trying to do is to read a zipfile and put the images in a List<Image> and I want to open any subdirectories in the zip file and add those images to my list as well. I'm not sure how to open a directory within the zip file for this. Basically I'm trying to create a little picture viewer program where I click next to go through the List and see the pictures display in a picturebox. Seemed simple but the subdirectories have stymied me. This is what I've come up with so far. Just in case what I think will work is wrong... ``` using (Stream stream = File.OpenRead(filepath)) { var reader = ReaderFactory.Open(stream); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) { } else { System.Drawing.Image img = System.Drawing.Image.FromStream(reader.OpenEntryStream()); book.addImage(img); } } } ``` book is just an object of a class I created with the List declared inside
Author
Owner

@adamhathcock commented on GitHub (Apr 3, 2016):

You're thinking about how Zip file entries work slightly wrong.

Entries in Zip (and everything else really) are just key/value pairs. The keys happen to have directory information (or are a marker to say "I am a directory" with no bytes) but there is no real directory. So foo\bar.zip is a complete key string. This just happens to mean that bar.zip is a file in the directory foo but it's up to the extractor code to handle it that way or any other way you chose.

@adamhathcock commented on GitHub (Apr 3, 2016): You're thinking about how Zip file entries work slightly wrong. Entries in Zip (and everything else really) are just key/value pairs. The keys happen to have directory information (or are a marker to say "I am a directory" with no bytes) but there is no real directory. So `foo\bar.zip` is a complete key string. This just happens to mean that `bar.zip` is a file in the directory `foo` but it's up to the extractor code to handle it that way or any other way you chose.
Author
Owner

@glcjr commented on GitHub (Apr 23, 2016):

Thank you! That is good information on zip files. I forgot to come back here and say that after applying it to my pictureviewer code. :)

@glcjr commented on GitHub (Apr 23, 2016): Thank you! That is good information on zip files. I forgot to come back here and say that after applying it to my pictureviewer code. :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#95