Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET compress string with StreamReader ?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19753 |

    Hello, tried to convert the example code with FileStream to use StreamReader to convert a string instead of a textfile, but recieve error. What am I doing wrong ?

    public byte[] WsTestZippedString(string doString) {
    using (StreamReader sourceStream = new StreamReader(doString)) {
    // Because the CompressedStream will automatically close the destination
    // memory stream, there is no need to declare the memory stream within a using
    // statement or to call Close once we are done with the stream.
    //
    // If you do not want the inner stream to be closed by the CompressedStream
    // then set the CompressedStream’s Transient property to true.

    MemoryStream destinationStream = new MemoryStream();

    using (CompressedStream compStream = new CompressedStream(destinationStream)) {
    // 32K at at time.
    byte[] buffer = new byte[32768];
    int bytesRead = 0;

    // Loop until we have nothing more to read from the source stream.
    while((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0) {
    compStream.Write(buffer, 0, bytesRead);
    }
    }

    // To get access to the MemoryStream’s compressed data, you can use
    // byte[] compressedData = destinationStream.ToArray();
    // ToArray() works even when the memory stream is closed.

    return destinationStream.ToArray();
    }
    }

    Error: CS1502: The best overloaded method match for ‘System.IO.TextReader.Read(char[], int, int)’ has some invalid arguments

    Imported from legacy forums. Posted by AmigoFd (had 2290 views)

    User (Old forums)
    Member
    Post count: 23064

    You need to use a regular stream to do this. You can build a stream around a string by using the GetBytes() method on the Encoding class.

    e.g.:

    string doString = “text to compress”;

    using( Stream sourceStream = new MemoryStream( System.Text.Encoding.ASCII.GetBytes( doString ) ) )
    {
    MemoryStream destinationStream = new MemoryStream();

    using (CompressedStream compStream = new CompressedStream(destinationStream))
    {
    // 32K at at time.
    byte[] buffer = new byte[32768];
    int bytesRead = 0;

    // Loop until we have nothing more to read from the source stream.
    while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
    {
    compStream.Write(buffer, 0, bytesRead);
    }
    }
    return destinationStream.ToArray();
    }

    Imported from legacy forums. Posted by André (had 3164 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.