?
Solved

.NET 2 - Compress and decompress not working

Posted on 2010-09-06
8
Medium Priority
?
397 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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