熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net 壓縮解壓縮zip文件

2022-06-13   來源: .NET編程 

  [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\Commandthe_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\Commandthe_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)}


From:http://tw.wingwit.com/Article/program/net/201311/12665.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.