今天头铁用System.IO.Compression类来写一下文件的Gzip压缩,结果你懂的。。。。。。。。(给自己整晕了)
主要是压缩之后我发现是有内容的,又想着写一下解压部分,结果要么溢出,要么解压成功后得到一个啥也没有的空壳。下面我给大家分享一下压缩部分吧(我觉得应该也是有问题的,因为他有内容但是明显不够,纯属个人看法)。后面解压部分我也放在这里,如果有要尝试的小伙伴搞出来了就给我指点指点,不胜感激。
——————————————————
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 文件压缩
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public FileStream stream1 { get; set; }
public FileStream stream2 { get; set; }
public GZipStream gZip { get; set; }
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("请选择要压缩的文件", "提示");
return;
}
if (string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("请输入压缩文件名", "提示");
}
string str1 = textBox1.Text; //获取文件路径
string str2 = textBox2.Text.Trim() + ".gzip"; //获取压缩文件名
try
{
FileStream stream1 = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] strByte = new byte[stream1.Length]; //根据源文件流创建缓冲区
stream1.Read(strByte, 0, strByte.Length); //把源文件的文件流写入缓冲区
FileStream stream2 = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);//根据压缩文件的文件流生成压缩流
GZipStream gZip = new GZipStream(stream2, CompressionMode.Compress, true);
gZip.Write(strByte, 0, strByte.Length); //把源文件的文件流写入压缩流
MessageBox.Show("压缩文件完成!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (stream1 != null)
{
stream1.Close();
stream2.Close();
gZip.Close();
}
}
}
#region 打开文件选择对话框
/// <summary>
/// 打开文件选择对话框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFile.FileName;
}
}
#endregion
private void label2_Click(object sender, EventArgs e)
{
}
#region 选择要解压的文件
private void 解压_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
textBox3.Text = openFile.FileName;
}
}
#endregion
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("请选择要解压的文件", "提示");
return;
}
string DisCompressFileName = textBox3.Text;
try
{
FileStream mystream = new FileStream(DisCompressFileName, FileMode.Open);//获得gzip文件的文件流
byte[] mybyte = new byte[mystream.Length];
int myLength = BitConverter.ToInt32(mybyte, 0);
mystream.Read(mybyte, 0, myLength);
FileStream myDesStream = new FileStream(textBox4.Text, FileMode.Create);
GZipStream gZipDisCompress = new GZipStream(mystream, CompressionMode.Decompress, true);
myDesStream.Write(mybyte, 0, myLength);
myDesStream.Flush();
MessageBox.Show("解压文件完成!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (stream1 != null)
{
stream1.Close();
stream2.Close();
gZip.Close();
}
this.Dispose();
}
}
}
}
————————————
这是一个失败的案例。。。。
所以得出结论:这种方法太扯,强烈建议用
ICSharpCode.SharpZipLib
这个类库来做很简便了,也容易理解。
我去查到有篇文章写的不错,应该是很有帮助的。
补充说明一下,我用上面这篇博客代码写出来的结果是一样的,都是压缩有东西,解压为空壳。个人觉得是解压后的文件字节太多,解压会超过定义的范围,就是压缩内容是比原内容小的,不好去界定到底要多大空间,应该是需要自己设计一种算法去解决的。(我的认知有限,不知道说得对不对,先挖个坑以后再填)