Solved

.NET 2 - Compress and decompress not working

Posted on 2010-09-06
8
355 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now