Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am implementing WCF REST service to accept .gzip file. We are able to receive .txt.and .xml file but when client sends the .gz file we are getting invalid stream or garbage data. we are not able to convert received stream into .gz file again.
Clinet sending gzip compressed ASCII file to service as HTTP POST method.

IService

VB
[OperationContract]
       [WebInvoke(Method = "POST", UriTemplate = "processfile",
           RequestFormat = WebMessageFormat.Json,
              ResponseFormat = WebMessageFormat.Xml,
              BodyStyle = WebMessageBodyStyle.Bare)]

       string processfile(Stream input);


Service :
C#
public string processfile(Stream input)
        {

            string saveToLog1 = @"E:\TestFiles\LogFilett.txt";
           
            compress(input);

            byte[] input1 = ReadFully(input);
            string output1 = Encoding.UTF8.GetString(input1, 0, input1.Length);
            File.WriteAllText(saveToLog1, output1);
            string saveToLog = @"E:\TestFiles\LogFile.txt";
                      File.AppendAllText(saveToLog, "File received at: " + System.DateTime.Now.ToString().Replace(":", "_").Replace("/", "-").Replace(" ", "_"));
             string saveTo = @"E:\TestFiles\Testtest.gz";
            File.WriteAllText(saveTo, output1, UTF8Encoding.UTF8);

            return string.Format("The input string is: {0}", DateTime.Now.ToString());

        }


ReadFully Method

C#
public static byte[] ReadFully(Stream input)
       {
           byte[] buffer = new byte[16 * 1024];
           using (MemoryStream ms = new MemoryStream())
           {
               int read;
               while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
               {
                   ms.Write(buffer, 0, read);
               }
               return ms.ToArray();
           }
       }


Thank you in advance
Posted
Comments
madhav mojad 20-Feb-15 6:10am    
I forgot to add Compress method here
private void compress(Stream input)
{
byte[] inputData = ReadFully(input);// File.ReadAllBytes( (input);
using (var output = new MemoryStream())
{
using (var compression = new GZipStream(output, CompressionMode.Compress,
true))
{
compression.Write(inputData, 0, inputData.Length);
}
string filename = System.DateTime.Now.ToString().Replace(":", "_").Replace("/", "-").Replace(" ", "_");
File.WriteAllBytes(@"E:\TestFiles\Filecomp" + filename + ".gz", output.ToArray());
Decompress(output.ToArray());
FileInfo f = new FileInfo(@"E:\TestFiles\Filecomp" + filename + ".gz");
DecompressFile(f);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900