本文是我自己在写功能的时候用到,在网上查看到别人的,所以转载过来,再添点我自己的理解备忘。原文链接:http://www.cnblogs.com/GoCircle/p/6544678.html。
这里利用了一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll,下载地址:http://download.csdn.net/detail/cleopard/8304539,下载完成后将这个dll引用进来,然后再代码的开头要声明一下:
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
其中ICSharpCode是引用文件夹中存放dll的路径。
压缩文件以及文件夹为zip代码如下:
/// <summary>
/// ファイルをZipする
/// </summary>
/// <param name="filesName">ファイル名</param>
/// <param name="ZipedFileName">Zipファイル名</param>
/// <returns></returns>
public static bool Zip(string[] filesName, string ZipedFileName,string kbn)
{
//有効ファイルの再配列
filesName = filesName.Where(f => File.Exists(f) || Directory.Exists(f)).ToArray();
//有効ファイルがある場合、ジップを行う
if (filesName.Length != 0)
{
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFileName));
s.SetLevel(6);
if (kbn == "PC")
{
//ジップファイル作成
ZipFileDictory(filesName, s, "/");
}
else
{
//ジップファイル作成
ZipFileDictory(filesName, s, "/");
}
s.Finish();
s.Close();
return true;
}
else
{
MessageBox.Show(Msg.msgNoEffectiveFile);
return false;
}
}
private static void ZipFileDictory(string[] filesName, ZipOutputStream s, string folderName,string kbn)
{
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
int projNums = 1;
int dirNums = 1;
int boardNums = 1;
for (int i = 0; i < filesName.Length; i++)
{
//ファイルのしょの所属フォルダ作成
string file = filesName[i];
entry = new ZipEntry(folderName);
s.PutNextEntry(entry);
s.Flush();
//該当ファイル名がファイルかフォルダかを判断する
if (Directory.Exists(file))
{
//フォルダの場合
string tmpFilePath = file;
//黒板及び黒板画像を取得する
string[] tmpFileName = Directory.GetFileSystemEntries(tmpFilePath, "BD*");
if (kbn == "PC")
{
if (!tmpFilePath[tmpFilePath.Length - 1].Equals("/"))
{
tmpFilePath = (tmpFilePath + "/").Replace("\\", "/".ToString());
}
tmpFilePath = tmpFilePath.Substring(Para.path.Replace("\\", "/".ToString()).Length + 1);
}
else
{
if (!tmpFilePath[tmpFilePath.Length - 1].Equals("/"))
{
tmpFilePath = tmpFilePath.Replace("\\", "/".ToString());
}
if (dirNums < 10)
{
tmpFilePath = "project" + dirNums.ToString("00") + "/";
dirNums++;
}
else
{
tmpFilePath = "project" + dirNums.ToString() + "/";
dirNums++;
}
}
tmpFileName = GetCompleteBD(tmpFileName);
tmpFileName = tmpFileName.Where(f => File.Exists(f)).ToArray();
if (tmpFileName.Length > 0)
{
if (kbn == "PC")
{
//黒板がある場合、再帰を呼び出す
ZipFileDictory(tmpFileName, s, tmpFilePath,"PC");
}
else
{
//黒板がある場合、再帰を呼び出す
ZipFileDictory(tmpFileName, s, tmpFilePath, "IPAD");
}
}
else
{
entry = new ZipEntry(tmpFilePath);
s.PutNextEntry(entry);
s.Flush();
}
}
else
{
string tempFile = string.Empty;
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
tempFile = Path.GetFileName(file);
if (kbn == "IPAD")
{
if (tempFile.Contains("project"))
{
if (projNums < 10)
{
tempFile = "project" + projNums.ToString("00") + ".xml";
}
else
{
tempFile = "project" + projNums.ToString() + ".xml";
}
projNums++;
}
else if (tempFile.Contains("BD"))
{
if (boardNums < 10000)
{
tempFile = "BD" + boardNums.ToString("0000") + ".xml";
}
else
{
tempFile = "BD" + boardNums.ToString() + ".xml";
}
boardNums++;
}
}
if (!folderName.Equals("/"))
{
tempFile = folderName + tempFile;
}
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);
}
}
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
}
}
解压zip文件的代码如下,解压文件fileToUnzip到zipFolder路径下:
public static bool UnZip(string fileToUnZip, string zipedFolder)
{
bool result = true;
FileStream fs = null;
ZipInputStream zipStream = null;
ZipEntry ent = null;
string fileName;
if (!File.Exists(fileToUnZip))
return false;
if (!Directory.Exists(zipedFolder))
Directory.CreateDirectory(zipedFolder);
try
{
zipStream = new ZipInputStream(File.OpenRead(fileToUnZip));
while ((ent = zipStream.GetNextEntry()) != null)
{
if (!string.IsNullOrEmpty(ent.Name))
{
fileName = Path.Combine(zipedFolder, ent.Name);
fileName = fileName.Replace('/', '\\');
if (fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
}
fs = File.Create(fileName);
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
fs.Write(data, 0, size);
else
break;
}
fs.Close();
}
}
}
catch
{
result = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (zipStream != null)
{
zipStream.Close();
zipStream.Dispose();
}
if (ent != null)
{
ent = null;
}
GC.Collect();
GC.Collect(1);
}
return result;
}