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

文件夾壓縮到zip以及解壓

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

  需要用到SharpLibZip代碼如下

  Zipcs

  public class Zip

  {

  /// <summary>

  /// 壓縮文件夾

  /// </summary>

  /// <param name=dirPath>壓縮文件夾的路徑</param>

  /// <param name=fileName>生成的zip文件路徑</param>

  /// <param name=level>壓縮級別 是存儲級別 是最大壓縮</param>

  /// <param name=bufferSize>讀取文件的緩沖區大小</param>

  public void CompressDirectory(string dirPathstring fileNameint levelint bufferSize)

  {

  byte[] buffer = new byte[bufferSize];

  using (ZipOutputStream s = new ZipOutputStream(FileCreate(fileName)))

  {

  sSetLevel(level);

  CompressDirectory(dirPath dirPath s buffer);

  sFinish();

  sClose();

  }

  }

  /// <summary>

  /// 壓縮文件夾

  /// </summary>

  /// <param name=root>壓縮文件夾路徑</param>

  /// <param name=path>壓縮文件夾內當前要壓縮的文件夾路徑</param>

  /// <param name=s></param>

  /// <param name=buffer>讀取文件的緩沖區大小</param>

  private void CompressDirectory(string root string path ZipOutputStream s byte[] buffer)

  {

  root = rootTrimEnd(\\) + \\;

  string[] fileNames = DirectoryGetFiles(path);

  string[] dirNames = DirectoryGetDirectories(path);

  string relativePath = pathReplace(root );

  if (relativePath != )

  {

  relativePath = relativePathReplace(\\ /) + /;

  }

  int sourceBytes;

  foreach (string file in fileNames)

  {

  ZipEntry entry = new ZipEntry(relativePath + PathGetFileName(file));

  entryDateTime = DateTimeNow;

  sPutNextEntry(entry);

  using (FileStream fs = FileOpenRead(file))

  {

  do

  {

  sourceBytes = fsRead(buffer bufferLength);

  sWrite(buffer sourceBytes);

  } while (sourceBytes > );

  }

  }

  foreach (string dirName in dirNames)

  {

  string relativeDirPath = dirNameReplace(root );

  ZipEntry entry = new ZipEntry(relativeDirPathReplace(\\ /) + /);

  sPutNextEntry(entry);

  CompressDirectory(root dirName s buffer);

  }

  }

  /// <summary>

  /// 解壓縮zip文件

  /// </summary>

  /// <param name=zipFilePath>解壓的zip文件路徑</param>

  /// <param name=extractPath>解壓到的文件夾路徑</param>

  /// <param name=bufferSize>讀取文件的緩沖區大小</param>

  public void Extract(string zipFilePath string extractPath int bufferSize)

  {

  extractPath = extractPathTrimEnd(\\) + \\;

  byte[] data = new byte[bufferSize];

  int size;

  using (ZipInputStream s = new ZipInputStream(FileOpenRead(zipFilePath)))

  {

  ZipEntry entry;

  while ((entry = sGetNextEntry()) != null)

  {

  string directoryName = PathGetDirectoryName(entryName);

  string fileName = PathGetFileName(entryName);

  //先創建目錄

  if (directoryNameLength > )

  {

  DirectoryCreateDirectory(extractPath + directoryName);

  }

  if (fileName != StringEmpty)

  {

  using (FileStream streamWriter = FileCreate(extractPath + entryNameReplace(/ \\)))

  {

  while (true)

  {

  size = sRead(data dataLength);

  if (size > )

  {

  streamWriterWrite(data size);

  }

  else

  {

  break;

  }

  }

  }

  }

  }

  }

  }

  }

  網上找了很久 包括老外的site也找了 沒找到現成的 自己寫了一個

  使用方法

  使用方法

  Zip z = new Zip();

  zExtract(c:\\cczipc:\\haha ); zExtract(cczipc:\\haha ); zExtract(c:\\ccziphaha );

  //上面的解壓縮沒啥大問題

  zCompressDirectory(E:\\DotNet_Library\\SharpZipLib\\SharpZipLib__SourceSamples c:\\aazip );

  這個代碼有一點點小問題 沒有異常處理 壓縮過程中 有文件被編輯或者被刪除 那就無法壓縮了

  如果這樣那也會異常

  zCompressDirectory(C:\\CC C:\\CC\\zip );


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