• I removed the www from the URL, please update your bookmarks and pay attention to the bottom of the screen if using Google Chrome to allow push notifications again.

Question View zipped image directly

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
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?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,062
Location
Sydney, Australia
Programming Experience
10+
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.
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
Programming Experience
3-5
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?

Code:
            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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,062
Location
Sydney, Australia
Programming Experience
10+
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.
 

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
481
Programming Experience
3-5
I think CreateEntryFromFile is what i need to use, but after i typed archive. there is no CreateEntryFromFile. It is some zipFileExtensions class

Code:
            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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,062
Location
Sydney, Australia
Programming Experience
10+
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.
 
Top Bottom