none
GZIP Compress and Decompress with Progress Reporting RRS feed

  • Question

  • I have figured out how to report progress while compressing:

        Private Sub CompressFileToFile(InFile As String, Outfile As String)
            Dim InFileInfo As FileInfo = New FileInfo(InFile)
            Dim InFileLength As Long = InFileInfo.Length
            Dim Bytesread As Long = 0
            Dim BytesRemaining As Long = InFileLength
            Dim BufferSize As Long = InFileLength \ 100
            Dim BytesToRead As Long = BufferSize
            Dim buffer(CInt(BufferSize - 1)) As Byte
            Dim OutFileinfo As New FileInfo(Outfile)
            Try
                Using originalFileStream As FileStream = InFileInfo.OpenRead()
                    Using compressedFileStream As FileStream = File.Create(Outfile)
                        Using compressionStream As New GZipStream(compressedFileStream, CompressionMode.Compress)
                            Do While BytesRemaining > 0
                                originalFileStream.Read(buffer, 0, CInt(BufferSize))
                                compressionStream.Write(buffer, 0, CInt(BufferSize))
                                Bytesread += BufferSize
                                TxtStatus.Text = "Scrambling - " & ((Bytesread * 100) \ InFileLength).ToString("000") & "%"
                                TxtStatus.Refresh()
                                BytesRemaining -= BufferSize
                                If BytesRemaining < BufferSize Then BufferSize = BytesRemaining
                            Loop
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                If File.Exists(CompressedFileName) Then File.Delete(CompressedFileName) 'Outfile = CompressedFileName
            End Try
        End Sub
    

    I also can Decompress without reporting progress:

        Private Sub DecompressFileToFile(InputFile As String, OutputFile As String)
            Dim InFileInfo As FileInfo = New FileInfo(InputFile)
            Using originalFileStream As FileStream = InFileInfo.OpenRead()
                Using decompressedFileStream As FileStream = File.Create(OutputFile)
                    Using decompressionStream As GZipStream = New GZipStream(originalFileStream, CompressionMode.Decompress)
                        decompressionStream.CopyTo(decompressedFileStream)
                    End Using
                End Using
            End Using
        End Sub

    Tried to combine the two but the decompressed file is exactly the same as the compressed file:

        Private Sub DeCompressFileToFileWProgress(InFile As String, Outfile As String)
            Dim InFileInfo As FileInfo = New FileInfo(InFile)
            Dim InFileLength As Long = InFileInfo.Length
            Dim Bytesread As Integer = 0
            Dim BytesWritten As Integer = 0
            Dim BytesRemaining As Long = InFileLength
            Dim BufferSize As Integer = CInt(InFileLength \ 100)
            Dim BytesToRead As Long = BufferSize
            Dim buffer(CInt(BufferSize - 1)) As Byte
            Dim OutFileinfo As New FileInfo(Outfile)
            Try
                Using originalFileStream As FileStream = InFileInfo.OpenRead()
                    Using DecompressedFileStream As FileStream = File.Create(Outfile)
                        Using compressionStream As New GZipStream(DecompressedFileStream, CompressionMode.Compress)
                            Do While BytesRemaining > 0
                                ReDim buffer(CInt(BufferSize) - 1)
                                originalFileStream.Read(buffer, 0, CInt(BufferSize))
                                ReDim buffer(CInt(compressionStream.BaseStream.Length) - 1)
                                compressionStream.Write(buffer, 0, CInt(compressionStream.BaseStream.Length))
                                DecompressedFileStream.Write(buffer, BytesWritten, buffer.Length)
                                BytesWritten += buffer.Length
                                Bytesread += BufferSize
                                TxtStatus.Text = "DeScrambling - " & ((Bytesread * 100) \ InFileLength).ToString("000") & "%"
                                TxtStatus.Refresh()
                                BytesRemaining -= BufferSize
                                If BytesRemaining < BufferSize Then BufferSize = CInt(BytesRemaining)
                            Loop
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                If File.Exists(CompressedFileName) Then File.Delete(CompressedFileName)
            End Try
        End Sub
    
    Any ideas ?

    Wednesday, November 15, 2017 10:54 PM

All replies