Solved

.NET 2 - Compress and decompress not working

Posted on 2010-09-06
8
384 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

828 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