Link to home
Start Free TrialLog in
Avatar of IUFITS

asked on

Extra character when decompressing with System.IO.Compression

The attached code snippet is a Gzip class to compress/uncompress a file.  If I take a text file with ASCII encoding, compress it and then decompress it the decompressed final file has an extra character at the end which appears to be a character 0.  Questions:

1.)  Does that have to do with encoding some how?
2.)  What am I missing to decompress the file to the exact same format it was when it was originally compressed, code example or at least where to look appreciated.

Public Class GZip
        Private Const buffer_size As Integer = 100
        Public Sub New(ByVal SourcePath As String, ByVal DestinationPath As String)
            SourceFileName = SourcePath
            DestinationFileName = DestinationPath
        End Sub
        Public Sub New()
        End Sub
        Public Sub Decompress()
            Dim sourceFile As FileStream = File.OpenRead(SourceFileName)
            Dim destFile As FileStream = File.Create(DestinationFileName)
            Dim fileStream As New GZipStream(sourceFile, CompressionMode.Decompress)
            ' Read and write the data on byte at a time
            Dim aByte As Integer = fileStream.ReadByte()
            While aByte <> -1
                destFile.WriteByte(CType(aByte, Byte))
                aByte = fileStream.ReadByte()
            End While
        End Sub
        Public Sub Compress()
            Dim inFileStream As New FileStream(SourceFileName, FileMode.Open)
            Dim Buffer(inFileStream.Length) As Byte
            inFileStream.Read(Buffer, 0, Buffer.Length)
            Dim outFileStream As New FileStream(DestinationFileName, FileMode.Create)
            Dim compressedStream As New GZipStream(outFileStream, CompressionMode.Compress)
            compressedStream.Write(Buffer, 0, Buffer.Length)
        End Sub
        Private _sourceFileName As String = ""
        Public Property SourceFileName() As String
                Return _sourceFileName
            End Get
            Set(ByVal value As String)
                _sourceFileName = value
            End Set
        End Property
        Private _destinationFileName As String = ""
        Public Property DestinationFileName() As String
                Return _destinationFileName
            End Get
            Set(ByVal value As String)
                _destinationFileName = value
            End Set
        End Property
    End Class

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IUFITS


That makes perfect sense.  I completely spaced out on that one.  

Thank you very much for the prompt and very helpful response!   :)
Avatar of IUFITS


Tested and worked perfect.  Sometimes a second set of eyes is needed to remind us of what later appears to be obvious!  :)   Again, thank you for your assistance and expertise, it's very much appreciated.