Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Decompressing a Base-64, BWT compressed data string created with XceedSco & XceedBen ActiveX components

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

    Hello,

     I represent a company that’s been using Xceed components for a number of years.  In one of our legacy products we compress and base64 encode a stream of data and store it in a file.  I’ve been tasked to extract, decode, and decompress this data, but we would like to use your .NET components to do so.  I have found a thread simply stating that this can be done (http://xceed.com/CS/forums/post/12154.aspx), but it doesn’t provide an implementation.

     Our legacy product uses Interop to call into the XceedZip library, this is the code used to store the data:

     <code>

    XceedStreamingCompressionLib.XceedStreamingCompression objStream = new XceedStreamingCompressionLib.XceedStreamingCompressionClass();
    objStream.License(“xxx”);

    XceedStreamingCompressionLib.XceedBWTCompressionMethod objMethod = new XceedStreamingCompressionLib.XceedBWTCompressionMethodClass();
    objMethod.CompressionLevel = XceedStreamingCompressionLib.EXCCompressionLevel.cclMedium;
    objStream.CompressionFormat = objMethod;

    XceedBinaryEncodingLib.XceedBinaryEncoding objEncoder = new XceedBinaryEncodingLib.XceedBinaryEncodingClass();
    objEncoder.License(“xxx”);

    objEncoder.EncodingFormat = new XceedBinaryEncodingLib.XceedBase64EncodingFormatClass();

    object OData = sData;
    object CData = objStream.Compress(ref OData, true);
    object EData = objEncoder.Encode(ref CData, true);

    objEncoder = null;
    objMethod = null;
    objStream = null;

    return Encoding.Unicode.GetString((byte[])EData);

    </code>

    If I understand correctly, this compresses an input string using BWT medium compression and then encodes the result as a base 64 unicode string so we can store it in our propietary XML file format.

     Now, according to the forum thread I posted earlier, I should be able to use .NET’s class library to decode the base 64 string, use BWT “normal” decompression, and end up with the original string?  This is a sample of the code that attempts to do this.  I’m using build 3.7.8465.14130 of the Xceed .NET components.

     <code>

    // parse through proprietary file, return the encoded and compressed string “data” 

    // This will raise a FormatException “Invalid character in a Base-64 string”
    var bytes = Convert.FromBase64String(data);

    // So I tried manually creating a UTF8 encoding and passing in false to “throw exception on invalid characters” (second param of constructor)
    /*
    var encoding = new UTF8Encoding(false, false);
    var utf8String = encoding.GetString(Encoding.Unicode.GetBytes(data));
    var decoded64Bytes = Convert.FromBase64String(utf8String); */

    // I was able to get a byte array that happily went into an XceedCompressedStream, but when I tried to read from the stream
    // I received a NotSupportedException “The XceedCompressedStream does not support having data written in its reserved bits.”

    var xceedCompressedStream = new XceedCompressedStream(new MemoryStream(bytes), CompressionMethod.BWT, CompressionLevel.Normal);

    var outputStream = new MemoryStream();

    // 32K at at time.

    var buffer = new byte[32768];
    int bytesRead;

    // Loop until we have nothing more to read from the source stream.

    while ((bytesRead = xceedCompressedStream.Read(buffer, 0, buffer.Length)) > 0)
    {
     outputStream.Write(buffer, 0, bytesRead);
    }

    </code>

    As a last resort we will use the ActiveX COM component to do the decoding and decompression (I’ve already tested that implementation and it works as advertised), but we would really appreciate some assistance getting this working with the .NET components so we don’t have to have a COM dependency.

     Thanks,

    Imported from legacy forums. Posted by Sean (had 5745 views)

    User (Old forums)
    Member
    Post count: 23064

    Some follow up.  I’ve successfully decoded the base 64 compressed string that was generated by XceedBen, but I’m unable to decompress.  

    Below are two code snippets, the first is a repeat from the first post in this thread showing how we compress and encode our data.  The second shows my latest attempt at reversing this process using .NET only components. I included comments to detail the exceptions raised during the process, but for clarity’s sake the exception I’m getting is:

    “NotSupportedException, The XceedCompressedStream does not support having data written in its reserved bits”

    <code>

    // Legacy ActiveX components used to compress and encode our data

    XceedStreamingCompressionLib.XceedStreamingCompression objStream = new XceedStreamingCompressionLib.XceedStreamingCompressionClass();
    objStream.License(“xxx”);

    XceedStreamingCompressionLib.XceedBWTCompressionMethod objMethod = new XceedStreamingCompressionLib.XceedBWTCompressionMethodClass();
    objMethod.CompressionLevel = XceedStreamingCompressionLib.EXCCompressionLevel.cclMedium;
    objStream.CompressionFormat = objMethod;

    XceedBinaryEncodingLib.XceedBinaryEncoding objEncoder = new XceedBinaryEncodingLib.XceedBinaryEncodingClass();
    objEncoder.License(“xxx”);

    objEncoder.EncodingFormat = new XceedBinaryEncodingLib.XceedBase64EncodingFormatClass();

    object OData = sData;
    object CData = objStream.Compress(ref OData, true);
    object EData = objEncoder.Encode(ref CData, true);

    objEncoder = null;
    objMethod = null;
    objStream = null;

    return Encoding.Unicode.GetString((byte[])EData);

    </code>

    <code>

    // Decode base 64 string using .NET encoding libraries
    // I have confirmed this produces the same output as the XceedBen library
    var asciiString = Encoding.ASCII.GetString(Encoding.Unicode.GetBytes(data));

    var decodedBytes = Convert.FromBase64String(asciiString);

    // According to the documentation, this is the CompressedStream implementation
    // we should be using to decompress data compressed with the ActiveX XceedSco
    // http://doc.xceedsoft.com/products/XceedWfWorkflow/Xceed.Compression.Formats~Xceed.Compression.Formats.XceedCompressedStreamEx.html

    var xceedCompressedStream = new XceedCompressedStreamEx(new MemoryStream(decodedBytes),
                                                                        CompressionMethod.BWT, CompressionLevel.Normal, false);

    // The above documentation reference says to make sure the header and footer are written that I write 0 bytes
    // but an exception is thrown when I attempt this
    // “NotSupportedException, An attempt was made to use an unsupported compression method.  The XceedCompressedStream only supports the Deflate, Deflate64 and Stored compression methods
    //xceedCompressedStream.Write(new byte[0], 0, 0);

    var outputStream = new MemoryStream();
               
    var buffer = new byte[32768];
    int bytesRead;

    // Loop until we have nothing more to read from the source stream.
    // Exception thrown whenever I try to read from the buffer:
    // “NotSupportedException, The XceedCompressedStream does not support having data written in its reserved bits”
    // I’ve tried all compression levels with the BWT compression method
    while ((bytesRead = xceedCompressedStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        outputStream.Write(buffer, 0, bytesRead);
    }

    </code>

    Imported from legacy forums. Posted by Sean (had 362 views)

    Xceed Support
    Member
    Post count: 5658

    In your case, XceedCompressedStream and XceedCompressedStreamEx don’t apply because you used the Xceed Streaming Compression component and its XceedBWTCompressionMethod, which is ‘raw’ BWT without Xceed-specific format data.

    I suggest you use the class

    Xceed.Compression.CompressedStream

    Example:

    var compressedStream = new CompressedStream(new MemoryStream(decodedBytes), CompressionMethod.BWT, CompressionLevel.Normal, false);

    Imported from legacy forums. Posted by Jb [Xceed] (had 1900 views)

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