Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言解压数据#_C#_Web Services_Gzipstream - Fatal编程技术网

C# 用C语言解压数据#

C# 用C语言解压数据#,c#,web-services,gzipstream,C#,Web Services,Gzipstream,我是C#的新手,我正在编写一个简单的web服务。它获取zip文件并在文件系统中解压缩。在C中,代码是: 并从java代码发送zip数据: void callService(byte[] xmlData) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("device_id", AGENT); Deflater deflater = new Def

我是C#的新手,我正在编写一个简单的web服务。它获取zip文件并在文件系统中解压缩。在C中,代码是:

并从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

我做错了什么?

编辑:我一直在研究,有一种方法可以从Java生成C#认可的DEFLATE格式数据:您必须使用构造函数。因此,将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#中解压缩
输入


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


顺便说一句,在许多情况下使用原始zip文件是很有用的。当某些东西不能按计划工作时,特别是当您只有一次机会捕获文件时,它会成为一个安全网

您是否尝试使用
GZipStream
而不是
deflatesttream
解压?是的,并且出现其他异常“GZip头中的无效幻数。传输必须进入GZip流。”+1”回答问题。这家伙是个新手。
ByteArrayOutputStream baos = new ByteArrayOutputStream()
OutputStream out = new GZIPOutputStream(baos);
out.write(xmlData);
out.close();
byte[] compressedBytes = baos.toByteArray();
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();
    }
  }
}