全栈工程师开发手册 (作者:栾鹏)
c#教程全解
c#实现gzip压缩解压缩byte[]字节数组,文件,字符串。
测试代码
static void Main()
{
//测试字符串
String inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";
System.Console.WriteLine("原文:\t" + inputStr);
byte[] input = System.Text.Encoding.Default.GetBytes(inputStr);
System.Console.WriteLine("长度:\t" + input.Length);
byte[] data = gzipCompress(input);
System.Console.WriteLine("压缩后:\t");
System.Console.WriteLine("长度:\t" + data.Length);
byte[] output = gzipDecompress(data);
String outputStr = System.Text.Encoding.Default.GetString(output);
System.Console.WriteLine("解压缩后:\t" + outputStr);
System.Console.WriteLine("长度:\t" + output.Length);
//测试文件
FileStream fos = new FileStream("test.txt",FileMode.OpenOrCreate);
fos.Write(input,0,input.Length);
fos.Flush();
fos.Close();
gzipCompress("test.txt", false);
gzipDecompress("test.txt.gz", false);
FileStream fis = new FileStream("test.txt", FileMode.Open);
byte[] data1 = new byte[(int)fis.Length];
fis.Read(data1, 0, data1.Length);
fis.Close();
outputStr = System.Text.Encoding.Default.GetString(data1);
System.Console.WriteLine("解压缩后:\t" + outputStr);
}
gzip压缩解压缩函数的实现
//gzip字节数组压缩
public static byte[] gzipCompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(data, 0, data.Length);
zip.Close();
byte[] buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
ms.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
//文件压缩 ,默认删除原始文件
public static void gzipCompress(string file)
{
gzipCompress(file, true);
}
//文件压缩 是否删除原始文件
public static void gzipCompress(string file, bool delete)
{
FileStream fis = new FileStream(file,FileMode.Open);
FileStream fos = new FileStream(file + ".gz", FileMode.OpenOrCreate, FileAccess.Write);
gzipCompress(fis, fos);
fis.Close();
// fos.Flush();
fos.Close();
if (delete)
{
File.Delete(file);
}
}
//数据压缩
public static void gzipCompress(Stream input, Stream output)
{
GZipOutputStream gos = new GZipOutputStream(output);
int count;
byte[] data = new byte[1024];
while ((count = input.Read(data, 0, 1024)) >0)
{
gos.Write(data, 0, count);
}
gos.Finish();
gos.Flush();
gos.Close();
}
//gzip字节数组解压缩
public static byte[] gzipDecompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
MemoryStream msreader = new MemoryStream();
byte[] buffer = new byte[0x1000];
while (true)
{
int reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}
msreader.Write(buffer, 0, reader);
}
zip.Close();
ms.Close();
msreader.Position = 0;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
//文件解压缩 ,默认删除源文件
public static void gzipDecompress(string path)
{
gzipDecompress(path, true);
}
//文件解压缩 是否删除原始文件
public static void gzipDecompress(string file, bool delete){
FileStream fis = new FileStream(file,FileMode.Open);
FileStream fos = new FileStream(file.Replace(".gz", ""),FileMode.OpenOrCreate);
gzipDecompress(fis, fos);
fis.Close();
fos.Flush();
fos.Close();
if (delete) {
File.Delete(file);
}
}
//数据解压缩
public static void gzipDecompress(Stream input, Stream output){
GZipInputStream gis = new GZipInputStream(input);
int count;
byte[] data = new byte[1024];
while ((count = gis.Read(data, 0, 1024)) >0) {
output.Write(data, 0, count);
}
gis.Close();
}