Results 1 to 10 of 10

Thread: [RESOLVED] Zip archives

  1. #1
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Resolved [RESOLVED] Zip archives

    Here is my code:

    Code:
    Public Function extractZipArchive() As Boolean
        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(".txt", StringComparison.OrdinalIgnoreCase) Then
                    entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
                End If
            Next
        End Using
    End Function
    I am importing the following:

    Code:
    Imports System.IO
    Imports System.IO.Compression
    I am getting the following error:

    Type 'ZipArchive' is not defined.
    How can I fix this error?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: Zip archives

    ZIP support in System.IO.Compression is new in .NET 4.5. If you're not using VS 2012 and targeting .NET 4.5 then that support is not available to you. ZIP support has existed since .NET 3.0 in the System.IO.Packaging namespace but it works differently. Other than that, you can use a third-party component, e.g. DotNetZip and SharpZipLib are both free.

  3. #3
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Re: Zip archives

    I am using both VS 2012 and .NET 4.5

    Why am I getting the 'Type 'ZipArchive' is not defined' error?

    More specifically... how can I get my code working?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,538

    Re: Zip archives

    Isn't 'ZipFile' the type?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: Zip archives

    Quote Originally Posted by dunfiddlin View Post
    Isn't 'ZipFile' the type?
    No it isn't. You'd be thinking about one or more third-party components that provide ZIP support.

    Have you referenced the appropriate assembly? Presumably not. Some members of the System.IO.Compression namespace are defined in the System.dll assembly, so you can import that namespace without any issue. The ZipArchive class is not defined in that assembly though. You can open the MSDN Library and read the documentation for the class, which you should have already done. As for all types, that documentation will tell you what namespace the type is a member of and what assembly it's defined in. Hopefully doing it for yourself this time will help you remember to do it for yourself next time without being prompted.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,538

    Re: Zip archives

    Quote Originally Posted by jmcilhinney View Post
    No it isn't. You'd be thinking about one or more third-party components that provide ZIP support.
    Er no, I'd be thinking about http://visualstudiomagazine.com/arti...VBzipfig1.ashx

    and the accompanying article

    By comparison, the code to compress a folder and its contents with the ZIPFile class in .NET Framework 4.5 Beta is a single line using static methods of the ZIPFile class:

    IO.Compression.ZIPFile.CreateFromDirectory(
    FolderPath, ZipFullFilename, Optimization, includeBaseDirectory:=False)

  7. #7
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Re: Zip archives

    which you should have already done
    I have done this.

    The page says:

    Namespace: System.IO.Compression
    Assembly: System.IO.Compression (in System.IO.Compression.dll)
    I have imported the following:

    Imports System.IO.Compression
    What else do I need to do... isn't that all that needs to be done?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: Zip archives

    Quote Originally Posted by dunfiddlin View Post
    Er no, I'd be thinking about http://visualstudiomagazine.com/arti...VBzipfig1.ashx

    and the accompanying article
    Ah OK, didn't know about that one. Actually, the OP is using both. ZipFile is a static class, much like a module, so it has just Shared members. Some of those members create ZipArchive objects.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: Zip archives

    Quote Originally Posted by Simon Canning View Post
    I have done this.

    The page says:



    I have imported the following:



    What else do I need to do... isn't that all that needs to be done?
    You could start by reading what I posted previously.
    Have you referenced the appropriate assembly? ... that documentation will tell you ... what assembly it's defined in.
    If you don't know the difference between an assembly and a namespace then I suggest that you follow the Blog link in my signature and check out my post on Assemblies & Namespaces.

  10. #10
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Re: Zip archives

    @jmcilhinney: Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •