国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#中利用SharpZipLib進(jìn)行壓縮和解壓

2024-07-21 02:29:13
字體:
供稿:網(wǎng)友

  我在做項(xiàng)目的時(shí)候需要將文件進(jìn)行壓縮和解壓縮,于是就從http://www.icsharpcode.net下載了關(guān)于壓縮和解壓縮的源碼,但是下載下來后,面對(duì)這么多的代碼,一時(shí)不知如何下手。只好耐下心來,慢慢的研究,總算找到了門路。針對(duì)自己的需要改寫了文件壓縮和解壓縮的兩個(gè)類,分別為zipclass和unzipclass。其中碰到了不少困難,就決定寫出來壓縮和解壓的程序后,一定把源碼貼出來共享,讓首次接觸壓縮和解壓縮的朋友可以少走些彎路。下面就來解釋如何在c#里用http://www.icsharpcode.net下載的sharpziplib進(jìn)行文件的壓縮和解壓縮。

  首先需要在項(xiàng)目里引用sharpziplib.dll。然后修改其中的關(guān)于壓縮和解壓縮的類。實(shí)現(xiàn)源碼如下:

/// <summary>
/// 壓縮文件
/// </summary>

using system;
using system.io;

using icsharpcode.sharpziplib.checksums;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.gzip;

namespace compression
{
public class zipclass
{

public void zipfile(string filetozip, string zipedfile ,int compressionlevel, int blocksize)
{
//如果文件沒有找到,則報(bào)錯(cuò)
if (! system.io.file.exists(filetozip))
{
throw new system.io.filenotfoundexception("the specified file " + filetozip + " could not be found. zipping aborderd");
}

system.io.filestream streamtozip = new system.io.filestream(filetozip,system.io.filemode.open , system.io.fileaccess.read);
system.io.filestream zipfile = system.io.file.create(zipedfile);
zipoutputstream zipstream = new zipoutputstream(zipfile);
zipentry zipentry = new zipentry("zippedfile");
zipstream.putnextentry(zipentry);
zipstream.setlevel(compressionlevel);
byte[] buffer = new byte[blocksize];
system.int32 size =streamtozip.read(buffer,0,buffer.length);
zipstream.write(buffer,0,size);
try
{
while (size < streamtozip.length)
{
int sizeread =streamtozip.read(buffer,0,buffer.length);
zipstream.write(buffer,0,sizeread);
size += sizeread;
}
}
catch(system.exception ex)
{
throw ex;
}
zipstream.finish();
zipstream.close();
streamtozip.close();
}

public void zipfilemain(string[] args)
{
string[] filenames = directory.getfiles(args[0]);

crc32 crc = new crc32();
zipoutputstream s = new zipoutputstream(file.create(args[1]));

s.setlevel(6); // 0 - store only to 9 - means best compression

foreach (string file in filenames)
{
//打開壓縮文件
filestream fs = file.openread(file);

byte[] buffer = new byte[fs.length];
fs.read(buffer, 0, buffer.length);
zipentry entry = new zipentry(file);

entry.datetime = datetime.now;

// set size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// some zip programs have problems with zip files that don't store
// the size and crc in the header.
entry.size = fs.length;
fs.close();

crc.reset();
crc.update(buffer);

entry.crc = crc.value;

s.putnextentry(entry);

s.write(buffer, 0, buffer.length);

}

s.finish();
s.close();
}
}
}

  現(xiàn)在再來看看解壓文件類的源碼

/// <summary>
/// 解壓文件
/// </summary>

using system;
using system.text;
using system.collections;
using system.io;
using system.diagnostics;
using system.runtime.serialization.formatters.binary;
using system.data;

using icsharpcode.sharpziplib.bzip2;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.zip.compression;
using icsharpcode.sharpziplib.zip.compression.streams;
using icsharpcode.sharpziplib.gzip;

namespace decompression
{
public class unzipclass
{
public void unzip(string[] args)
{
zipinputstream s = new zipinputstream(file.openread(args[0]));

zipentry theentry;
while ((theentry = s.getnextentry()) != null)
{

string directoryname = path.getdirectoryname(args[1]);
string filename = path.getfilename(theentry.name);

//生成解壓目錄
directory.createdirectory(directoryname);

if (filename != string.empty)
{
//解壓文件到指定的目錄
filestream streamwriter = file.create(args[1]+theentry.name);

int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.read(data, 0, data.length);
if (size > 0)
{
streamwriter.write(data, 0, size);
}
else
{
break;
}
}

streamwriter.close();
}
}
s.close();
}
}
}

  有了壓縮和解壓縮的類以后,就要在窗體里調(diào)用了。怎么?是新手,不會(huì)調(diào)用?ok,接著往下看如何在窗體里調(diào)用。

  首先在窗體里放置兩個(gè)命令按鈕(不要告訴我你不會(huì)放啊~),然后編寫以下源碼

/// <summary>
/// 調(diào)用源碼
/// </summary>

private void button2_click_1(object sender, system.eventargs e)
{
string []fileproperties=new string[2];
fileproperties[0]="c://unzipped//";//待壓縮文件目錄
fileproperties[1]="c://zip//a.zip"; //壓縮后的目標(biāo)文件
zipclass zc=new zipclass();
zc.zipfilemain(fileproperties);
}

private void button2_click(object sender, system.eventargs e)
{
string []fileproperties=new string[2];
fileproperties[0]="c://zip//test.zip";//待解壓的文件
fileproperties[1]="c://unzipped//";//解壓后放置的目標(biāo)目錄
unzipclass unzc=new unzipclass();
unzc.unzip(fileproperties);
}

  好了,到此為止,如何壓縮和解壓縮的類都已經(jīng)完成了,需要的朋友直接拿走調(diào)吧。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁门县| 连州市| 鹤庆县| 株洲县| 朝阳县| 齐齐哈尔市| 黑龙江省| 嫩江县| 蓬安县| 宿松县| 齐河县| 阳曲县| 涟水县| 长葛市| 获嘉县| 文昌市| 滁州市| 普陀区| 江门市| 怀化市| 龙江县| 永登县| 鄄城县| 晋宁县| 栾城县| 成武县| 德化县| 乾安县| 凤凰县| 云林县| 台中县| 公主岭市| 嘉定区| 札达县| 泸州市| 驻马店市| 犍为县| 天等县| 黑山县| 吴川市| 舒城县|