locked
how to gzip unzip in wcf RRS feed

  • Question

  • User-776477372 posted

    Hi friends,

    how to gzip unzip in wcf

    Monday, August 5, 2013 12:45 AM

All replies

  • User1293050055 posted

    http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

    Monday, August 5, 2013 1:01 AM
  • User-776477372 posted

    Hi,

    While comperessing, it wirks fine, but while decompressing the compressed sting using GZip, I am getting error "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream." Please go through the follwoing code.

    string Body = string.Empty;

    Body ="sample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample textsample ";
    string strZiped= Zip(Body);
    string strUnZiped = UnZip(strZiped);

    public static string Zip(string value)
    {
    byte[] byteArray = new byte[value.Length];
    int indexBA = 0;
    foreach (char item in value.ToCharArray())
    {
    byteArray[indexBA++] = (byte)item;
    }

    //Prepare for compress
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
    System.IO.Compression.CompressionMode.Compress);
    sw.Write(byteArray, 0, byteArray.Length);
    sw.Close();
    byteArray = ms.ToArray();
    System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
    foreach (byte item in byteArray)
    {
    sB.Append((char)item);
    }
    ms.Close();
    sw.Dispose();
    ms.Dispose();
    string strCompressed = sB.ToString();
    return strCompressed;
    }


    public static string UnZip(string Compressedvalue)
    {
    byte[] byteArray = Encoding.UTF8.GetBytes(Compressedvalue);
    MemoryStream stream = new MemoryStream(byteArray);
    Stream sourceStream = stream;
    if (sourceStream == null)
    {
    throw new ArgumentException();
    }

    if (!sourceStream.CanRead)
    {
    throw new ArgumentException();
    }

    MemoryStream memoryStream = new MemoryStream();
    const int bufferSize = 65536;

    using (GZipStream gzipStream = new GZipStream(sourceStream, CompressionMode.Decompress))
    {
    byte[] buffer = new byte[bufferSize];

    int bytesRead = 0;

    do
    {
    bytesRead = gzipStream.Read(buffer, 0, bufferSize);
    }
    while (bytesRead == bufferSize);

    memoryStream.Write(buffer, 0, bytesRead);
    }
    byte[] btArraymemoryStream = memoryStream.ToArray();
    string st = Encoding.UTF8.GetString(btArraymemoryStream);
    return st;

    }

    Monday, August 5, 2013 3:42 AM
  • User260886948 posted

    Hi,

    I see that you have post a same question in: http://forums.asp.net/t/1927234.aspx . Please try to check the reply.

     

    Best Regards,
    Amy Peng

    Tuesday, August 6, 2013 2:58 AM