asp教程.net c# zip文件压缩与解压
using system;
using system.collections.generic;
using system.text;
using system.io;
using icsharpcode.sharpziplib.zip;
using system.diagnostics;
using icsharpcode.sharpziplib.core;namespace testconsole
{
class program
{
static void main()
{
//createzipfile(@"d:", @"d:a.zip");
unzipfile(@"d:a.zip");console.read();
}private static void createzipfile(string filespath, string zipfilepath)
{if (!directory.exists(filespath))
{
console.writeline("cannot find directory '{0}'", filespath);
return;
}try
{
string[] filenames = directory.getfiles(filespath);
using (zipoutputstream s = new zipoutputstream(file.create(zipfilepath)))
{s.setlevel(9); // 压缩级别 0-9
//s.password = "123"; //zip压缩文件密码
byte[] buffer = new byte[4096]; //缓冲区大小
foreach (string file in filenames)
{
zipentry entry = new zipentry(path.getfilename(file));
entry.datetime = datetime.now;
s.putnextentry(entry);
using (filestream fs = file.openread(file))
{
int sourcebytes;
do
{
sourcebytes = fs.read(buffer, 0, buffer.length);
s.write(buffer, 0, sourcebytes);
} while (sourcebytes > 0);
}
}
s.finish();
s.close();
}
}
catch (exception ex)
{
console.writeline("exception during processing {0}", ex);
}
}private static void unzipfile( string zipfilepath)
{
if (!file.exists(zipfilepath))
{
console.writeline("cannot find file '{0}'", zipfilepath);
return;
}using (zipinputstream s = new zipinputstream(file.openread(zipfilepath)))
{zipentry theentry;
while ((theentry = s.getnextentry()) != null)
{console.writeline(theentry.name);
string directoryname = path.getdirectoryname(theentry.name);
string filename = path.getfilename(theentry.name);// create directory
if (directoryname.length > 0)
{
directory.createdirectory(directoryname);
}if (filename != string.empty)
{
using (filestream streamwriter = file.create(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;
}
}
}
}
}
}
}
}}
虽然网上很多流行的压缩文件格式都是rar的,但是由于rar不是一个开放的标准,因此zip成了更多人的选择。如果你不想自己开发的话可以选择开源的项目,比如sharpziplib就是一个不错的选择
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔