.net压缩功能实现方法
程序员文章站
2024-02-24 14:27:16
复制代码 代码如下:public static class compressor { &n...
复制代码 代码如下:
public static class compressor {
public static byte[] compress(byte[] data)
{
using (memorystream output = new memorystream())
{
using (gzipstream gzip = new gzipstream(output, compressionmode.compress, true))
{
gzip.write(data, 0, data.length);
gzip.close();
return output.toarray();
}
}
}
public static byte[] decompress(byte[] data)
{
using (memorystream input = new memorystream())
{
input.write(data, 0, data.length);
input.position = 0;
using (gzipstream gzip = new gzipstream(input, compressionmode.decompress, true))
{
using (memorystream output = new memorystream())
{
byte[] buff = new byte[64];
int read = -1;
read = gzip.read(buff, 0, buff.length);
while (read > 0)
{
output.write(buff, 0, read);
read = gzip.read(buff, 0, buff.length);
}
gzip.close();
return output.toarray();
}
}
}
}
上一篇: 编写线程安全的JSP程序