Domanda

I'm having troubles in writing a static Deflate extension method, that i would use to deflate a string, using BZip2 alghorithm of the SharpZipLib library (runtime version: v2.0.50727).

I'm doing it using .NET framework 4.

This is my VB.NET code:

Public Function Deflate(ByVal text As String)
    Try
        Dim compressedData As Byte() = Convert.FromBase64String(text)

        System.Diagnostics.Debug.WriteLine(String.Concat("Compressed text data size: ", text.Length.ToString()))
        System.Diagnostics.Debug.WriteLine(String.Concat("Compressed byte data size: ", compressedData.Length.ToString()))

        Using compressedStream As MemoryStream = New MemoryStream(compressedData)
            Using decompressionStream As BZip2OutputStream = New BZip2OutputStream(compressedStream)
                Dim cleanData() As Byte

                Using decompressedStream As MemoryStream = New MemoryStream()
                    decompressionStream.CopyTo(decompressedStream) // HERE THE ERROR!

                    cleanData = decompressedStream.ToArray()
                End Using

                decompressionStream.Close()
                compressedStream.Close()

                Dim cleanText As String = Encoding.UTF8.GetString(cleanData, 0, cleanData.Length)

                System.Diagnostics.Debug.WriteLine(String.Concat("After decompression text data size: ", cleanText.Length.ToString()))
                System.Diagnostics.Debug.WriteLine(String.Concat("After decompression byte data size: ", cleanData.Length.ToString()))

                Return cleanText
            End Using
        End Using
    Catch
        Return String.Empty
    End Try
End Function

The strange thing is that I wrote a C# counterpart of the same method, and it works perfectly!!! This is the code:

public static string Deflate(this string text)
{
        try
        {

            byte[] compressedData = Convert.FromBase64String(text);

            System.Diagnostics.Debug.WriteLine(String.Concat("Compressed text data size: ", text.Length.ToString()));
            System.Diagnostics.Debug.WriteLine(String.Concat("Compressed byte data size: ", compressedData.Length.ToString()));

            using (MemoryStream compressedStream = new MemoryStream(compressedData))
            using (BZip2InputStream decompressionStream = new BZip2InputStream(compressedStream))
            {
                byte[] cleanData;

                using (MemoryStream decompressedStream = new MemoryStream())
                {
                    decompressionStream.CopyTo(decompressedStream);

                    cleanData = decompressedStream.ToArray();
                }

                decompressionStream.Close();
                compressedStream.Close();

                string cleanText = Encoding.UTF8.GetString(cleanData, 0, cleanData.Length);

                System.Diagnostics.Debug.WriteLine(String.Concat("After decompression text data size: ", cleanText.Length.ToString()));
                System.Diagnostics.Debug.WriteLine(String.Concat("After decompression byte data size: ", cleanData.Length.ToString()));

                return cleanText;
            }

        }
        catch(Exception e)
        {
            return String.Empty;
        }
}

In VB.NET version I get this error: "Stream does not support reading." (see the code to understand where it comes!)

Where is the mistake?!! I cannot understand what's the difference between the two methods...

Thank you very much!

È stato utile?

Soluzione

A game of spot the difference shows that in the first you are using BZip2OutputStream whereas the second is BZip2InputStream.

It seems reasonable that the output stream is used to write to and so as it says is not readable.

For what its worth there are a lot of good comparison tools out there. They won't cope with syntax different but the way the matching works it shows up quite well when you are using totally different objects (in this case at least). The one I personally use and recommend is Beyond Compare

Altri suggerimenti

You switched BZip2OutputStream and BZip2InputStream

In one version you are using a BZip2InputStream and in the other a BZip2OutputStream.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top