📄 gziphelper.cs
字号:
namespace Imps.Utils
{
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public class GZipHelper
{
private static Encoding encode = Encoding.UTF8;
public static void CompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile))
{
throw new FileNotFoundException();
}
byte[] buffer = null;
FileStream stream = null;
FileStream stream2 = null;
GZipStream stream3 = null;
try
{
stream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[stream.Length];
if (stream.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new ApplicationException();
}
stream2 = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);
stream3 = new GZipStream(stream2, 1, true);
stream3.Write(buffer, 0, buffer.Length);
}
finally
{
if (stream != null)
{
stream.Close();
}
if (stream3 != null)
{
stream3.Close();
}
if (stream2 != null)
{
stream2.Close();
}
}
}
public static string CompressString(string input)
{
if (input.Length == 0)
{
return string.Empty;
}
string text = string.Empty;
MemoryStream stream = null;
GZipStream stream2 = null;
try
{
byte[] bytes = encode.GetBytes(input);
stream = new MemoryStream();
using (stream2 = new GZipStream(stream, 1, true))
{
stream2.Write(bytes, 0, bytes.Length);
}
text = Convert.ToBase64String(stream.ToArray());
}
catch
{
}
finally
{
if (stream != null)
{
stream.Dispose();
}
if (stream2 != null)
{
stream2.Dispose();
}
}
return text;
}
public static void DecompressFile(string sourceFile, string destinationFile)
{
if (!File.Exists(sourceFile))
{
throw new FileNotFoundException();
}
FileStream stream = null;
FileStream stream2 = null;
GZipStream stream3 = null;
byte[] buffer = null;
try
{
stream = new FileStream(sourceFile, FileMode.Open);
stream3 = new GZipStream(stream, 0, true);
buffer = new byte[4];
int num = ((int) stream.Length) - 4;
stream.Position = num;
stream.Read(buffer, 0, 4);
stream.Position = 0;
byte[] buffer2 = new byte[BitConverter.ToInt32(buffer, 0) + 100];
int offset = 0;
int count = 0;
while (true)
{
int num5 = stream3.Read(buffer2, offset, 100);
if (num5 == 0)
{
break;
}
offset += num5;
count += num5;
}
stream2 = new FileStream(destinationFile, FileMode.Create);
stream2.Write(buffer2, 0, count);
stream2.Flush();
}
finally
{
if (stream != null)
{
stream.Close();
}
if (stream3 != null)
{
stream3.Close();
}
if (stream2 != null)
{
stream2.Close();
}
}
}
public static string DecompressString(string base64String)
{
if (base64String.Length == 0)
{
return string.Empty;
}
string text = string.Empty;
try
{
byte[] buffer = Convert.FromBase64String(base64String);
int count = BitConverter.ToInt32(new byte[] { buffer[buffer.Length - 4], buffer[buffer.Length - 3], buffer[buffer.Length - 2], buffer[buffer.Length - 1] }, 0);
using (MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length))
{
using (GZipStream stream2 = new GZipStream(stream, 0))
{
byte[] buffer3 = new byte[count];
stream2.Read(buffer3, 0, count);
return encode.GetString(buffer3);
}
}
}
catch
{
}
return text;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -