檔案資料夾壓縮解壓工具類

2021-08-09 06:01:53 字數 3405 閱讀 1431

需引用:icsharpcode.sharpziplib.dll

* 名稱: 檔案資料夾壓縮解壓工具類

* 版本: 1

* 說明: 檔案資料夾壓縮解壓工具類

*       

* 歷史:

* 版本

時間修改人

說明* 1       2017.08.15             lizhongxiang      檔案資料夾壓縮解壓工具類

*/using system;

using system.collections;

using system.collections.generic;

using system.io;

using icsharpcode.sharpziplib.checksums;

using icsharpcode.sharpziplib.zip;

namespace tysoft

if (zipedfile == string.empty)

if (path.getextension(zipedfile) != ".zip")

如果指定位置目錄不存在,建立該目錄

//string zipeddir = zipedfile.substring(0,zipedfile.lastindexof("\\"));

//if (!directory.exists(zipeddir))

//    directory.createdirectory(zipeddir);

//被壓縮檔案名稱

string filename = filetozip.substring(filetozip.lastindexof('\\') + 1);

filestream streamtozip = new filestream(filetozip, filemode.open, fileaccess.read);

filestream zipfile = file.create(zipedfile);

zipoutputstream zipstream = new zipoutputstream(zipfile);

zipentry zipentry = new zipentry(filename);

zipstream.putnextentry(zipentry);

zipstream.setlevel(compressionlevel);

byte buffer = new byte[2048];

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

zipstream.write(buffer, 0, size);

try}

catch (exception ex)

finally

}///

/// 壓縮資料夾的方法

///

/// 被壓縮的檔案路徑

/// 壓縮後的檔名稱(包含檔案路徑)

/// 壓縮率0(無壓縮)-9(壓縮率最高)

public static void zipdir(string dirtozip, string zipedfile, int compressionlevel)

if (path.getextension(zipedfile) != ".zip")

using (zipoutputstream zipoutputstream = new zipoutputstream(file.create(zipedfile)))}}

///

/// 獲取所有檔案

///

///

private static hashtable getallfies(string dir)

getalldirfiles(filedire, fileslist);

getalldirsfiles(filedire.getdirectories(), fileslist);

return fileslist;

}///

/// 獲取乙個資料夾下的所有資料夾裡的檔案

///

///

///

private static void getalldirsfiles(directoryinfo dirs, hashtable fileslist)

getalldirsfiles(dir.getdirectories(), fileslist);}}

///

/// 獲取乙個資料夾下的檔案

///

/// 目錄名稱

/// 檔案列表hasttable

private static void getalldirfiles(directoryinfo dir, hashtable fileslist)

}///

/// 功能:解壓zip格式的檔案。

///

/// 壓縮檔案路徑

/// 解壓檔案存放路徑,為空時預設與壓縮檔案同一級目錄下,跟壓縮檔案同名的資料夾

/// 解壓是否成功

public static void unzip(string zipfilepath, string unzipdir)

if (!file.exists(zipfilepath))

//解壓資料夾為空時預設與壓縮檔案同一級目錄下,跟壓縮檔案同名的資料夾

if (unzipdir == string.empty)

unzipdir = zipfilepath.replace(path.getfilename(zipfilepath), path.getfilenamewithoutextension(zipfilepath));

if (!unzipdir.endswith("\\"))

unzipdir += "\\";

if (!directory.exists(unzipdir))

directory.createdirectory(unzipdir);

using (zipinputstream s = new zipinputstream(file.openread(zipfilepath)))

if (directoryname != null && !directoryname.endswith("\\"))

if (filename != string.empty)

else}}

}}}}

public static listgetdirnames(string filename)

if (!file.exists(filename))

using (zipinputstream s = new zipinputstream(file.openread(filename)))}}

return doucumentlist;}}

}

壓縮和解壓多級資料夾

壓縮和解壓多級資料夾 空資料夾除外 public class test4 private static void compression string zipfilename,file targetfile catch filenotfoundexception e catch ioexceptio...

檔案 資料夾刪除工具類

public class deletefileutil else 刪除單個檔案 param filename 要刪除的檔案的檔名 return 單個檔案刪除成功返回true,否則返回false public static boolean deletefile string filename else...

linux資料夾壓縮 解壓縮命令

tar 解壓 tar zxvf filename.tar 壓縮 tar czvf filename.tar dirname gz命令 解壓1 gunzip filename.gz 解壓2 gzip d filename.gz 壓縮 gzip filename tar.gz 和 tgz 解壓 tar ...