spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
 
Home arrow Source Codes arrow .NET arrow Zip/Unzip files from your VB.NET application

Zip/Unzip files from your VB.NET application PDF Print E-mail
Source Codes - .NET
Written by Thomas Kaloyani   
ImageIn order to add zip/unzip functionality to your programs you don't have to use third party components and libraries. .NET Framework has all you need via J#. Below you will find a translation of source code to Vb.NET from C#, found at codeproject.com posted by Valeri.

'To unzip Call

Try
Ziplib.Uncompress("C:\Testing.zip", "C:\")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

'To Zip Call
Dim s(1) As String
Try
s(0) = "C:\ZipTest\test.txt"
s(1) = "C:\ZipTest\test.doc"

Ziplib.Compress(s, "C:\Testing.zip")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Imports java.util.zip
Imports java.io

Public Class Ziplib

    Public Shared Function Uncompress(ByVal strZipFileName As String, ByVal strLocation As String) As Boolean
        Try
            Dim oFileInputStream As New java.io.FileInputStream(strZipFileName)
            Dim oZipInputStream As New java.util.zip.ZipInputStream(oFileInputStream)
            Dim bTrue As Boolean = True
            Dim sbBuf(1024) As SByte

            While 1 = 1
                Dim oZipEntry As ZipEntry = oZipInputStream.getNextEntry()

                If oZipEntry Is Nothing Then Exit While

                If oZipEntry.isDirectory Then
                    If Not My.Computer.FileSystem.DirectoryExists(strLocation & oZipEntry.getName) Then
                        My.Computer.FileSystem.CreateDirectory(strLocation & oZipEntry.getName)
                    End If
                Else


                    Dim oFileOutputStream As New java.io.FileOutputStream(strLocation.Replace("\", "/") & oZipEntry.getName())

                    While 1 = 1
                        Dim iLen As Integer = oZipInputStream.read(sbBuf)
                        If iLen < 0 Then Exit While
                        oFileOutputStream.write(sbBuf, 0, iLen)
                    End While
                    oFileOutputStream.close()
                End If
            End While
            oZipInputStream.close()
            oFileInputStream.close()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

Public Shared Function Compress(ByVal strFileNames() As String, ByVal strZipFileName As String) As Boolean
        Try
            Compress = False
            Dim oFileOutputStream As New java.io.FileOutputStream(strZipFileName)
            Dim oZipOutputStream As New java.util.zip.ZipOutputStream(oFileOutputStream)

            For Each strFileName As String In strFileNames
                Dim oFileInputStream As New java.io.FileInputStream(strFileName)
                Dim oZipEntry As New java.util.zip.ZipEntry(strFileName.Substring(3).Replace("\", "/"))

                oZipOutputStream.putNextEntry(oZipEntry)
                Dim sbBuf(1024) As SByte
                While 1 = 1
                    Dim iLen As Integer = oFileInputStream.read(sbBuf)
                    If iLen < 0 Then Exit While
                    oZipOutputStream.write(sbBuf, 0, iLen)
                End While
                oZipOutputStream.closeEntry()
                oFileInputStream.close()
            Next

            oZipOutputStream.close()
            oFileOutputStream.close()

            Return True

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
End Class

Original post can be found at The Code Project


Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website www.Kaloyani.com or through VBprofs.com - the newest Visual Basic and VB.NET resources portal.
Next >

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer