Share via

Stream overload for file based function

Peter Volz 1,295 Reputation points
2023-06-05T17:17:01.2633333+00:00

Hello all,

I have this function to extract image001.emf from image001.emz which is Office internal file.

How to change the function to accept Streams for input and output? Can anyone help?

    Public Function GZipExtract(InputEMZ As String, OutputEMF As String) As Boolean
        GZipExtract = False
        Try
            Using MyImportStream As New FileStream(InputEMZ, FileMode.Open, FileAccess.Read)
                Using MyExportStream As New FileStream(OutputEMF, FileMode.Create, FileAccess.Write)
                    Using MyGZipStream As Compression.GZipStream = New Compression.GZipStream(MyImportStream, Compression.CompressionMode.Decompress)
                        Try
                            MyGZipStream.CopyTo(MyExportStream)
                            Return True
                        Catch Exception As Exception
                            Return False
                        End Try
                    End Using
                End Using
            End Using
        Catch Exception As Exception
            Return False
        End Try
    End Function
Developer technologies | VB
0 comments No comments

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2023-06-06T06:11:57.12+00:00

Hi @Peter Volz , You can refer to the following code to to make this function accept streams as input and output parameters.

Public Function GZipExtract(inputStream As Stream, outputStream As Stream) As Boolean
    GZipExtract = False
    Try
        Using MyGZipStream As New Compression.GZipStream(inputStream, Compression.CompressionMode.Decompress)
            Try
                MyGZipStream.CopyTo(outputStream)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Using
    Catch ex As Exception
        Return False
    End Try
End Function

Best Regards.

Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.