来自 URL 的 Zip 文件无效 [英] Zip file from URL not valid

查看:31
本文介绍了来自 URL 的 Zip 文件无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了另一篇文章,以便能够压缩 URL 的内容..

I followed another post to be able to zip the content of an URL..

当我单击下载"按钮时,我压缩"了 URL 的内容并将其保存在默认下载文件夹中...

When I click my button Download, I "zip" the content of the URL and I save it in the default download folder...

到目前为止,这是我的代码:

So far this is my code:

WebClient wc = new WebClient(); 

ZipFile zipFile = new ZipFile();

string filename = "myfile.zip";

zipFile.Password = item.Password;

Stream s = wc.OpenRead(myUrl); 

zipFile.AddeEntry(filename, s);

return File(s, "application/zip", filename);

它类似于这个(压缩文件夹的内容...)(它工作正常)

it´s similar to this one (which zips the content of a folder... ) (It works correctly)

ZipFile zipFile = new ZipFile();

zipFile.Password = item.Password;

zipFile.AddDirectory(sourcePath, "");

MemoryStream stream = new MemoryStream();
zipFile.Save(stream);
zipFile.Dispose();
stream.Seek(0, SeekOrigin.Begin);

return File(stream, "application/zip", fileName);

所以,我想对 URL 做完全相同的事情..

So, I want to do exactly the same with an URL..

谢谢!

推荐答案

最后我使用了这段代码,它像我想要的那样工作......

at the end I use this code and It works like I wanted...

再次感谢大家!

 string fileName = "filename" + ".zip";

 MemoryStream stream = new MemoryStream();

 ZipFile zipFile = new ZipFile();     

 WebRequest webRequest = WebRequest.Create(myUrl);

 webRequest.Timeout = 1000;

 WebResponse webResponse = webRequest.GetResponse();

 using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
 {
     string content = reader.ReadToEnd();
     zipFile.AddEntry("myfile.txt", content);
 }

 zipFile.Save(stream);
 zipFile.Dispose();
 stream.Seek(0, SeekOrigin.Begin);

 return File(stream, "application/zip", fileName);

这篇关于来自 URL 的 Zip 文件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆