gpt4 book ai didi

c# - C#中的Gzip压缩和解压

转载 作者:太空狗 更新时间:2023-10-29 18:22:06 33 4
gpt4 key购买 nike

我试图在一个模块中压缩一个字符串,然后在另一个模块中解压它。这是我正在使用的代码。

压缩

public static string CompressString(string text)
{
byte[] buffer = Encoding.ASCII.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}

ms.Position = 0;
MemoryStream outStream = new MemoryStream();

byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}

解压

public static byte[] DecompressString(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}

将其用作:

 DecompressString(System.Text.Encoding.ASCII.GetBytes(ip));

但是,对于上述声明,我遇到了以下错误。

{"The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."} System.SystemException {System.IO.InvalidDataException}

最佳答案

这里是对代码的重写,它应该按您希望的方式工作。

我写在了LINQPad并且可以在其中进行测试。

请注意,这里几乎没有错误检查。您应该添加检查以查看是否所有读取操作都已完成并且实际上已经读取了它们应该读取的内容以及类似的检查。

输出

original: 256
This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.

compressed: 56
AAEAAB+LCAAAAAAABAALycgsVgCiRIWS1OISPYWQEcYHANU9d5YAAQAA

decompressed: 256
This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.

程序

void Main()
{
var input = "This is a test. This is a test. ";
input += input;
input += input;
input += input;
string compressed = Compress(input);
string decompressed = Decompress(compressed);

input.Dump("original: " + input.Length);
compressed.Dump("compressed: " + compressed.Length);
decompressed.Dump("decompressed: " + decompressed.Length);
}

public static string Decompress(string input)
{
byte[] compressed = Convert.FromBase64String(input);
byte[] decompressed = Decompress(compressed);
return Encoding.UTF8.GetString(decompressed);
}

public static string Compress(string input)
{
byte[] encoded = Encoding.UTF8.GetBytes(input);
byte[] compressed = Compress(encoded);
return Convert.ToBase64String(compressed);
}

public static byte[] Decompress(byte[] input)
{
using (var source = new MemoryStream(input))
{
byte[] lengthBytes = new byte[4];
source.Read(lengthBytes, 0, 4);

var length = BitConverter.ToInt32(lengthBytes, 0);
using (var decompressionStream = new GZipStream(source,
CompressionMode.Decompress))
{
var result = new byte[length];
decompressionStream.Read(result, 0, length);
return result;
}
}
}

public static byte[] Compress(byte[] input)
{
using (var result = new MemoryStream())
{
var lengthBytes = BitConverter.GetBytes(input.Length);
result.Write(lengthBytes, 0, 4);

using (var compressionStream = new GZipStream(result,
CompressionMode.Compress))
{
compressionStream.Write(input, 0, input.Length);
compressionStream.Flush();

}
return result.ToArray();
}
}

关于c# - C#中的Gzip压缩和解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134897/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com