SharpZipLib壓縮檔案和資料夾

2021-06-06 18:36:20 字數 3077 閱讀 8586

正常zip方式壓縮檔案,然後用wrar工具可以解壓出來(開始用wrar3.4版本的怎麼也解不出來,以為是不支援wrar解壓,後來改用3.9及4.11版本可以實現正常解壓,搞了半天時間,原來是版本問題,版本不對害死人啊),另外wrar壓縮時選擇「zip」選項壓縮時,再用此類進行解壓,也可以正常解壓。

在此以做標記,給自己個別人做鋪路,以後少走彎路,

呼叫方式,如下:

private void btnnewzip_click(object sender, eventargs e)

catch

}private void btnnewunzip_click(object sender, eventargs e)

catch

}

以下是helper類庫,當中修改了乙個地方:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.io;

using icsharpcode.sharpziplib.zip;

using icsharpcode.sharpziplib.checksums;

namespace common

string ars = new string[2];

if (recursive == false)

filestream zipfile;

zipoutputstream zipstream;

//open

zipfile = file.create(targetfilename);

zipstream = new zipoutputstream(zipfile);

if (dir != string.empty)

//close

zipstream.finish();

zipstream.close();

if (file.exists(targetfilename))

return true;

else

return false;

}/// /// 壓縮目錄

///

/// 陣列(陣列[0]: 要壓縮的目錄; 陣列[1]: 壓縮的檔名)

public static bool zipfiledictory(string args)

}catch (exception e)

finally

return true;

}/// /// 壓縮某個子資料夾

///

///

///

///

private static void _compressfolder(string basepath, zipoutputstream zips, string zipfolername)

string names = directory.getfiles(basepath);

foreach (string filename in names)

names = directory.getdirectories(basepath);

foreach (string foldername in names)

}/// /// 壓縮某個子檔案

///

///

///

///

private static void _addfile(string filename, zipoutputstream zips, string zipfolername)

}/// /// 壓縮單獨檔案

///

///

///

///

private static void _createzipfile(string filetozip, zipoutputstream zips, string zipfolername)

zipentry zipen = new zipentry(temp.substring(3));

zips.putnextentry(zipen);

byte buffer = new byte[16384];

system.int32 size = streamtozip.read(buffer, 0, buffer.length);

zips.write(buffer, 0, size);

try}

catch (system.exception ex)

streamtozip.close();

}catch (exception e)

}#endregion

#region

/// /// 解壓縮目錄

///

/// 壓縮目錄路徑

/// 解壓縮目錄路徑

public void unzipdirectory(string zipdirectorypath, string unzipdirecotypath, string password)

using (zipinputstream zipstream = new zipinputstream(file.openread(zipdirectorypath)))

zipentry zipentry = null;

while ((zipentry = zipstream.getnextentry()) != null)

if (!string.isnullorempty(filename))

else//2012-5-28修改,支援單個檔案壓縮時自己建立目標資料夾

}using (filestream stream = file.create(unzipdirecotypath + @"\" + zipentry.name))

else}}

}}}}

#endregion

}}

C 壓縮檔案(SharpZipLib)

使用國外開源加壓解壓庫icsharpcode.sharpziplib實現加壓,該庫的官方 為 使用體驗 可以照著例子實現簡單的加壓解壓,可以加壓乙個資料夾中的所有檔案,但沒有提供加壓子資料夾的說明。目前網上的一些 有的無法加壓空資料夾,有的加壓了用rar解不開,這是一點需要改進的。但如果只需要加壓資...

利用SharpZipLib壓縮 解壓檔案

public class zipinfo 壓縮檔案 private void filetozip string path,string address 解壓到乙個目錄 public bool ziptofile string path,string addres s.close catch exce...

SharpZipLib 檔案 資料夾壓縮

zipfile類用於選擇檔案或資料夾進行壓縮生成壓縮包。常用屬性 屬性說明 count 檔案數目 注意是在comitupdat之後才有 password 壓縮包密碼 size 壓縮包占用空間大小 name 壓縮包名稱,預設輸出是檔案路徑 zipentry 壓縮包裡的檔案,通過索引訪問 其常用方法如下...