Last post Aug 14, 2009 03:26 PM by rkeslar
Aug 14, 2009 02:24 PM|rkeslar|LINK
I'm trying to compress a file in .NET, but having some trouble. The zip file is created, but it's only 1KB and no files are within the zip file. Basically, it's not working for some reason and I'm not sure why.
This is the method I am using. Could you help? Thanks
Public Sub CompressFile(ByVal fileName As String)
Dim strOutput As Stream = File.Create("zippedFile.zip")
Dim strInput As Stream = File.OpenRead(fileName)
Dim reader As StreamReader = New StreamReader(strInput, System.Text.Encoding.Default)
Dim compress As DeflateStream = New DeflateStream(strOutput, CompressionMode.Compress, True)
Dim writer As StreamWriter = New StreamWriter(compress, System.Text.Encoding.Default)
Dim myString As String = reader.Read()
Catch Ex As Exception
MessageBox.Show(Ex.Message & vbCr & vbCr & Ex.Source & vbCr & vbCr & Ex.StackTrace)
Aug 14, 2009 03:07 PM|rtpHarry|LINK
I dont think this is how you create a zip file in .net.
There is the system.IO.packages namespace which can create zip files though:
specifically this writing a package sample:
Aug 14, 2009 03:26 PM|rkeslar|LINK
Actually I found a really cool example on www.vbforums.com that uses a library from j#.
The Packaging Namespace is probably preferable if you're using .NET 3.0 or higher, but for this project I am required to use .NET 2.0. This compression works very well and is simple to implement too.