因為專案需要打包檔案,就在同事的建議下用icsharpcode寫了個打包函式.icsharpcode的介紹就不說了.具體請到官方** 上了解.
**實現多檔案,自定義檔案,整目錄打包等功能.好了..奉上**:
壓縮打包**
//////
zips the specified zip path.
//////
the zip path.
///the zip top directory path.
///the zip level.
///the password.
///the files or directories paths.
///private
bool
zip(
string
strzippath,
string
strziptopdirectorypath,
intintziplevel,
string
strpassword,
string
filesordirectoriespaths)
else
if(directory.exists(filesordirectoriespaths[i]))}}
if(allfilespath.count
>0)
zipentry entry
=new
zipentry(strfilename);
entry.datetime
=datetime.now;
zipoutputstream.putnextentry(entry);
}else
//file
zipentry entry
=new
zipentry(strfilename);
entry.datetime
=datetime.now;
zipoutputstream.putnextentry(entry);
zipoutputstream.write(buffer,
0, buffer.length);
fs.close();
fs.dispose();}}
catch
}zipoutputstream.finish();
zipoutputstream.close();
return
true;}
else
}catch
}///
///gets the directory files.
//////
the parent directory path.
///all files path.
private
void
getdirectoryfiles(
string
strparentdirectorypath, list
<
string
>
allfilespath)
string
directorys
=directory.getdirectories(strparentdirectorypath);
for(
inti =0
; i
<
directorys.length; i++)
if(files.length ==0
&&directorys.length ==0
) //
empty folder
}呼叫也很簡單:
code
string
strzippath =@"
c:\documents and settings\ken\desktop\task1\ziptest.zip";
string
strziptopdirectorypath =@"
c:\documents and settings\ken\desktop\task1\";
intintziplevel =6
;string
strpassword =""
;string
filesordirectoriespaths
=new
string
;zip(strzippath, strziptopdirectorypath, intziplevel, strpassword, filesordirectoriespaths);
就這樣.如果需要儘管拿走.
(**參考過網路上的資源,如有冒犯,莫怪莫怪~)
由於上邊**對於大檔案是一次讀入,所以遇到大檔案的時候是占用資源比較緊張。所以改為分段讀取,每次讀取64k資料:
string
filename
=file.replace(ziptopdirectory,
"");
if(filename.startswith("\\
"))filename
=filename.substring(1);
zipentry entry
=new
zipentry(filename);
entry.datetime
=datetime.now;
zipedstream.putnextentry(entry);
for(
longj =
0; j
<
fs.length; j
+=65536
)byte
buffer
=new
byte
[bytelength];
fs.read(buffer,
0, bytelength);
zipedstream.write(buffer,
0, bytelength);
}fs.close();
c 操作壓縮檔案(ICSharpCode)
網上轉過來的,經測試很好用。using system using system.collections.generic using system.linq using system.text using system.threading.tasks using system.io using ics...
利用union壓縮資料
最近在使用vc 做專案時遇到乙個問題,在使用udp通訊時資料量太大導致無法傳送,查閱資料後找到了乙個使用union來壓縮資料的辦法。原理就是利用union內部定義的變數位址指向同一塊記憶體,改變其中乙個另乙個也跟著改變。以下是 include using namespace std typedef ...
利用SharpZipLib壓縮 解壓檔案
public class zipinfo 壓縮檔案 private void filetozip string path,string address 解壓到乙個目錄 public bool ziptofile string path,string addres s.close catch exce...