How to extract files from zip file

Hello

Can I extract files from a zip file I have

Thanks



Answer this question

How to extract files from zip file

  • Pierre Leclerc

    CAB - no thats different again. The GZip stream will only extract/compress GZip files AFAIK, CAB files are different (Cabinet files) which you need some other tool to do

  • Marc DESJARDINS

    Spotty,

    Your VB pointer program is in some older version of basic,

    and (if I scan read the specs correctly,) only goes in one direction,

    extract, not compact.

    There is a guy named Elmue who has some DLLs that will let you

    go in both directions (I believe he is German, but his English is excellent.)

    His DLL programs (for CAB files) are at http://www.codeproject.com/cs/files/CABCompressExtract.asp

    His web page (with lots of other downloads) is at http://www.netcult.ch/elmue/ElmueSoft-en.htm

    The CAB Dll is in several versions C++ and DotNet; and he send me a version

    compiled in Visual Studio 8.

    When my test program tryed to use it, it could 'see' the DLL because I had some

    intellisense that was from the DLL; However, the thing errored for 'file not found',

    which I am sure was not about my cab file but about a DLL dependancy.

    (The DLL uses the system cabinet.dll.)

    Elmue, tryed to help me a little, but gave up when he found out I was at the level

    of not understanding stack traces and other advanced debugging techniques.

    (Also Elmue is a C dude, not a VB dude from what I can tell.)

    Anyway, I sure wish you or NoBugz could help me out with this offline.

    I really need to be able to work with cab files, but the project is not urgent.

    Thanks



  • Gooseman1977

    OK, can I extract files from CAP file
  • DPMevents

    I want to extract from CAB files.

    Can I do that in VB2005

    Thanks


  • Steve Huckett

    you cant really I believe. it may well require some 3rd party tool or "shelling" out to expand cab files

  • Ariel S

    Check this thread...


  • R.Tutus

  • Boris Zakharin

     

    The is a Microsoft example that uses the java libraries to handle the zip files.  http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx

    I have been trying to convert the code to VB. I keep get an invalid cast exception on the following line.  Any help would be greatly appreceated.

    'Reset to first object in collection.  Here is where I get the invalid cast exception

    m_Wrapped = m_Method

    change m_Method to m_Method.invoke and this code will work.

    Here is the main form

    Imports System

    Imports System.Collections

    'Imports java.util

    Imports java.util.zip

    'Imports CsZip

    Imports System.Collections.Specialized

    Imports System.IO

    Imports System.ComponentModel

    Imports System.Reflection

    Public Class Form1

    Private m_CurrentFile As java.util.zip.ZipFile

    Public Property CurrentFile() As ZipFile

    Get

    Return m_CurrentFile

    End Get

    Set(ByVal value As ZipFile)

    Try

    m_CurrentFile.close()

    Catch ex As Exception

    End Try

    m_CurrentFile = value

    If m_CurrentFile.ToString <> "" Then

    DisplayEntries()

    Text = m_CurrentFile.getName

    End If

    End Set

    End Property

    Private Sub DisplayEntries()

    zipListView.BeginUpdate()

    zipImageList.Images.Clear()

    zipListView.Items.Clear()

    Dim imgs As New ListDictionary

    Dim entry As ZipEntry

    For Each entry In New CsZip.EnumerationAdapter(New CsZip.EnumerationMethod(AddressOf CurrentFile.entries))

    If Not entry.isDirectory Then

    'Try

    Dim name As String = entry.getName

    Dim item As New ListViewItem(Path.GetFileName(name))

    item.SubItems.Add(entry.getSize.ToString)

    item.SubItems.Add(entry.getCompressedSize.ToString)

    item.SubItems.Add(Path.GetDirectoryName(name))

    item.Tag = entry

    Dim ext As String = Path.GetExtension(name)

    If imgs(ext) = Nothing Then

    Dim ic As Icon = IconFromFileType(name)

    If ic.ToString <> "" Then

    zipImageList.Images.Add(ic)

    imgs(ext) = zipImageList.Images.Count - 1

    End If

    If imgs(ext).ToString <> "" Then

    item.ImageIndex = imgs(ext)

    End If

    zipListView.Items.Add(item)

    End If

    'Catch ex As Exception

    'End Try

    End If

    Next

    zipListView.EndUpdate()

    End Sub

    The class that does the zip file handleing

    Imports System

    Imports System.Collections

    Imports java.util

    Imports java.util.zip

    Imports System.Collections.Specialized

    Namespace frmZip

    Public Delegate Function EnumerationMethod() As Enumeration

    Public Class EnumerationAdapter

    Implements IEnumerable

    Private m_Method As EnumerationMethod

    Public Class EnumerationWrapper

    Implements IEnumerator

    Private m_Method As EnumerationMethod

    Private m_Wrapped As Enumeration

    Private m_Current As Object

    Public Sub New(ByRef method As EnumerationMethod)

    m_Method = method

    End Sub

    Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current

    Get

    Return m_Current

    End Get

    End Property

    Public Sub Reset() Implements System.Collections.IEnumerator.Reset

    ' Reset to first object in collection.

    If m_Method Is Nothing Then Throw New InvalidOperationException

    Try

    'Reset to first object in collection.  Here is where I get the invalid cast exception

    m_Wrapped = m_Method

    If m_Wrapped Is Nothing Then Throw New InvalidOperationException

    Catch ex As Exception

    MessageBox.Show(ex.Message.ToString)

    End Try

     

    End Sub

    Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext

    If m_Wrapped Is Nothing Then Reset()

    Dim Result As Boolean = m_Wrapped.hasMoreElements

    If Result Then m_Current = m_Wrapped.nextElement

    Return Result

    End Function

    End Class

    Public Sub New(ByRef method As EnumerationMethod)

    If method Is Nothing Then Throw New ArgumentException

    m_Method = method

    End Sub

    Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator

    Return New EnumerationWrapper(m_Method)

    End Function

    End Class

    Public Delegate Function FilterEntryMethod(ByRef e As ZipEntry) As Boolean

    Public Class ZipUtils

    Public Shared Sub CopyStream(ByVal from As java.io.InputStream, ByVal [to] As java.io.OutputStream)

    Dim buffer(8192) As SByte

    buffer(8192) = New SByte

    Dim got As Integer

    While (got = from.read(buffer, 0, buffer.Length)) > 0

    [to].write(buffer, 0, got)

    End While

    End Sub

    Public Shared Sub ExtractZipFile(ByVal file As ZipFile, ByVal path As String, ByVal filter As FilterEntryMethod)

    Dim entry As ZipEntry

    For Each entry In New EnumerationAdapter(New EnumerationMethod(AddressOf file.entries))

    If Not entry.isDirectory() Then

    If filter Is Nothing Or filter(entry) Then

    Dim s As java.io.InputStream = file.getInputStream(entry)

    Try

    Dim fname As String = System.IO.Path.GetFileName(entry.getName())

    Dim newpath As String = System.IO.Path.Combine(path, System.IO.Path.GetDirectoryName(entry.getName()))

    System.IO.Directory.CreateDirectory(newpath)

    Dim dest As New java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname))

    Try

    CopyStream(s, dest)

    Finally

    dest.close()

    End Try

    Finally

    s.close()

    End Try

    End If

    End If

    Next entry

    End Sub

    Public Shared Function CreateEmptyZipFile(ByRef fileName As String) As ZipFile

    Dim zosSPDownload As ZipOutputStream = New ZipOutputStream(New java.io.FileOutputStream(fileName))

    zosSPDownload.close()

    Return New ZipFile(fileName)

    End Function

    Public Shared Function UpdateZipFile(ByRef file As ZipFile, ByRef filter As FilterEntryMethod, ByRef newFiles() As String) As ZipFile

    Dim prev As String = file.getName()

    Dim tmp As String = System.IO.Path.GetTempFileName()

    Dim [to] As New ZipOutputStream(New java.io.FileOutputStream(tmp))

    Try

    CopyEntries(file, [to], filter)

    ' add entries here

    If Not (newFiles Is Nothing) Then

    Dim f As String

    For Each f In newFiles

    Dim z As New ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length))

    z.setMethod(ZipEntry.DEFLATED)

    [to].putNextEntry(z)

    Try

    Dim s As New java.io.FileInputStream(f)

    Try

    CopyStream(s, [to])

    Finally

    s.close()

    End Try

    Finally

    [to].closeEntry()

    End Try

    Next f

    End If

    Finally

    [to].close()

    End Try

    file.close()

    ' now replace the old file with the new one

    System.IO.File.Copy(tmp, prev, True)

    System.IO.File.Delete(tmp)

    Return New ZipFile(prev)

    End Function

    Public Overloads Shared Sub CopyEntries(ByRef from As ZipFile, ByRef [to] As ZipOutputStream)

    CopyEntries(from, [to], Nothing)

    End Sub

    Public Overloads Shared Sub CopyEntries(ByRef from As ZipFile, ByRef [to] As ZipOutputStream, ByRef filter As FilterEntryMethod)

    Dim entry As ZipEntry

    Try

    For Each entry In New EnumerationAdapter(New EnumerationMethod(AddressOf from.entries))

    If filter Is Nothing Or filter(entry) Then

    Dim s As java.io.InputStream = from.getInputStream(entry)

    Try

    [to].putNextEntry(entry)

    Try

    CopyStream(s, [to])

    Finally

    [to].closeEntry()

    End Try

    Finally

    s.close()

    End Try

    End If

    Next entry

    Catch ex As Exception

    MessageBox.Show(ex.Message.ToString)

    End Try

    End Sub

    End Class

     

    End Namespace


  • Aaron Silverwatch

    not entirely. you may need to use 3rd party zip software like pkzip to execute it in a process and give it the right commands to extract the files or maybe some Win32 P/Invoke if the OS is Windows ME or higher as it has zip built in. The .NET Framework 2.0 has a Compression namespace but that is for GZip which is a different zip format I believe

  • Mirricle

    The following may provide some clues to extracting cab files.

    http://www.thescarms.com/vbasic/CabExplorer.asp

    3rd party components are a great option which gives you the functionality in a very easy to use form and there are numerous components

    http://www.programurl.com/software/compression-vb-net.htm

    Some are free and support cab files - so these might be something you would want to explore.


  • GoDaddy

    OK

    How can I do it from gzip file


  • How to extract files from zip file