Solved

.NET 2 - Compress and decompress not working

Posted on 2010-09-06
8
390 Views
Last Modified: 2013-11-07
I must be going daft here, but definitely mad anyway ! I've used this library for years and now I'm finding that my compress and decompress functions are not working

I've attached the code for the compression functions. When I compress a text file and then uncompress it, the output I get is a load of binary.

Can you spot anything in my code ??

thanks
Paul


    Public Sub ZipFile(ByVal sSourceFile As String, ByVal sTargetFile As String)

        Dim fs As New System.IO.FileStream(sSourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)

        Dim ds As New System.IO.FileStream(sTargetFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)

        Dim cs As New System.IO.Compression.GZipStream(ds, System.IO.Compression.CompressionMode.Compress, True)

        Const blk As Integer = 100
        Dim sBuffer(blk) As Byte
        Dim bytesRead As Integer
        Do
            bytesRead = fs.Read(sBuffer, 0, blk)
            If (bytesRead = 0) Then Exit Do
            cs.Write(sBuffer, 0, bytesRead)
        Loop

        fs.Close()
        cs.Close()
        ds.Close()

    End Sub

    Public Sub UnzipFile(ByVal sSourceFile As String, ByVal sTargetFile As String)

        Dim ss As New System.IO.FileStream(sSourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim ds As New System.IO.FileStream(sTargetFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)

        Dim dcs As New System.IO.Compression.GZipStream(ss, System.IO.Compression.CompressionMode.Decompress, True)

        Const blk As Integer = 100
        Dim sBuffer(blk) As Byte
        Dim bytesRead As Integer
        Do
            bytesRead = dcs.Read(sBuffer, 0, blk)
            If (bytesRead = 0) Then Exit Do
            ds.Write(sBuffer, 0, bytesRead)
        Loop

        ss.Close()
        dcs.Close()
        ds.Close()
    End Sub
0
Comment
Question by:plq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33611403
use the following code:
Public Sub Compress(sSourceFile As [String], sTargetFile As [String])
	Using inFile As New FileStream(sSourceFile, FileMode.Open, FileAccess.Read)
		Using outFile As New FileStream(sSourceFile, FileMode.OpenOrCreate, FileAccess.Write)
			Using compress__1 As New GZipStream(outFile, CompressionMode.Compress)
				CopyStream(inFile, compress__1)
			End Using
		End Using
	End Using
End Sub

Public Sub UnzipFile(sSourceFile As [String], sTargetFile As [String])
	Using inFile As New FileStream(sSourceFile, FileMode.Open, FileAccess.Read)
		Using outFile As New FileStream(sSourceFile, FileMode.OpenOrCreate, FileAccess.Write)
			Using decompress As New GZipStream(inFile, CompressionMode.Decompress)
				CopyStream(decompress, outFile)
			End Using
		End Using
	End Using
End Sub

Public Sub CopyStream(input As Stream, output As Stream)
	Dim buffer As Byte() = New Byte(32767) {}
	While True
		Dim read As Integer = input.Read(buffer, 0, buffer.Length)
		If read <= 0 Then
			Return
		End If
		output.Write(buffer, 0, read)
	End While
End Sub

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33611450
sorry, i made a mistake in the code, here's an updated one (tested and working):
Public Sub ZipFile(ByVal sSourceFile As [String], ByVal sTargetFile As [String])
        Using inFile As New FileStream(sSourceFile, FileMode.Open, FileAccess.Read)
            Using outFile As New FileStream(sTargetFile, FileMode.OpenOrCreate, FileAccess.Write)
                Using compress As New GZipStream(outFile, CompressionMode.Compress)
                    CopyStream(inFile, compress)
                End Using
            End Using
        End Using
    End Sub

    Public Sub UnzipFile(ByVal sSourceFile As [String], ByVal sTargetFile As [String])
        Using inFile As New FileStream(sSourceFile, FileMode.Open, FileAccess.Read)
            Using outFile As New FileStream(sTargetFile, FileMode.OpenOrCreate, FileAccess.Write)
                Using decompress As New GZipStream(inFile, CompressionMode.Decompress)
                    CopyStream(decompress, outFile)
                End Using
            End Using
        End Using
    End Sub

    Public Sub CopyStream(ByVal input As Stream, ByVal output As Stream)
        Dim buffer As Byte() = New Byte(32767) {}
        While True
            Dim read As Integer = input.Read(buffer, 0, buffer.Length)
            If read <= 0 Then
                Return
            End If
            output.Write(buffer, 0, read)
        End While
    End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33611468
btw, your code works as well, which file did u try to compress and got binary data after decompress?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 8

Author Comment

by:plq
ID: 33611505
Thanks

I just pasted some plain text into a text file and tried it, compress, and then uncompress. If you've got my code working, I'd better look back at the basics here.. must be something silly.

0
 
LVL 8

Author Comment

by:plq
ID: 33611563
Its fixed - I'll explain what it was in 20 mins
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33611717
i'm not going anywhere
0
 
LVL 8

Author Comment

by:plq
ID: 33611739
Right it was something really stupid. The called had /unzip and /zip, we were calling it with minus instead of slash ! Sorry to mess you around but I really liked seeing the USING code and I'll definitely include that now as its much nicer.
0
 
LVL 8

Author Comment

by:plq
ID: 33611755
called = caller

            Dim bUnzip As Boolean
            If sArguments(0) = "/unzip" Then
                bUnzip = True
            Else
                bUnzip = False
            End If

            Dim c As New Compression
            If bUnzip Then
                c.UnzipFile(sArguments(1), sArguments(2))
            Else
                c.ZipFile(sArguments(1), sArguments(2))
            End If

and we used -unzip not /unzip ... very embarrasing !
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question