0

我是 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

我做错了什么?

4

2 回答 2

0

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

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

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


似乎 Java 和 C# 不同意“放气”压缩算法的确切种类,但根据我的测试,“gzip”应该可以工作:

xmlData这在 Java 中压缩:

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

input在 C# 中解压缩:

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();
    }
  }
}
于 2012-02-23T15:27:35.303 回答
0

看起来@Joni Salonen 已经对放气问题给出了答案,所以我想补充一点关于架构的内容。要隔离问题,您应该将这两个问题分开。首先,您需要删除一个压缩文件。然后你需要给它放气。然后,分别关注问题区域。以后您总是可以“流水线”处理这两个问题。

顺便说一句,在许多情况下拥有原始 zip 文件很有用。当某些事情没有按计划工作时,它就变成了一个安全网,尤其是。如果您只有一次机会捕获文件。

于 2012-02-23T15:59:20.810 回答