C 構造檔案和資料夾壓縮軟體

2021-10-12 11:10:24 字數 2667 閱讀 2566

實驗目的

通過製作乙個小專案,學習c#的圖形化程式設計。

實驗任務

構造檔案和資料夾壓縮軟體

準備工作

將設定轉化為c# 設定

專案說明:

運用vs中自帶的nuget包,安裝icsharpcode.sharpziplib庫

開始程式設計

匯入icsharpcode.sharpziplib庫

using icsharpcode.sharpziplib.zip;

using system;

using system.io;

using icsharpcode.sharpziplib.checksum;

由於英文文件比較混亂,且最近牆比較高,所以直接參考晚上的各種版本的運用icsharpcode的例子

一、建立ziphelper類

public

bool

zipdirectory

(string foldertozip,

string zipedfile,

string password)

/// 要壓縮的資料夾路徑

/// 壓縮檔案完整路徑

public

bool

zipdirectory

(string foldertozip,

string zipedfile)

public

bool

unzip

(string filetounzip,

string zipedfolder,

string password)

fs = file.

create

(filename)

;int size =

2048

;byte

data =

newbyte

[size]

;while

(true)}

}}catch

finally

if(zipstream !=

null)if

(ent !=

null

) gc.

collect()

; gc.

collect(1

);}return result;

}

二、建立圖形使用者介面,並將函式與圖形使用者介面相關聯

如圖所示,在圖中有五個按鈕,對應選擇資料夾、選擇檔案、壓縮、選擇壓縮包,解壓縮。而五個textbox則是儲存選擇時得到的路徑,當然,textbox設定為不可見。

private

void

button1_click

(object sender,

eventargs e)

}

private

void

button3_click

(object sender,

eventargs e)

elseif(

this

.textbox1.text ==

""&&

this

.textbox2.text =="")

else

elseif(

this

.textbox1.text !="")

@"\zipfolder\zip"

+ datetime.now.

tostring

("yyyymmddhhmmss")+

".zip";if

(!directory.

exists

(path.

getdirectoryname

(dest)))

ziphelper ziphelper =

newziphelper()

;bool flag = ziphelper.

zip(src, dest);if

(flag)

this

.textbox1.text ="";

this

.textbox2.text ="";

messagebox.

show

(this

,"壓縮檔案成功"

,"完成"

, messageboxbuttons.ok);}

else

}}

三、測試一下

C 檔案和資料夾

獲取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。返回上一級路徑 是你返回的資料夾級數 string parentpath di.fullname 判斷檔案是否存在 if system.io.file.exists parentpath directory.exists path pat...

SharpZipLib壓縮檔案和資料夾

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

C 中實現壓縮檔案和資料夾

c 壓縮檔案 方法1 filepath想要壓縮檔案的位址 zippath輸出壓縮檔案的位址 private void getfiletozip string filepath,string zippath 方法2 private void filetozip string path,string a...