用SharpZipLib中的组件进行压缩和解压缩
来源:互联网 发布:电话爆破软件 编辑:程序博客网 时间:2024/04/27 17:16
近来做了一个小程序,其中用到了压缩与解压缩,在网上找到的都是些怪码,很是郁闷.现在我把自己的源码写出下来与大家分享!
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
using System.IO;
记得不要忘了把在项目里引用sharpziplib.dll (添加--引用---确定)
我用的是sharpziplib.dll 中的2.0
下面是压缩代码:
this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip";
this.saveFileDialog1.Title = "保存文件";
this.saveFileDialog1.FileName = "数据备份";
this.saveFileDialog1.InitialDirectory = Application.StartupPath;
this.saveFileDialog1.RestoreDirectory = true;
string Fname = "";
if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] filenames ={ "image", "Company.xml", "config.xml", "Room.xml", "RoomMaintain.xml", "RoomRent.xml", "UserKey.xml" };//可以自己定义所压缩的文件
Fname = this.saveFileDialog1.FileName;
ZipOutputStream s = new ZipOutputStream(File.Create(Fname));//创建压缩文件的目录及名称
s.Password = "jiang";//设置压缩文件的密码
s.SetLevel(6);// 0 - store only to 9 - means best compression
foreach (string file in filenames)
{
if (Directory.Exists(Application.StartupPath + @"/" + file))
{
strBaseDir = Application.StartupPath + @"/" + file + @"/";//压缩文件夹
addZipEntry(strBaseDir,s);
}
else
{
FileStream fs = File.OpenRead(Application.StartupPath + @"/" + file);//打开文件读取
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = file;
ZipEntry entry = new ZipEntry(strEntryName);
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
s.Finish();
s.Close();
}
private void addZipEntry(string PathStr,ZipOutputStream s)//压缩文件夹及内容
{
DirectoryInfo di = new DirectoryInfo(PathStr);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName,s);
}
foreach (FileInfo item in di.GetFiles())
{
if (item.FullName.Replace(strBaseDir, "") != "Thumbs.db")
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = item.FullName.Replace(strBaseDir, "");
strEntryName = @"image/" + strEntryName;
ZipEntry entry = new ZipEntry(strEntryName);
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
}
下面代码是解压缩
this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip";
this.openFileDialog1.Title = "打开文件";
this.openFileDialog1.FileName = "数据备份";
this.openFileDialog1.InitialDirectory = Application.StartupPath;
this.openFileDialog1.RestoreDirectory = true;
string Fname = "";
if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Fname = this.openFileDialog1.FileName;
Unzip(Fname, Application.StartupPath);
}
private void Unzip(string zipfile, string directory)//解压缩
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
ZipInputStream zis = new ZipInputStream(File.OpenRead(zipfile));
string str = zis.Password;
zis.Password = "jiang";//解压密码
ZipEntry theEntry = null;
while ((theEntry = zis.GetNextEntry()) != null)
{
ZipConstants.DefaultCodePage = 936;
string directoryName = Path.GetDirectoryName(theEntry.Name);//directoryName=null
string fileName = Path.GetFileName(theEntry.Name);
//创建文件
if (directoryName != string.Empty)
{
if ((directoryName == "image") && (!Directory.Exists(directory + @"/" + directoryName)))
{
Directory.CreateDirectory(directory + @"/" + directoryName);
}
}
if (fileName != string.Empty)
{
FileStream streamWriter = File.Create(Path.Combine(directory, 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();
}
现在压缩与解压缩都完成了,并且还带了压缩密码哦!真的很不错,如有问题,请大家告诉我哦!^^!
http://api.dllzj.com/dll/CSharpZipLib.zip
- 用SharpZipLib中的组件进行压缩和解压缩
- 用SharpZipLib中的组件进行压缩和解压缩
- 利用SharpZipLib进行压缩和解压缩
- 利用SharpZipLib进行文件的压缩和解压缩
- 利用SharpZipLib进行文件的压缩和解压缩
- 利用SharpZipLib进行文件的压缩和解压缩
- C#利用SharpZipLib进行文件的压缩和解压缩
- 用ICSharpCode.SharpZipLib进行压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- [转贴]在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩(转)
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- 使用Jquery应该注意的
- Matlab数据导入方法
- 字符,字节和编码
- C语言编写Socket程序---入门篇
- (转载)myeclipse加速启动设置
- 用SharpZipLib中的组件进行压缩和解压缩
- 智能卡技术减轻护理和急救人员工作
- js根据text选中select
- JavaScript DateTime格式化
- PB窗口之间传递多参数的方法
- 在Ubuntu 11.04 上安装Xilinx ISE 13.1指南,附出错的改正方法
- Armitage pentest
- C/C++内存与运行时深入研究
- Android Activity 测试