?
Solved

Using GZipStream to explore a zip file

Posted on 2012-08-26
8
Medium Priority
?
662 Views
Last Modified: 2012-08-27
Hello, I am writing a VB.NET program, and I'd like to just do a quick scan of a ZIP file to make sure it contains a small file called info.txt (and maybe look at the contents, not completely necessary).  I'm not sure if I have to decompress it to do this -- I'd rather not because the entire ZIP file is pretty big.

I'm not sure if it is possible to inspect w/o extracting, OR to just extract a single file info.txt, w/o doing the very slow (since lots of big files) decompression.

Anyway, since it's VB.NET the

Imports System.IO.Compression

namespace is available to me, with the GZipStream class.  The example code for this class talks about compression and decompression, but nothing more advanced like what I'm talking about (if possible).  If it is possible, sample code would be appreciated.  

Thanks VERY much.
0
Comment
Question by:RonMexico
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:teebon
ID: 38335254
Hi Ron,

You may use DotNetZip library to achieve what you wanted.

http://dotnetzip.codeplex.com/
http://dotnetzip.codeplex.com/wikipage?title=VB-Examples&referringTitle=Examples

Refer to the second link above for the VB examples where you can loop through the files in the zip file and check the name:

  For Each e In zip1               
          'Check the file name for e here, if it is info.txt
          if(e.FileName = "info.txt")
             'Do something
      Next  

Open in new window

0
 

Author Comment

by:RonMexico
ID: 38335410
Thanks teebon... I'll take a look.  I came across that library in google hits and also read that it probably pre-dated the addition of the System.IO.Compression namespace to .NET.  I would prefer to use built-in namespaces if possible.  Do you happen to know whether this can't be done with the built-in component?

Also this might be greedy, but does that library allow you to look at the contents of a file w/o decompressing the whole thing?

Thanks.
0
 
LVL 9

Accepted Solution

by:
teebon earned 2000 total points
ID: 38335666
Hi Ron,

Yes, you may use the built in ZipArchive class in System.IO.Compression if you are using .NET 4.5

http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx

The following example only extract info.txt that you required without extracting every files in the zip:

Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\example\start.zip" 
        Dim extractPath As String = "c:\example\extract" 

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                If entry.FullName.EndsWith("info.txt", StringComparison.OrdinalIgnoreCase) Then
                 'Only extract info.txt, and exit the loop
                  entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
                  Exit For 
                End If 
            Next 
        End Using 
    End Sub 

End Module

Open in new window

0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:RonMexico
ID: 38337147
Awesome, thanks!   Was unable to find anything close to that helpful in sample code.  Did you find that online or write it.  Thanks either way...
0
 

Author Comment

by:RonMexico
ID: 38337154
Oh... I see the link now.  Don't know how I missed that.  Thanks again!
0
 

Author Comment

by:RonMexico
ID: 38337310
Ohhhhh.... THAT's how I missed it... it's not released yet.  :)
0
 

Author Comment

by:RonMexico
ID: 38339105
I downloaded VS2012, started a .NET 4.5 VB  application, does not seem like ZipARchive is part of IO.Compression.  What gives?
0
 
LVL 9

Expert Comment

by:teebon
ID: 38339364
I modified the msdn code based on your requirement.

Have you add reference to System.IO.Compression?
Are you still facing any issue?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question