C#中实现zip协议,通过WebRequest查询zip文件内容。统一访问接口
2011-05-14 19:40
976 查看
private static void Test() {
bool httpResult = WebRequest.RegisterPrefix("zip://C", new ZipWebRequestCreator(@"C:/"));
WebRequest req = WebRequest.Create(@"zip://C/output.zip/index.html");
WebResponse response = req.GetResponse();
using (Stream stream = response.GetResponseStream()) {
System.Console.WriteLine(stream.Length);
using (Stream mem = new FileStream(@"C:/1.txt", FileMode.Create)) {
int size = 0;
while (true) {
byte[] buffer = new byte[4096];
size = stream.Read(buffer, 0, buffer.Length);
if (size > 0) {
mem.Write(buffer, 0, size);
} else {
break;
}
}
}
}
}
public class ZipWebRequestCreator : IWebRequestCreate {
public WebRequest Create(Uri uri) {
return new ZipWebRequest(uri, _zipDir);
}
private string _zipDir;
public ZipWebRequestCreator(string zipDir) {
_zipDir = zipDir;
if (string.IsNullOrEmpty(zipDir)) {
_zipDir = Path.GetDirectoryName(this.GetType().Assembly.Location);
}
}
}
public class ZipWebResponse : WebResponse {
public override String ContentType {
get { return ""; }
set { /* override */ }
}
public override Stream GetResponseStream() {
string zipFileName = Path.Combine(_zipDir, _zipFile);
if (File.Exists(zipFileName)) {
ZipFile file = new ZipFile(zipFileName);
int index = file.FindEntry(_entryName, true);
if (index == -1) {
throw new FileNotFoundException(_entryName);
}
Stream zipStream = file.GetInputStream(index);
return zipStream;
}
throw new FileNotFoundException(zipFileName);
}
private string _zipDir, _zipFile, _entryName;
internal ZipWebResponse(string zipDir, string absPath) {
_zipDir = zipDir;
if (absPath[0] == '/') absPath = absPath.Substring(1);
int index = absPath.IndexOfAny(new char[] { '/', '//' });
_zipFile = absPath.Substring(0, index);
_entryName = absPath.Substring(index + 1);
}
}
[Serializable]
public class ZipWebRequest : WebRequest, ISerializable {
public override WebResponse GetResponse() {
this.m_syncHint = true;
try {
string absPath = m_uri.AbsolutePath;
ZipWebResponse response = new ZipWebResponse(_zipDir, absPath);
return response;
} catch (Exception exception2) {
//if (Logging.On) {
// Logging.Exception(Logging.Web, this, "GetResponse", exception2);
//}
throw;
}
}
}
bool httpResult = WebRequest.RegisterPrefix("zip://C", new ZipWebRequestCreator(@"C:/"));
WebRequest req = WebRequest.Create(@"zip://C/output.zip/index.html");
WebResponse response = req.GetResponse();
using (Stream stream = response.GetResponseStream()) {
System.Console.WriteLine(stream.Length);
using (Stream mem = new FileStream(@"C:/1.txt", FileMode.Create)) {
int size = 0;
while (true) {
byte[] buffer = new byte[4096];
size = stream.Read(buffer, 0, buffer.Length);
if (size > 0) {
mem.Write(buffer, 0, size);
} else {
break;
}
}
}
}
}
public class ZipWebRequestCreator : IWebRequestCreate {
public WebRequest Create(Uri uri) {
return new ZipWebRequest(uri, _zipDir);
}
private string _zipDir;
public ZipWebRequestCreator(string zipDir) {
_zipDir = zipDir;
if (string.IsNullOrEmpty(zipDir)) {
_zipDir = Path.GetDirectoryName(this.GetType().Assembly.Location);
}
}
}
public class ZipWebResponse : WebResponse {
public override String ContentType {
get { return ""; }
set { /* override */ }
}
public override Stream GetResponseStream() {
string zipFileName = Path.Combine(_zipDir, _zipFile);
if (File.Exists(zipFileName)) {
ZipFile file = new ZipFile(zipFileName);
int index = file.FindEntry(_entryName, true);
if (index == -1) {
throw new FileNotFoundException(_entryName);
}
Stream zipStream = file.GetInputStream(index);
return zipStream;
}
throw new FileNotFoundException(zipFileName);
}
private string _zipDir, _zipFile, _entryName;
internal ZipWebResponse(string zipDir, string absPath) {
_zipDir = zipDir;
if (absPath[0] == '/') absPath = absPath.Substring(1);
int index = absPath.IndexOfAny(new char[] { '/', '//' });
_zipFile = absPath.Substring(0, index);
_entryName = absPath.Substring(index + 1);
}
}
[Serializable]
public class ZipWebRequest : WebRequest, ISerializable {
public override WebResponse GetResponse() {
this.m_syncHint = true;
try {
string absPath = m_uri.AbsolutePath;
ZipWebResponse response = new ZipWebResponse(_zipDir, absPath);
return response;
} catch (Exception exception2) {
//if (Logging.On) {
// Logging.Exception(Logging.Web, this, "GetResponse", exception2);
//}
throw;
}
}
}
相关文章推荐
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- 一个简单的QQ隐藏图生成算法 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
- 通过FTP服务端来实现匿名用户和基本用户的访问,可以实现文件上传和下载。通过web网站来浏览内容
- 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
- C# 使用HttpWebRequest通过PHP接口 上传文件
- C# 使用HttpWebRequest通过PHP接口 上传文件
- C# 通过HttpWebRequest 创建连接获取文件大小 判断Http类型的文件是否存在
- NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(3)
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容 。。。。。
- 转:C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- J-02. 小米路由器通过Web接口访问硬盘文件
- C#通过WebClient/HttpWebRequest实现http的post/get方法