相关疑难解决方法(0)

C#到Java:Base64String,MemoryStream,GZipStream

我有一个在.NET中压缩过的Base64字符串,我想将其转换回Java中的字符串。我正在寻找与C#语法等效的Java,尤其是:

  • Convert.FromBase64String
  • 内存流
  • GZipStream

这是我想转换的方法:

public static string Decompress(string zipText) {
    byte[] gzipBuff = Convert.FromBase64String(zipText);

    using (MemoryStream memstream = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzipBuff, 0);
        memstream.Write(gzipBuff, 4, gzipBuff.Length - 4);

        byte[] buffer = new byte[msgLength];

        memstream.Position = 0;
        using (GZipStream gzip = new GZipStream(memstream, CompressionMode.Decompress))
        {
            gzip.Read(buffer, 0, buffer.Length);
        }
        return Encoding.UTF8.GetString(buffer);
     }
}
Run Code Online (Sandbox Code Playgroud)

任何指针表示赞赏。

c# java memorystream gzipstream gzipinputstream

5
推荐指数
1
解决办法
7658
查看次数

GZipStream的解压缩性能很差

我有一个连接到后端WAS服务器的.NET 2.0 WinForms应用程序。我正在使用GZipStream解码从对服务器进行的HttpWebRequest调用返回的数据。返回的数据是Apache压缩的压缩CSV。整个服务器堆栈是Hibernate-> EJB-> Spring-> Apache。

对于较小的响应,性能很好(<50ms)。当我收到> 150KB的响应时,解压缩需要60秒钟以上。大部分时间似乎都花在了GZipStream构造函数上。

这是显示从HttpWebResponse调用获取响应流的代码:

using (Stream stream = this.Response.GetResponseStream())
{
 if (this.CompressData && this.Response.ContentEncoding == "gzip")
 {
        // Decompress the response
  byte[] b = Decompress(stream);
  this.ResponseBody = encoding.GetString(b);
    }
 else
 {
  // Just read the stream as a string
  using (StreamReader sr = new StreamReader(stream))
  {
   this.ResponseBody = sr.ReadToEnd();
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

编辑1

基于Lucero的评论,我将Decompress方法修改为以下内容,但是在实例化GZipStream之前,将ResponseStream加载到MemoryStream中不会带来任何性能优势。

private static byte[] Decompress(Stream stream)
{
 using (MemoryStream ms = new MemoryStream())
 {
  byte[] buffer = new byte[4096];
  int read = …
Run Code Online (Sandbox Code Playgroud)

c# compression performance gzipstream

5
推荐指数
1
解决办法
7032
查看次数

如何解决Gzip Magic Number Missing

我有一个字符串,我在服务器上Gzip并使用WebClient类下载到客户端.当我尝试解压缩它时,我收到错误信息,表明Magic Number丢失了.我已经尝试过GZipStream类和解决这个问题的ICSharpLib方法,所以我很茫然.

如果我省略通过WebClient下载的步骤(使用将数据作为byte []返回的DownloadData),压缩/解压缩工作,所以我只能假设数据被截断或损坏有些问题,但是因为它是压缩数据,我不知道如何调试它.

这是代码片段,似乎是有问题的部分:

   byte[] response
   try {
        response = client.DownloadData(Constants.GetSetting("SyncServer"));
   } catch {
        MessageBox.Show("There was a problem synchronizing the data. Please try verify the supplied credentials or try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
   }

   int rows = SQLiteAPI.ImportStatHistoryXML(CurrentUser.User, myCampus, Convert.ToBase64String(response));

public static int ImportStatHistoryXML(Person tempPerson, Campus tempCampus, string xmlFile) {
            byte[] encryptedFile = Convert.FromBase64String(xmlFile);
            MemoryStream memStream = new MemoryStream(encryptedFile);
            memStream.ReadByte();
            GZipInputStream stream = new GZipInputStream(memStream);
            MemoryStream memory = new MemoryStream();
            byte[] writeData = new byte[4096];
            int size; …
Run Code Online (Sandbox Code Playgroud)

c# compression gzip webclient httpresponse

1
推荐指数
1
解决办法
6968
查看次数