[csharp] view plaincopyprint?
using Systemusing SystemIOusing SystemDiagnosticsusing MicrosoftWin
using ICSharpCodeSharpZipLibChecksumsusing ICSharpCodeSharpZipLibZip
///压缩解压缩类namespace DotNetUtilities { public class SharpZip { public SharpZip()
{ }
/// <summary> /// 压缩/// </summary> /// <param name=filename> 压缩后的文件名(包含物理路径)</param> /// <param name=directory>待压缩的文件夹(包含物理路径)</param> public static void PackFiles(string filename string directory)
{ try { FastZip fz = new FastZip()fzCreateEmptyDirectories = truefzCreateZip(filename directory true )fz = null} catch (Exception)
{ throw}
/// <summary> /// 解压缩/// </summary> /// <param name=file>待解压文件名(包含物理路径)</param> /// <param name=dir> 解压到哪个目录中(包含物理路径)</param> public static bool UnpackFiles(string file string dir)
{ try { if (!DirectoryExists(dir))
{ DirectoryCreateDirectory(dir)} ZipInputStream s = new ZipInputStream(FileOpenRead(file))ZipEntry theEntrywhile ((theEntry = sGetNextEntry()) != null)
{ string directoryName = PathGetDirectoryName(theEntryName)string fileName = PathGetFileName(theEntryName)if (directoryName != StringEmpty)
{ DirectoryCreateDirectory(dir + directoryName)} if (fileName != StringEmpty)
{ FileStream streamWriter = FileCreate(dir + theEntryName)int size = byte[] data = new byte[]while (true)
{ size = sRead(data dataLength)if (size > )
{ streamWriterWrite(data size)} else { break} streamWriterClose()} sClose()return true} catch (Exception)
{ throw}
public class ClassZip { #region 私有方法/// <summary> /// 递归压缩文件夹方法/// </summary> private static bool ZipFileDictory(string FolderToZip ZipOutputStream s string ParentFolderName)
{ bool res = truestring[] folders filenamesZipEntry entry = nullFileStream fs = nullCrc crc = new Crc()try { entry = new ZipEntry(PathCombine(ParentFolderName PathGetFileName(FolderToZip) + /))sPutNextEntry(entry)sFlush()filenames = DirectoryGetFiles(FolderToZip)foreach (string file in filenames)
{ fs = FileOpenRead(file)byte[] buffer = new byte[fsLength]fsRead(buffer bufferLength)entry = new ZipEntry(PathCombine(ParentFolderName PathGetFileName(FolderToZip) + / + PathGetFileName(file)))entryDateTime = DateTimeNowentrySize = fsLengthfsClose()crcReset()crcUpdate(buffer)entryCrc = crcValuesPutNextEntry(entry)sWrite(buffer bufferLength)} catch { res = false} finally { if (fs != null)
{ fsClose()fs = null} if (entry != null)
{ entry = null} GCCollect()GCCollect()} folders = DirectoryGetDirectories(FolderToZip)foreach (string folder in folders)
{ if (!ZipFileDictory(folder s PathCombine(ParentFolderName PathGetFileName(FolderToZip))))
{ return false} return res}
/// <summary> /// 压缩目录/// </summary> /// <param name=FolderToZip>待压缩的文件夹全路径格式</param> /// <param name=ZipedFile>压缩后的文件名全路径格式</param> private static bool ZipFileDictory(string FolderToZip string ZipedFile int level)
{ bool resif (!DirectoryExists(FolderToZip))
{ return false} ZipOutputStream s = new ZipOutputStream(FileCreate(ZipedFile))sSetLevel(level)res = ZipFileDictory(FolderToZip s )sFinish()sClose()return res}
/// <summary> /// 压缩文件/// </summary> /// <param name=FileToZip>要进行压缩的文件名</param> /// <param name=ZipedFile>压缩后生成的压缩文件名</param> private static bool ZipFile(string FileToZip string ZipedFile int level)
{ if (!FileExists(FileToZip))
{ throw new SystemIOFileNotFoundException(指定要压缩的文件 + FileToZip + 不存在!)} FileStream ZipFile = nullZipOutputStream ZipStream = nullZipEntry ZipEntry = nullbool res = truetry { ZipFile = FileOpenRead(FileToZip)byte[] buffer = new byte[ZipFileLength]ZipFileRead(buffer bufferLength)ZipFileClose()
ZipFile = FileCreate(ZipedFile)ZipStream = new ZipOutputStream(ZipFile)ZipEntry = new ZipEntry(PathGetFileName(FileToZip))ZipStreamPutNextEntry(ZipEntry)ZipStreamSetLevel(level)
ZipStreamWrite(buffer bufferLength)} catch { res = false} finally { if (ZipEntry != null)
{ ZipEntry = null} if (ZipStream != null)
{ ZipStreamFinish()ZipStreamClose()} if (ZipFile != null)
{ ZipFileClose()ZipFile = null} GCCollect()GCCollect()} return res} #endregion
/// <summary> /// 压缩/// </summary> /// <param name=FileToZip>待压缩的文件目录</param> /// <param name=ZipedFile>生成的目标文件</param> /// <param name=level></param> public static bool Zip(String FileToZip String ZipedFile int level)
{ if (DirectoryExists(FileToZip))
{ return ZipFileDictory(FileToZip ZipedFile level)} else if (FileExists(FileToZip))
{ return ZipFile(FileToZip ZipedFile level)} else { return false}
/// <summary> /// 解压/// </summary> /// <param name=FileToUpZip>待解压的文件</param> /// <param name=ZipedFolder>解压目标存放目录</param> public static void UnZip(string FileToUpZip string ZipedFolder)
{ if (!FileExists(FileToUpZip))
{ return} if (!DirectoryExists(ZipedFolder))
{ DirectoryCreateDirectory(ZipedFolder)} ZipInputStream s = nullZipEntry theEntry = nullstring fileNameFileStream streamWriter = nulltry { s = new ZipInputStream(FileOpenRead(FileToUpZip))while ((theEntry = sGetNextEntry()) != null)
{ if (theEntryName != StringEmpty)
{ fileName = PathCombine(ZipedFolder theEntryName)if (fileNameEndsWith(/) || fileNameEndsWith(\\))
{ DirectoryCreateDirectory(fileName)continue} streamWriter = FileCreate(fileName)int size = byte[] data = new byte[]while (true)
{ size = sRead(data dataLength)if (size > )
{ streamWriterWrite(data size)} else { break} finally { if (streamWriter != null)
{ streamWriterClose()streamWriter = null} if (theEntry != null)
{ theEntry = null} if (s != null)
{ sClose()s = null} GCCollect()GCCollect()}
public class ZipHelper { #region 私有变量String the_rarRegistryKey the_RegObject the_ObjString the_InfoProcessStartInfo the_StartInfoProcess the_Process#endregion
/// <summary> /// 压缩/// </summary> /// <param name=zipname>要解压的文件名</param> /// <param name=zippath>要压缩的文件目录</param> /// <param name=dirpath>初始目录</param> public void EnZip(string zipname string zippath string dirpath)
{ try { the_Reg = RegistryClassesRootOpenSubKey(@Applications\WinRARexe\Shell\Open\Command)the_Obj = the_RegGetValue()the_rar = the_ObjToString()the_RegClose()the_rar = the_rarSubstring( the_rarLength )the_Info = a + zipname + + zippaththe_StartInfo = new ProcessStartInfo()the_StartInfoFileName = the_rarthe_StartInfoArguments = the_Infothe_StartInfoWindowStyle = ProcessWindowStyleHiddenthe_StartInfoWorkingDirectory = dirpaththe_Process = new Process()the_ProcessStartInfo = the_StartInfothe_ProcessStart()} catch (Exception ex)
{ throw new Exception(exMessage)}
/// <summary> /// 解压缩/// </summary> /// <param name=zipname>要解压的文件名</param> /// <param name=zippath>要解压的文件路径</param> public void DeZip(string zipname string zippath)
{ try { the_Reg = RegistryClassesRootOpenSubKey(@Applications\WinRarexe\Shell\Open\Command)the_Obj = the_RegGetValue()the_rar = the_ObjToString()the_RegClose()the_rar = the_rarSubstring( the_rarLength )the_Info = X + zipname + + zippaththe_StartInfo = new ProcessStartInfo()the_StartInfoFileName = the_rarthe_StartInfoArguments = the_Infothe_StartInfoWindowStyle = ProcessWindowStyleHiddenthe_Process = new Process()the_ProcessStartInfo = the_StartInfothe_ProcessStart()} catch (Exception ex)
{ throw new Exception(exMessage)}