c#的文件压缩
来源:互联网 发布:snmp出口流量监控软件 编辑:程序博客网 时间:2024/04/28 01:24
使用
ICSharpCode.SharpZipLib.dll
下载地址:
http://www.icsharpcode.net/OpenSource/SharpZipLib/
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
代码:
压缩到文件
private void btnZip_Click(object sender, System.EventArgs e)
{
string filename;
ZipOutputStream zos;
FileStream fs;
Crc32 crc = new Crc32();
OpenFileDialog openfileDialog = new OpenFileDialog();
//openfileDialog.InitialDirectory = "c://" ;
openfileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openfileDialog.FilterIndex = 2 ;
openfileDialog.RestoreDirectory = false ;
if(openfileDialog.ShowDialog() == DialogResult.OK)
{
filename = Path.GetFileName(openfileDialog.FileName);
zos = new ZipOutputStream(File.Create(filename + ".zip"));
zos.SetLevel(6);
fs = File.OpenRead(filename);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
this.richTextBox1.Text=Encoding.UTF8.GetString(buffer);
ZipEntry entry = new ZipEntry(filename);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
zos.Finish();
zos.Close();
}
}
压缩到内存
private void btnZipOneText_Click(object sender, System.EventArgs e)
{
msFileIn = new MemoryStream();
//this.statusBarPanel1.Text="正在压缩编码第一个文本框里的数据 ... ...";
byte[] getchr = Encoding.UTF8.GetBytes(this.richTextBox1.Text.ToString());
ZipOutputStream zos = new ZipOutputStream(msFileIn);
zos.SetLevel(6);
ZipEntry entry = new ZipEntry("Stream");
Crc32 crc = new Crc32();
crc.Reset();
crc.Update(getchr);
entry.Crc = crc.Value;
entry.DateTime = DateTime.Now;
entry.Size = getchr.Length;
zos.PutNextEntry(entry);
zos.Write(getchr,0,getchr.Length);
zos.Finish();
zos.Close();
MessageBox.Show("压缩完成,结果存入内存!");
//this.statusBarPanel1.Text = "第一个文本框里的数据压缩完成。";
}
解压文件
private void btnUnZip_Click(object sender, System.EventArgs e)
{
string filename;
OpenFileDialog openfileDialog = new OpenFileDialog();
//openfileDialog.InitialDirectory = "c://" ;
openfileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openfileDialog.FilterIndex = 2 ;
openfileDialog.RestoreDirectory = false ;
if(openfileDialog.ShowDialog() == DialogResult.OK)
{
filename = Path.GetFileName(openfileDialog.FileName);
ZipInputStream zis = new ZipInputStream(File.OpenRead(filename));
ZipEntry theEntry;
while ((theEntry = zis.GetNextEntry()) != null)
{
//string directoryName = Path.GetDirectoryName(theEntry.Name);
//Directory.CreateDirectory(directoryName);
string fileName = Path.GetFileName(theEntry.Name);
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(theEntry.Name);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zis.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
zis.Close();
}
}
- c#的文件压缩
- C#实现文件的压缩和解压缩
- C#实现文件的压缩和解压缩
- C# dataset 压缩,文件压缩
- C#利用SharpZipLib进行文件的压缩和解压缩
- C# ZIP文件的压缩和解压缩(SharpZipLib.dll)
- C#封装的文件压缩和解压缩类
- c#压缩ACCESS文件
- C# 压缩、解压缩文件
- C# 压缩 ZIP文件
- c# 压缩解压文件
- C# 压缩、解压缩文件
- C# 压缩解压文件
- c# 压缩解压文件
- C#压缩打包文件
- C# 压缩、解压缩文件
- C# 文件压缩下载
- C# 文件压缩
- 历届图灵奖获得者最新名单
- 软件开发误区之五-归根结底
- 最简单的方法实现ORACLE的定时备份
- inverters
- office 办公软件中利用VBA(Visual Basic for Application)宏
- c#的文件压缩
- C#调用API
- 学习小结(2005-2-22)
- 短信开发相关技术
- APCCAL bit
- 什么东东
- SPS 2003 的10大优点(收录)
- Windows服务程序
- 心情日记