创建zip文件. (ICSharpCode.SharpZipLib)
时间:2010-09-09 来源:无尽思绪
两种方式,但其实还是一种方式:
1.依托于DNN来创建,使用的是DNN自带的SharpZipLib.dll文件。
/// <summary>
/// using DNN way for create zip file
/// </summary>
/// <param name="fileName"></param>
/// <param name="files"></param>
/// <returns></returns>
public static bool CreateZipFile2(string fileName, LiftEngineDocumentCollection files)
{
bool isCreated = false;
int level = 9;
string path = fileName;
FileStream baseOutputStream = null;
try
{
baseOutputStream = File.Create(path);
ZipOutputStream zipFile = null;
try
{
zipFile = new ZipOutputStream(baseOutputStream);
zipFile.SetLevel(level);
try
{
foreach (LiftEngineDocumentEntity entity in files)
{
string file = Path.GetFullPath(HttpRuntime.AppDomainAppPath + entity.FullPath);
if (File.Exists(file))
{
FileSystemUtils.AddToZip(ref zipFile, file, Path.GetFileName(file), "");
}
}
isCreated = true;
}
finally
{
}
}
catch (Exception exception1)
{
Exception exc = exception1;
DotNetNuke.Services.Exceptions.Exceptions.LogException(exception1);
isCreated = false;
}
finally
{
if (zipFile != null)
{
zipFile.Finish();
zipFile.Close();
}
}
}
catch (Exception exception3)
{
Exception exception2 = exception3;
DotNetNuke.Services.Exceptions.Exceptions.LogException(exception2);
isCreated = false;
}
finally
{
if (baseOutputStream != null)
{
baseOutputStream.Close();
}
}
return isCreated;
}
其中的LiftEngineDocumentCollection 是一个文件集合。使用的是LLBLGen来做DAL层。
参考代码:DotNetNuke.Entities.Modules.PaWriter.CreateZipFile()
第二种方式:使用的是ICSharpCode.SharpZipLib.dll文件,这个文件相对于DNN自带的要新一些。
/// <summary>
/// A method that creates a zip file
/// </summary>
/// <param name="zipFileStoragePath">the storage location</param>
/// <param name="zipFileName">the zip file name</param>
/// <param name="fileToCompress">the file to compress</param>
private void CreateZipFile(string zipFileStoragePath
, string zipFileName
, string fileToCompress)
{
//create our zip file
ZipFile z = ZipFile.Create(zipFileStoragePath + zipFileName);
//initialize the file so that it can accept updates
z.BeginUpdate();
//add the file to the zip file
z.Add(fileToCompress);
//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();
}
这种方式有个问题,创建zip文件后,会带有目录。
参考:
http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx
http://codecruncher.blogsome.com/2007/01/04/37/
相关阅读 更多 +










