/// <summary> /// 创建一个压缩文件 /// </summary> /// <param name="zipFilename">压缩后的文件名</param> /// <param name="sourceDirectory">待压缩文件的所在目录</param> public static void PackFiles(string zipFilename,string sourceDirectory) { FastZip fz = new FastZip() ; fz.CreateEmptyDirectories = true ; fz.CreateZip(zipFilename,sourceDirectory,true,"") ; fz = null ; }
/// <summary> /// 解压缩文件 /// </summary> /// <param name="zipFile">待解压缩的文件</param> /// <param name="directory">解压缩后文件存放的目录</param> public static bool UnpackFiles(string zipFile,string directory) { if( !Directory.Exists(directory) ) Directory.CreateDirectory(directory) ;
ZipInputStream zis = new ZipInputStream( File.OpenRead(zipFile) ) ; ZipEntry theEntry = null ; while( (theEntry = zis.GetNextEntry()) != null ) { string directoryName = Path.GetDirectoryName(theEntry.Name) ; string fileName = Path.GetFileName(theEntry.Name) ; if( directoryName != string.Empty ) Directory.CreateDirectory(directory + directoryName) ;
if( fileName != string.Empty ) { FileStream streamWriter = File.Create( Path.Combine( directory,theEntry.Name) ) ; int size = 2048 ; byte[] data = new byte[size] ; while ( true ) { size = zis.Read(data,0,data.Length) ; if( size > 0 ) streamWriter.Write( data,0,size ) ; else break ; }
streamWriter.Close() ; } }
zis.Close() ; return true ; } }最后别忘了using ICSharpCode.SharpZipLib.Zip ;以上代码参照 利用 SharpZipLib方便地压缩和解压缩文件 而做了小修改
另外一种方法使用Microsoft J# 类库,具体参考MSDN文章:通过 C# 使用 J# 类库中的 Zip 类压缩文件和数据 本文首发于http://www.365keyi.com/article.asp?id=5
转载于:https://www.cnblogs.com/xiaozhang/archive/2006/06/05/417964.html
相关资源:c# 解压缩文件或文件夹代码 可设置压缩密码