- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图在一个模块中压缩一个字符串,然后在另一个模块中解压它。这是我正在使用的代码。
压缩
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/
我正在尝试创建有效的 gzip 文件(可以使用标准 Linux gzip 解压缩),其中的数据使用 DEFLATE 算法和静态/预设字典编码。 我已阅读 DEFLATE 的两个规范和 gzip ,而且
我刚刚搜索了gzip和 Deflate ,并发现 Deflate 更好。 GZip or Deflate for HTTP compression Why use deflate instead of
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我一直在寻找 Gzip 是否支持多部分文件压缩。 From what I have seen so far it does not, but how come 7z allows multipart
我可以连接两个已经 gzip 的文件(使用 gzip)然后用枪压缩它们吗? 截至今天,我从远程服务器下载 gzip 压缩文件,单独对其进行 gunzip,然后将它们合并。 希望通过合并 gzipped
我正在创建两个 gzip 文件,一个只包含一个 gzip 成员,而第二个包含 2 个 gzip 成员(两个文件连接成一个 gzip 文件)。 当我尝试通过网络服务器下载此文件时,chrome 会很好地
当我对文件夹进行去皮重和 gzip 压缩时,每次我都会得到不同的文件大小。 目录内容不变,没有变化。压缩后的文件大小变化 20 到 100 字节。这是正常行为吗? 我的数据会受到影响吗? 谢谢 最佳答
比方说 file.txt.gz有 2GB,我想看到最后 100 行左右。 zcat
我一直听说对您的网站进行 gzip 压缩是加快交付速度的良好做法。我的网站总体负载非常大,我还应该研究 gzip 吗?我还了解了使用 gzip 的缺点,例如解压缩内容以便浏览器显示所需的时间。这是真的
下载 jQuery 的生产副本时,链接旁边显示该文件为 32K Minified & Gzipped。我得到了 Minified,但是 Gzipped 是什么意思? 它是否被网络服务器压缩,如 Apa
我的网站很高兴根据以下内容进行 Gzip 压缩: http://www.gidnetwork.com/tools/gzip-test.php 但是,当我通过 Yslow 运行它时,我得到了 Gzip
我使用命令 tar 一组文件:tar -czvf file.tar.gz file/ 然后复制到 USB(ext4 格式),我检查了我可以解压它。重装系统后,挂载usb时发生了一些错误,我执行fsck
我有一个提供小部件的网络服务。为了可扩展性,我想在 Amazon S3 上保留 js 文件的 gzip 版本。问题是不能接受 gzip 文件的浏览器将不会被提供。 任何人都知道我在哪里可以找到统计数据
我正在使用 Chrome 和 Firefox 下的 Yslow 工具查看我的开发站点,其中一项建议是我对适当的内容进行 gzip。作为起点,我刚刚在我的 [/] 配置中添加了“tools.gzip.o
我正在开发一个网站,我正在使用 gzip.exe 来预压缩 css 和 js 文件(只有 1 个 css 文件从 4.53 KB 到 1.50 KB,还有一个 js 文件包含 jquery 和一些来自
我们必须使用什么类型的响应监听器来处理 Android Volley 的 gzip 响应? 如果使用 String 监听器,则响应似乎会丢失其编码。 你如何使用 Volley 处理 gzip 响应?
我用 Fiddler调试我的应用程序。每当响应被服务器压缩,而不是解压缩响应时,Fiddler 显示不可读的二进制数据: /* Response to my request (POST) */ HTT
我通常使用tar zcvf压缩并使用tar zxvf解压缩(由于习惯使用gzip)。 我最近购买了一个具有超线程功能的四核 CPU,因此我有 8 个逻辑核心,并且我注意到许多核心在压缩/解压缩期间未使
我正在使用 Google Cloud Storage 控制台上传文件。我没有使用任何命令行工具。 我想在元数据中将 Content-Encoding 设置为 gzip(-z 选项)。 请看下面的截图,
我正在将mysqldump的结果用管道传输到gzip,gzip的速度似乎大大落后 gzip: 34.9MiB 0:01:54 [ 218kiB/s] mysqldump: 735MiB 0:01:5
我是一名优秀的程序员,十分优秀!