/// <summary>
/// 文件流压缩解压
/// </summary>
public class ziphelper
{
public static int best_compression = 9;
public static int best_speed = 1;
public static int default_compression = -1;
public static int no_compression = 0;
#region deflate压缩
#region deflate压缩
/// <summary>
/// deflate方式压缩(默认压缩级别最高)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static stream deflate(stream stream)
{
return ziphelper.deflate(stream, ziphelper.default_compression);
}
/// <summary>
/// deflate方式压缩
/// </summary>
/// <param name="stream"></param>
/// <param name="level">压缩品质级别(0~9)</param>
/// <returns></returns>
public static stream deflate(stream stream, int level)
{
byte[] array = ziphelper.streamtobytes(stream);
byte[] array2 = new byte[array.length];
deflater deflater = new deflater();
deflater.setlevel(level);
deflater.setstrategy(deflatestrategy.default);
deflater.setinput(array);
deflater.finish();
int num = deflater.deflate(array2);
byte[] array3 = new byte[num];
array.copy(array2, array3, num);
return ziphelper.bytestostream(array3);
}
/// <summary>
/// deflate方式压缩
/// </summary>
/// <param name="input"></param>
/// <param name="level">压缩品质级别(0~9)</param>
/// <returns></returns>
public static byte[] deflate(byte[] input, int level)
{
byte[] result;
try
{
if (input == null && input.length == 0)
{
result = new byte[0];
}
else
{
byte[] array = new byte[input.length];
deflater deflater = new deflater();
deflater.setlevel(level);
deflater.setstrategy(deflatestrategy.default);
deflater.setinput(input);
deflater.finish();
int num = deflater.deflate(array);
byte[] array2 = new byte[num];
array.copy(array, array2, num);
result = array2;
}
}
catch (exception innerexception)
{
throw new exception("压缩程序出错!", innerexception);
}
return result;
}
#endregion
#region inflate解压
/// <summary>
/// inflate解压
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static byte[] inflate(byte[] input)
{
byte[] result;
try
{
if (input == null && input.length == 0)
{
result = new byte[0];
}
else
{
inflater inflater = new inflater();
inflater.setinput(input);
byte[] array = new byte[1024];
using (memorystream memorystream = new memorystream())
{
for (int i = inflater.inflate(array, 0, array.length); i > 0; i = inflater.inflate(array, 0, array.length))
{
memorystream.write(array, 0, i);
}
byte[] buffer = memorystream.getbuffer();
memorystream.close();
result = buffer;
}
}
}
catch (exception innerexception)
{
throw new exception("解压缩程序出错!", innerexception);
}
return result;
}
/// <summary>
/// inflate解压
/// </summary>
/// <param name="zipstream"></param>
/// <returns></returns>
public static stream inflate(stream zipstream)
{
byte[] input = ziphelper.streamtobytes(zipstream);
byte[] bytes = ziphelper.inflate(input);
return ziphelper.bytestostream(bytes);
}
#endregion
#endregion
#region gzip压缩
/// <summary>
/// gzip压缩
/// </summary>
/// <param name="srcstream"></param>
/// <param name="output"></param>
public static void gzipcompress(stream srcstream, stream output)
{
ziphelper.gzipcompress(srcstream, 6, output);
}
/// <summary>
/// gzip压缩
/// </summary>
/// <param name="srcstream"></param>
/// <param name="compresslevel">压缩品质级别(0~9)</param>
/// <param name="output"></param>
public static void gzipcompress(stream srcstream, int compresslevel, stream output)
{
if (compresslevel < 1 || compresslevel > 9)
{
throw new exception(string.format("您指定的压缩级别 {0} 不在有效的范围(1-9)内", compresslevel));
}
srcstream.position = 0l;
gzipoutputstream gzipoutputstream = new gzipoutputstream(output);
gzipoutputstream.setlevel(compresslevel);
try
{
int i = 4096;
byte[] buffer = new byte[i];
while (i > 0)
{
i = srcstream.read(buffer, 0, i);
gzipoutputstream.write(buffer, 0, i);
}
}
catch (exception ex)
{
throw new exception("gzip压缩出错:" + ex.message);
}
srcstream.close();
gzipoutputstream.finish();
}
/// <summary>
/// gzip解压
/// </summary>
/// <param name="zipstream"></param>
/// <param name="outputstream"></param>
public static void gzipdecompress(stream zipstream, stream outputstream)
{
gzipinputstream gzipinputstream = new gzipinputstream(zipstream);
try
{
int i = 4096;
byte[] buffer = new byte[i];
while (i > 0)
{
i = gzipinputstream.read(buffer, 0, i);
outputstream.write(buffer, 0, i);
}
}
catch (exception ex)
{
throw new exception("gzip解压缩出错:" + ex.message);
}
zipstream.close();
gzipinputstream.close();
}
#endregion
#region bzip2压缩
/// <summary>
/// bzip2压缩
/// </summary>
/// <param name="instream"></param>
/// <param name="outstream"></param>
/// <param name="blocksize"></param>
public static void bzip2compress(stream instream, stream outstream, int blocksize)
{
bzip2.compress(instream, outstream, blocksize);
}
/// <summary>
/// bzip2解压
/// </summary>
/// <param name="instream"></param>
/// <param name="outstream"></param>
public static void bzip2decompress(stream instream, stream outstream)
{
bzip2.decompress(instream, outstream);
}
#endregion
private static byte[] streamtobytes(stream stream)
{
byte[] array = new byte[stream.length];
stream.seek(0l, seekorigin.begin);
stream.read(array, 0, array.length);
stream.close();
return array;
}
private static stream bytestostream(byte[] bytes)
{
return new memorystream(bytes);
}
private static void streamtofile(stream stream, string filename)
{
byte[] array = new byte[stream.length];
stream.read(array, 0, array.length);
stream.seek(0l, seekorigin.begin);
filestream filestream = new filestream(filename, filemode.create);
binarywriter binarywriter = new binarywriter(filestream);
binarywriter.write(array);
binarywriter.close();
filestream.close();
}
private static stream filetostream(string filename)
{
filestream filestream = new filestream(filename, filemode.open, fileaccess.read, fileshare.read);
byte[] array = new byte[filestream.length];
filestream.read(array, 0, array.length);
filestream.close();
return new memorystream(array);
}
}
以上就是c# 文件流压缩解压的内容。
