C# WINFORM 压缩文件问题??!!

ivanho 2011-01-12 04:09:07
情况是这样的,我想把程序文件夹里的文件进行.zip压缩.但文件夹里有一个子文件夹,我不想.zip也把它压在里边,不知那个方法如何写!!!???

我现在是写成这样的:


using System;
using System.Data;
using System.Configuration;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;


namespace VEH.UPDATE
{
public class UPZIP
{
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(6); // 0 - store only to 9 - means best compression
zip(strFile, s, strFile);
s.Finish();
s.Close();


}


private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{

if (Directory.Exists(file))
{
zip(file, s, staticFile);
}

else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);

entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
}




#region 递归压缩文件夹方法

/// <summary>
/// 递归压缩文件夹方法
/// </summary>
/// <param name="FolderToZip"></param>
/// <param name="s"></param>
/// <param name="ParentFolderName"></param>
private bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();

try
{

//创建当前文件夹
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建
s.PutNextEntry(entry);
s.Flush();


//先压缩文件,再递归压缩文件夹
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
//打开压缩文件
fs = File.OpenRead(file);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));

entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();

crc.Reset();
crc.Update(buffer);

entry.Crc = crc.Value;

s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}


folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
}

return res;
}

/// <summary>
/// 压缩目录
/// </summary>
/// <param name="FolderToZip">待压缩的文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后的文件名,全路径格式</param>
/// <returns></returns>
public bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
{
bool res;
if (!Directory.Exists(FolderToZip))
{
return false;
}

ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
s.SetLevel(6);
s.Password = Password;

res = ZipFileDictory(FolderToZip, s, "");

s.Finish();
s.Close();

return res;
}

#endregion

#region 压缩 指定文件
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名</param>
/// <returns></returns>
private bool ZipFile(string FileToZip, string ZipedFile, String Password)
{
//如果文件没有找到,则报错
if (!File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
}
//FileStream fs = null;
FileStream ZipFile = null;
ZipOutputStream ZipStream = null;
ZipEntry ZipEntry = null;

bool res = true;
try
{
ZipFile = File.OpenRead(FileToZip);
byte[] buffer = new byte[ZipFile.Length];
ZipFile.Read(buffer, 0, buffer.Length);
ZipFile.Close();

ZipFile = File.Create(ZipedFile);
ZipStream = new ZipOutputStream(ZipFile);
ZipStream.Password = Password;
ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);

ZipStream.Write(buffer, 0, buffer.Length);
}
catch
{
res = false;
}
finally
{
if (ZipEntry != null)
{
ZipEntry = null;
}
if (ZipStream != null)
{
ZipStream.Finish();
ZipStream.Close();
}
if (ZipFile != null)
{
ZipFile.Close();
ZipFile = null;
}
GC.Collect();
GC.Collect(1);
}

return res;
}

/// <summary>
/// 压缩文件 和 文件夹
/// </summary>
/// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
/// <returns></returns>
public bool Zip(String FileToZip, String ZipedFile, String Password)
{
if (Directory.Exists(FileToZip))
{
return ZipFileDictory(FileToZip, ZipedFile, Password);
}
else if (File.Exists(FileToZip))
{
return ZipFile(FileToZip, ZipedFile, Password);
}
else
{
return false;
}
}

#endregion

}
}




望高手们帮帮们!!!
...全文
144 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuld 2011-01-12
  • 打赏
  • 举报
回复
C#常用代码集合库 - 功能完整|效率高速|文档丰富

含 ZIP 功能

using(ZipFile zip = new ZipFile()){
zip.Open("H:\\sav.zip");
zip.AddFile("H:\\d.dat");
zip.Extract(); // 解压
zip.Save(); // 保存
}
xuld 2011-01-12
  • 打赏
  • 举报
回复
C#常用代码集合库[下载] - 功能完整|效率高速|文档丰富

含 ZIP 功能



using(ZipFile zip = new ZipFile()){
zip.Open("H:\\sav.zip");
zip.AddFile("H:\\d.dat");
zip.Extract(); // 解压
zip.Save(); // 保存
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧