Solved

.NET 2 - Compress and decompress not working

Posted on 2010-09-06
8
388 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 48
Visual studio 2015 1 34
Filtering a datagrid view 8 34
Visual C# Windows Project - Clone Form 4 15
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
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 …

685 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