Resolved View zipped image directly

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
Is there a way to read image files inside a zip file without extracting it? Lets say i have 10 jpg in a zip file, can i load a specific image into a picturebox without first extracting it?
 
It depends what you mean by "extract". A ZIP file contains bytes, like any other file. Those bytes are a transformed representation of other files; in your case, image files. Anything that knows how to display an image is not going to understand those transformed bytes, so you have no choice but to transform them back, i.e. decompress the data.

If you mean "decompress" when you say "extract then no, there is no way to display that image without extracting it from the ZIP file. If you mean save all the original files to disk then yes, you can display the image without extracting.

In order to display an image in a PictureBox in Windows Forms, you need an Image object. As you're using .NET 4.5, you have access to the ZIP functionality in the System.IO.Compression namespace. You can create a FileStream from your ZIP file and then create a ZipArchive from that. You can then get a ZipArchiveEntry for the appropriate entry and call its Open method to get a Stream. Finally, you can call Image.FromStream to get your Image object.
 
Thanks that worked great. I have another question. How do i add an image file to an already existing zip file? is it something simular to this?

VB.NET:
            Using archive As IO.Compression.ZipArchive = IO.Compression.ZipFile.Open(zipFullFilename, IO.Compression.ZipArchiveMode.Update)
                Dim zipEntry As IO.Compression.ZipArchiveEntry = archive.CreateEntry("abc.jpg")
                Using writer As IO.StreamWriter = New IO.StreamWriter(zipEntry.Open())

                    ' Add an existing image file c:\xyz.jpg to the zipFullFilename



                End Using
            End Using
 
I've never done it myself so I don't know the details but I can tell you that you wouldn't use a StreamWriter for writing an Image because it's for text. Presumably that ZipEntry.Open call returns a Stream and the Image.Save method allows you to pass a Stream to write the saved Image to.
 
I think CreateEntryFromFile is what i need to use, but after i typed archive. there is no CreateEntryFromFile. It is some zipFileExtensions class

VB.NET:
            Using archive As IO.Compression.ZipArchive = IO.Compression.ZipFile.Open(zipFullFilename, IO.Compression.ZipArchiveMode.Update)
 [COLOR=#000000][FONT=Consolas]                archive.CreateEntryFromFile("[/FONT][/COLOR][COLOR=#000000]c:\xyz.jpg"[/COLOR][COLOR=#000000][FONT=Consolas], [/FONT][/COLOR][COLOR=#A31515][FONT=Consolas]"[/FONT][/COLOR][COLOR=#000000]xyz.jpg[/COLOR][COLOR=#A31515][FONT=Consolas]"[/FONT][/COLOR][COLOR=#000000][FONT=Consolas], [/FONT][/COLOR]IO.Compression.[COLOR=#000000][FONT=Consolas]CompressionLevel.Fastest)[/FONT][/COLOR]
            End Using
 
If the class is named ZipFileExtensions then that presumably contains extension methods on the ZipFile class, so you would need to call CreateEntryFromFile on a ZipFile rather then a ZipArchive.
 
I think CreateEntryFromFile is what i need to use, but after i typed archive. there is no CreateEntryFromFile. It is some zipFileExtensions class

VB.NET:
            Using archive As IO.Compression.ZipArchive = IO.Compression.ZipFile.Open(zipFullFilename, IO.Compression.ZipArchiveMode.Update)
 [COLOR=#000000][FONT=Consolas]                archive.CreateEntryFromFile("[/FONT]c:\xyz.jpg"[FONT=Consolas], [/FONT][/COLOR][COLOR=#A31515][FONT=Consolas]"[/FONT][/COLOR][COLOR=#000000]xyz.jpg[/COLOR][COLOR=#A31515][FONT=Consolas]"[/FONT][/COLOR][COLOR=#000000][FONT=Consolas], [/FONT][/COLOR]IO.Compression.[COLOR=#000000][FONT=Consolas]CompressionLevel.Fastest)[/FONT][/COLOR]
            End Using

Hi Zexor,

I'm new here and found the site when I was searching for a way to read individual images from a Zip file in MS Access.

Did you resolve a method for extracting .jpg from .zip? If so, I'd like to take a look and see if I can implement that in VB for MS Access.

I want to be able to get the nth image from a .zip.

Any help you can provide would be greatly appreciated.

Cheers

Nigel
 
@Nigel1500, pull the data back from your database as a Byte array. You need all the data for the whole ZIP file. Load that data into a MemoryStream and then create a ZipArchive from that stream. You can then index the Entries property to get a ZipArchiveEntry. Open that to get a Stream and then use that however you like, e.g. pass it to Image.FromStream to get an Image.

Note that I have provided step-by-step instructions here. It is my experience that beginners tend to try to attack a problem as a whole, rather than breaking it down and attacking the steps one by one. We can help further if required but make sure that you are taking it step by step, e.g. don't ask how to create a MemoryStream if you haven't already got a Byte array from your database. Getting the data from the database is the same as it would be for any data, so image files in ZIP files is irrelevant to that. Likewise, creating a MemoryStream is the same no matter where the data comes from or is going to. This is how you simply things such that you can find existing help on the web. If you try to find information on a process that involves multiple steps then you're less likely to be successful, because someone else would need to have done all the exact same steps. If you look for help on one step at a time, you're far more likely to find information.
 
Back
Top