在C#中解压缩数据

时间:2012-02-23 12:54:55

标签: c# web-services gzipstream

我是C#的新手,我写了一个简单的Web服务。它接受zip文件并在文件系统中解压缩。在C#中代码是:

[WebMethod]
public String SetZip(string device_id, string file)
{
    if (device_id == null || device_id.Length == 0)
    {
        return "10;no auth data";
    }

    StringBuilder output = new StringBuilder();

    if (direcory == null)
    {
        return output.ToString();
    }

    string dirname = "c:\\temp\\" + direcory + "\\";

    if (!System.IO.Directory.Exists(dirname))
    {
        System.IO.Directory.CreateDirectory(dirname);
    }

    string filename = dirname + "file1.txt";

    string text = UnZipStr(Convert.FromBase64String(file));

    File.WriteAllText(filename, text);

    output.AppendLine("0;done");

    return output.ToString();
}

public static string UnZipStr(byte[] input)
{
    using (MemoryStream memstream = new MemoryStream())
    {
        using (MemoryStream inputStream = new MemoryStream(input))
        {
            using (DeflateStream gzip =
              new DeflateStream(inputStream, CompressionMode.Decompress))
            {
                using (StreamReader reader =
                  new StreamReader(gzip, System.Text.Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}

从java代码发送zip数据:

    void callService(byte[] xmlData) {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("device_id", AGENT);

    Deflater deflater = new Deflater();
    deflater.setInput(xmlData);
    deflater.finish();

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buf = new byte[1024];
     while (!deflater.finished()) {
         int byteCount = deflater.deflate(buf);
         baos.write(buf, 0, byteCount);
     }
     deflater.end();

     byte[] compressedBytes = baos.toByteArray();

    request.addPropertyIfValue("file", org.kobjects.base64.Base64.encode(compressedBytes));...}

在C#代码中,当从StreamReader

读取数据时,我有异常
SoapFault - faultcode: 'soap:Server' faultstring:     'System.Web.Services.Protocols.SoapException: ---> InvalidDataException: Block length does    not correspond to the complement.
System.IO.Compression.Inflater.DecodeUncompressedBlock(Boolean& end_of_block)
System.IO.Compression.Inflater.Decode()
System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
System.IO.StreamReader.ReadBuffer()
System.IO.StreamReader.ReadToEnd()
Service.UnZipStr(Byte[] input) в c:\inetpub\wwwroot\WebSite\App_Code\Service.cs: at 94
Service.SetZip(String device_id, String file) в c:\inetpub\wwwroot\WebSite\App_Code    \Service.cs: at 73

我做错了什么?

2 个答案:

答案 0 :(得分:0)

编辑:我一直在挖掘,有一种方法可以从Java生成DEFLATE格式数据,C#识别出来:你必须使用Deflater(int,boolean)构造函数。因此,将Java代码更改为:

Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
deflater.setInput(xmlData);
deflater.finish();

原因是默认情况下,deflater会发出一个ZLIB标头和校验和,这是C#DeflateStream不期望的。


似乎Java和C#不同意“deflate”压缩算法的确切多样性,但通过我的测试“gzip”应该可以工作:

这压缩了Java中的xmlData

ByteArrayOutputStream baos = new ByteArrayOutputStream()
OutputStream out = new GZIPOutputStream(baos);
out.write(xmlData);
out.close();
byte[] compressedBytes = baos.toByteArray();

在C#中解压缩input

using (MemoryStream inputStream = new MemoryStream(input))
{
  using (GZipStream gzip = new GZipStream(inputStream, CompressionMode.Decompress))
  {
    using (StreamReader reader = new StreamReader(gzip, System.Text.Encoding.UTF8))
    {
      return reader.ReadToEnd();
    }
  }
}

答案 1 :(得分:0)

看起来@Joni Salonen已经解决了deflate问题,所以我想补充一点关于架构的问题。要隔离问题,您应该将这两个问题分开。首先,您需要删除压缩文件。然后你需要放气。然后,分别关注问题区域。你总是可以"管道"这两个问题后来。

顺便说一下,在许多实例中使用原始zip文件很有用。当某些东西不能按计划运行时,它就会变成一个安全网,尤其是如果你只有一次机会捕获文件。