.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

zip and unzip file

Posted By: Ajesh Rawat     Posted Date: May 19, 2010    Points:2   Category :VB.Net
I want to zip and unzip the file
without any third partee..
can any one help me..

Responses
Author: Lalij Mer             
Posted Date: May 19, 2010     Points: 5   

Example to create a Zip file in VB.NET


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim zip As New Chilkat.Zip()

' Anything begins the 30-day trial
Dim unlocked As Boolean
unlocked = zip.UnlockComponent("30-day trial")
If (Not unlocked) Then
MsgBox(zip.LastErrorText)
Exit Sub
End If

zip.NewZip("test.zip")

' Append a directory tree. This simply adds disk file references
' to the Zip object. No Zip is created at this point.
Dim success As Boolean
success = zip.AppendFiles("tree", True)
If (Not success) Then
MsgBox(zip.LastErrorText)
Exit Sub
End If

' Write test.zip
success = zip.WriteZipAndClose()
If (Not success) Then
MsgBox(zip.LastErrorText)
Else
MsgBox("Zip created!")
End If

End Sub

Example to create a UnZip file in VB.NET
   Dim zip As New Chilkat.Zip()

' Anything begins the 30-day trial
Dim unlocked As Boolean
unlocked = zip.UnlockComponent("30-day trial")
If (Not unlocked) Then
MsgBox(zip.LastErrorText)
Exit Sub
End If

Dim success As Boolean
success = zip.OpenZip("test.zip")


' Unzip to a specific directory:
Dim count As Integer
count = zip.Unzip("c:/temp/myUnzipDir")
If (count = -1) Then
MsgBox(zip.LastErrorText)
Else
MsgBox("Unzipped " + Str(count) + " files and directories")
End If

' Alternatively:

' Unzip creating a new subdirectory named "tree"
' under the current working directory.
Dim count As Integer
count = zip.Unzip("tree")
If (count = -1) Then
MsgBox(zip.LastErrorText)
Else
MsgBox("Unzipped " + Str(count) + " files and directories")
End If

End Sub


Please Check my answer if you help....
Thank You...

Lalit.
Author: Lalij Mer             
Posted Date: May 19, 2010     Points: 5   

Try also this code sample



Imports System

Imports System.IO

Imports System.IO.Compression



Namespace appWin


Public Enum Action
Zip
UnZip
End Enum


Public Class GZip

Private _SourceFileName As String = ""


Private _DestinationFileName As String = ""


Private oBuffer() As Byte


Private oZipper As GZipStream


Public Sub New()
End Sub

Public Sub New(ByVal SourceFileName As String, ByVal DestinationFileName As String, ByVal action As Action)

oZipper = Nothing

Me.SourceFileName = SourceFileName

Me.DestinationFileName = DestinationFileName

If action = Action.Zip Then
Me.CompressFile()
End If

If action = Action.UnZip Then
Me.DecompressFile()
End If



End Sub


Public Property SourceFileName() As String
Get

Return _SourceFileName

End Get
Set (ByVal Value As String)

_SourceFileName = value

End Set
End Property

Public Property DestinationFileName() As String
Get

Return _DestinationFileName

End Get
Set (ByVal Value As String)

_DestinationFileName = value



End Set
End Property


Protected Sub CompressFile()



If File.Exists(SourceFileName) Then

Imports (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

Imports (oZipper = New void NewStream(outputFile, CompressionMode.Compress))

{

oBuffer = New Byte(inputFile.Length) {}

Dim counter As Integer = 0

While (counter = inputFile.Read(oBuffer,0,oBuffer.Length)) <> 0

oZipper.Write(oBuffer, 0, counter)

End While

}

oBuffer = Nothing

}

End If



End Sub


Protected Sub DecompressFile()



If File.Exists(SourceFileName) Then

Imports (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

Imports (oZipper = New void NewStream(inputFile, CompressionMode.Decompress))

{

oBuffer = New Byte(inputFile.Length) {}

Dim counter As Integer

While (counter = oZipper.Read(oBuffer,0,oBuffer.Length)) <> 0

outputFile.Write(oBuffer, 0, counter)

End While

}

oBuffer = Nothing

}

End If

MessageBox.Show("Decompression done")

End Sub

End Class


End Namespace

Now,in order to consume the Class service here you is the code

Dim oZip As GZip = New GZip("C:\Test.txt","C:\Test1.txt",Action.Zip)

oZip = New GZip("C:\Test1.txt", "C:\Test2.txt", Action.UnZip)

End Class

This is another Class but that uses DeflateStream Class
Public Enum Action
Zip
UnZip
End Enum


Public Class Deflat

Private _SourceFileName As String = ""
Private _DestinationFileName As String = ""

Private oBuffer() As Byte

Private oZipper As DeflateStream

Public Sub New()
End Sub

Public Sub New(ByVal SourceFileName As String, ByVal DestinationFileName As String, ByVal action As Action)

oZipper = Nothing

Me.SourceFileName = SourceFileName

Me.DestinationFileName = DestinationFileName

If action = Action.Zip Then
Me.CompressFile()
End If

If action = Action.UnZip Then
Me.DecompressFile()
End If

End Sub

Public Property SourceFileName() As String
Get

Return _SourceFileName

End Get
Set (ByVal Value As String)

_SourceFileName = value

End Set
End Property

Public Property DestinationFileName() As String
Get

Return _DestinationFileName

End Get
Set (ByVal Value As String)

_DestinationFileName = value



End Set
End Property

Protected Sub CompressFile()

If File.Exists(SourceFileName) Then

Imports (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

Imports (oZipper = New void NeweStream(outputFile, CompressionMode.Compress))

{

oBuffer = New Byte(inputFile.Length) {}

Dim counter As Integer = 0

While (counter = inputFile.Read(oBuffer,0,oBuffer.Length)) <> 0

oZipper.Write(oBuffer, 0, counter)

End While

}

oBuffer = Nothing

}

End If

MessageBox.Show("Compression done")

End Sub
Protected Sub DecompressFile()
If File.Exists(SourceFileName) Then
Imports (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

Imports (oZipper = New void NeweStream(inputFile, CompressionMode.Decompress))

{

oBuffer = New Byte(inputFile.Length) {}

Dim counter As Integer

While (counter = oZipper.Read(oBuffer,0,oBuffer.Length)) <> 0

outputFile.Write(oBuffer, 0, counter)

End While

}

oBuffer = Nothing

}

End If

' TO DO here notify the user that the action is performed

End Sub

End Class

End Class

Now,in order to consume the Class service here you is the code

Dim oZip As GZip = New GZip("C:\Test.txt","C:\Test1.txt",Action.Zip)

oZip = New GZip("C:\Test1.txt", "C:\Test2.txt", Action.UnZip)

End Class



Please Check my answer if you help....
Thank You...

Lalit.
Author: Ajesh Rawat             
Posted Date: May 19, 2010     Points: 5   

Hi Lalit..
This work only 30 days or life time.

Ajesh Singh Rawat
Consultant Programmer
Author: sarav             
Posted Date: May 25, 2010     Points: 5   

Hi, where can I download that Chilkat???

Thanks & Regards,
Sarav,
http://dotnetworldblog.blogspot.com/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend