用.net2.0中的using System.IO.Compression;
实现加压和解压的源码。
关于.net2.0中的加压解压类:
GZipStream类表示 GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法。这种格式包括一个检测数据损坏的循环冗余校验值。此外还有另一个加压类DeflateStream;
程序界面:
程序清单:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace zipTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/* 取消 */
private void button4_Click( object sender, EventArgs e)
{
this .Close();
}
/* 确定 */
private void button3_Click( object sender, EventArgs e)
{
bool success = false ; // 默认失败
string err = "" ;
try
{
if (r_compress.Checked == true )
{
success = GzipCompress(t_source.Text, t_dis.Text);
}
if (r_deCompress.Checked == true )
{
success = GzipDecompress(t_source.Text, t_dis.Text);
}
}
catch (Exception ex)
{
err = ex.Message.ToString();
}
if (success == false )
MessageBox.Show( " 操作失败! " + err);
else
MessageBox.Show( " 操作成功! " );
}
/* 选择源文件 */
private void button1_Click( object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
t_source.Text = openFileDialog1.FileName;
}
}
/* 选择目标路径 */
private void button2_Click( object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
t_dis.Text = folderBrowserDialog1.SelectedPath;
}
}
/* 加压 */
public bool GzipCompress( string sourceFile, string disPath)
{
// 路径合法性检测
if ( ! File.Exists(sourceFile))
throw new FileNotFoundException();
if ( ! Directory.Exists(disPath))
throw new DirectoryNotFoundException();
if ( ! disPath.EndsWith( " \\ " ))
disPath += " \\ " ;
// 生成压缩后文件名
string oldName = Path.GetFileNameWithoutExtension(sourceFile);
string newName = oldName + " 压缩文件.gzip " ;
bool result = true ; // 默认成功
FileStream fs1 = null ;
FileStream fs2 = null ;
GZipStream zips = null ;
try
{
fs1 = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
fs2 = new FileStream(disPath + newName, FileMode.OpenOrCreate, FileAccess.Write);
zips = new GZipStream(fs2, CompressionMode.Compress);
byte [] tempb = new byte [fs1.Length];
fs1.Read(tempb, 0 , ( int )fs1.Length);
zips.Write(tempb, 0 , tempb.Length);
}
catch
{
result = false ; // 执行失败
}
finally
{
if (zips != null )
zips.Close();
if (fs1 != null )
fs1.Close();
if (fs2 != null )
fs2.Close();
}
return result;
}
/* 解压 */
public bool GzipDecompress( string sourceFile, string disPath)
{
// 路径合法性检测
if ( ! File.Exists(sourceFile))
throw new FileNotFoundException();
if ( ! Directory.Exists(disPath))
throw new DirectoryNotFoundException();
if ( ! disPath.EndsWith( " \\ " ))
disPath += " \\ " ;
// 生成解压后的文件名
string outFile = disPath + Path.GetFileNameWithoutExtension(sourceFile);
bool result = true ; // 默认成功
FileStream fs1 = null ;
FileStream fs2 = null ;
GZipStream zips = null ;
try
{
fs1 = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
fs2 = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);
zips = new GZipStream(fs1, CompressionMode.Decompress);
byte [] tempb = new byte [ 100 ];
int size = 0 ;
while ( true )
{
size = zips.Read(tempb, 0 , 100 );
if (size > 0 )
fs2.Write(tempb, 0 , size);
else
break ;
}
}
catch
{
result = false ; // 执行失败
}
finally
{
if (zips != null )
zips.Close();
if (fs1 != null )
fs1.Close();
if (fs2 != null )
fs2.Close();
}
return result;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace zipTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/* 取消 */
private void button4_Click( object sender, EventArgs e)
{
this .Close();
}
/* 确定 */
private void button3_Click( object sender, EventArgs e)
{
bool success = false ; // 默认失败
string err = "" ;
try
{
if (r_compress.Checked == true )
{
success = GzipCompress(t_source.Text, t_dis.Text);
}
if (r_deCompress.Checked == true )
{
success = GzipDecompress(t_source.Text, t_dis.Text);
}
}
catch (Exception ex)
{
err = ex.Message.ToString();
}
if (success == false )
MessageBox.Show( " 操作失败! " + err);
else
MessageBox.Show( " 操作成功! " );
}
/* 选择源文件 */
private void button1_Click( object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
t_source.Text = openFileDialog1.FileName;
}
}
/* 选择目标路径 */
private void button2_Click( object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
t_dis.Text = folderBrowserDialog1.SelectedPath;
}
}
/* 加压 */
public bool GzipCompress( string sourceFile, string disPath)
{
// 路径合法性检测
if ( ! File.Exists(sourceFile))
throw new FileNotFoundException();
if ( ! Directory.Exists(disPath))
throw new DirectoryNotFoundException();
if ( ! disPath.EndsWith( " \\ " ))
disPath += " \\ " ;
// 生成压缩后文件名
string oldName = Path.GetFileNameWithoutExtension(sourceFile);
string newName = oldName + " 压缩文件.gzip " ;
bool result = true ; // 默认成功
FileStream fs1 = null ;
FileStream fs2 = null ;
GZipStream zips = null ;
try
{
fs1 = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
fs2 = new FileStream(disPath + newName, FileMode.OpenOrCreate, FileAccess.Write);
zips = new GZipStream(fs2, CompressionMode.Compress);
byte [] tempb = new byte [fs1.Length];
fs1.Read(tempb, 0 , ( int )fs1.Length);
zips.Write(tempb, 0 , tempb.Length);
}
catch
{
result = false ; // 执行失败
}
finally
{
if (zips != null )
zips.Close();
if (fs1 != null )
fs1.Close();
if (fs2 != null )
fs2.Close();
}
return result;
}
/* 解压 */
public bool GzipDecompress( string sourceFile, string disPath)
{
// 路径合法性检测
if ( ! File.Exists(sourceFile))
throw new FileNotFoundException();
if ( ! Directory.Exists(disPath))
throw new DirectoryNotFoundException();
if ( ! disPath.EndsWith( " \\ " ))
disPath += " \\ " ;
// 生成解压后的文件名
string outFile = disPath + Path.GetFileNameWithoutExtension(sourceFile);
bool result = true ; // 默认成功
FileStream fs1 = null ;
FileStream fs2 = null ;
GZipStream zips = null ;
try
{
fs1 = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
fs2 = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);
zips = new GZipStream(fs1, CompressionMode.Decompress);
byte [] tempb = new byte [ 100 ];
int size = 0 ;
while ( true )
{
size = zips.Read(tempb, 0 , 100 );
if (size > 0 )
fs2.Write(tempb, 0 , size);
else
break ;
}
}
catch
{
result = false ; // 执行失败
}
finally
{
if (zips != null )
zips.Close();
if (fs1 != null )
fs1.Close();
if (fs2 != null )
fs2.Close();
}
return result;
}
}
}
这个版本暂时存在一个问题:解压后的文件没有后缀名,需要自己加上。
下一个版本解决此问题。