Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/162.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#Gzip写自定义压缩_C#_Gzip_Compression - Fatal编程技术网

C#Gzip写自定义压缩

C#Gzip写自定义压缩,c#,gzip,compression,C#,Gzip,Compression,我有这个解压方法,我想在此基础上制作一个压缩方法。有人能帮我吗?(我试着自己写,但没有成功) 解压缩方法: public static byte[] Decompress(byte[] data) { MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); memoryStream.Position = 0L; GZipStream gZipStre

我有这个解压方法,我想在此基础上制作一个压缩方法。有人能帮我吗?(我试着自己写,但没有成功)

解压缩方法:

public static byte[] Decompress(byte[] data)
{
   MemoryStream memoryStream = new MemoryStream();
   memoryStream.Write(data, 0, data.Length);
   memoryStream.Position = 0L;
   GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress, true);
   MemoryStream memoryStream2 = new MemoryStream();
   byte[] array = new byte[64];

   for (int i = gZipStream.Read(array, 0, array.Length); i > 0; i = gZipStream.Read(array, 0, array.Length))
   {
        memoryStream2.Write(array, 0, i);
   }

   gZipStream.Close();
   return memoryStream2.ToArray();
}
这就是我试图编写Compress方法的方式,但当我将其解压缩回来时没有得到正确的结果:

public static byte[] Compress(byte[] data)
        {
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(data, 0, data.Length);
            memoryStream.Position = 0L;
            GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, false);
            byte[] array = new byte[64];
            gZipStream.Write(array, 0, array.Length);
            gZipStream.Close();
            return memoryStream.ToArray();
        }

尝试使用以下简单实现:

public static byte[] Compress(byte[] data)
{
    using (MemoryStream memoryStream = new MemoryStream())
    using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
    {
        gzipStream.Write(data, 0, data.Length);
        return memoryStream.ToArray();
    }
}

你有什么特别的问题吗?请阅读。我没有其他规范,我想在解压方法的基础上制作压缩方法。然后试着去做。如果您面临某些特定问题-寻求解决方案。我编辑了我的问题,请立即检查ti。请澄清您的特定问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参阅页面以获取澄清此问题的帮助。是的,但在解压方法中,它总是读取64字节,而不是一次读取所有字节。如果该方法以单字节数组的形式返回所有解压数据,是否有理由按短块读取?是的。因为后来需要根据另一种算法对结果进行加密。完全不清楚你在说什么。“结果”到底是什么?问题中的加密在哪里?而且这个话题本身仍然没有具体的问题!对不起,我的英语不好,我只是想压缩方法正常工作。如果你能帮助我,那就太好了。