简体   繁体   English

从服务器下载zip文件并解析它

[英]Download zip file from the server and parsing it

I am trying to download a zipped file from the server and trying to show the content of each files in zipped folder to the view. 我试图从服务器下载一个压缩文件,并尝试将压缩文件夹中的每个文件的内容显示到视图中。

I wrote a separate code where the file is on my laptop and I ran across each file and dislpayed the content such as 我写了一个单独的代码,其中文件在我的笔记本电脑上,我跑过每个文件,并删除了诸如的内容

static void Main(string[] args)
{
     string filePath = "C:\\ACL Data\\New folder\\files.zip";
     var zip= new ZipInputStream(File.OpenRead(filePath));
     var filestream=new FileStream(filePath,FileMode.Open,FileAccess.Read);
     ZipFile zipfile = new ZipFile(filestream);
     ZipEntry item;
     while ((item = zip.GetNextEntry()) != null)
     {
         Console.WriteLine(item.Name);
         using (StreamReader s = new StreamReader(zipfile.GetInputStream(item)))
         {
             Console.WriteLine(s.ReadToEnd());
         }
     }
     Console.Read();
 }

I am using sharplibzip library to implement this This is the case when the zip file is located locally in the system. 我正在使用sharplibzip库来实现这一点当zip文件位于系统本地时就是这种情况。 My next task scenario is what if the zipped file is located on the server. 我的下一个任务场景是如果压缩文件位于服务器上。 I am figuring out the way to implement it, below is the code what I assume should work 我正在找出实现它的方法,下面是我认为应该工作的代码

static void Main(string[] args)
{
    string url = "https://test/code/304fd9c6-7e53-42a2-845a-624608bfd2ce.zip";
    WebRequest webRequest = WebRequest.Create(url);
    webRequest.Method = "GET";
    WebResponse webResponse = webRequest.GetResponse();
    var zip = new ZipInputStream(webResponse.GetResponseStream());
    ZipEntry item1;

    //var zip= new ZipInputStream(File.OpenRead(filePath));
    var filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    ZipFile zipfile = new ZipFile(filestream);
    ZipEntry item;
    while ((item = zip.GetNextEntry()) != null)
    {
        Console.WriteLine(item.Name);
        using (StreamReader s = new StreamReader(zipfile.GetInputStream(item)))
        {
            Console.WriteLine(s.ReadToEnd());
        }
    }
    Console.Read();
} 

I am stuck at this part: var filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read); 我被困在这个部分: var filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read);

This expect the first parameter to be path of the zip file. 这期望第一个参数是zip文件的路径。 Since in the new scenario zip file is located remotely on the server. 由于在新方案中,zip文件位于服务器上的远程位置。 What should be the parameter in this case? 在这种情况下应该是什么参数?

Your original code opens the stream twice on the following rows, which I think is causing some confusion: 您的原始代码会在以下行中打开两次流,我认为这会导致一些混淆:

var zip= new ZipInputStream(File.OpenRead(filePath));
var filestream=new FileStream(filePath,FileMode.Open,FileAccess.Read);

There is an overload to the ZipFile constructor that takes "any" Stream rather than specifically a FileStream, which you - unsurprisingly - can only create for files. ZipFile构造函数有一个重载,它接受“任意”Stream,而不是特别是FileStream,你不出所料 - 只能为文件创建。

However, you cannot use the stream returned by GetResponseStream directly, because it's CanSeek property is false . 但是,您不能直接使用GetResponseStream返回的流,因为它的CanSeek属性为false This is because it's a NetworkStream, which can only be read once from beginning to end. 这是因为它是一个NetworkStream,只能从头到尾读取一次。 SharpZipLib needs random access to read the file contents. SharpZipLib需要随机访问才能读取文件内容。

Depending on the size of the ZIP file, loading it in memory may be an option. 根据ZIP文件的大小,将其加载到内存中可能是一种选择。 If you expect large files, writing it to a temporary file may be better. 如果您期望大文件,将其写入临时文件可能会更好。

This should do the trick, without using both ZipInputStream and ZipFile , by enumerating through ZipFile instead: 这应该做的伎俩,而无需同时使用ZipInputStreamZipFile ,通过使用ZipFile枚举,而不是:

string url = "https://test/code/304fd9c6-7e53-42a2-845a-624608bfd2ce.zip";
WebRequest webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
WebResponse webResponse = webRequest.GetResponse();

using (var responseStream = webResponse.GetResponseStream())
using (var ms = new MemoryStream())
{
    // Copy entire file into memory. Use a file if you expect a lot of data
    responseStream.CopyTo(ms);

    var zipFile = new ZipFile(ms);

    foreach (ZipEntry item in zipFile)
    {
        Console.WriteLine(item.Name);
        using (var s = new StreamReader(zipFile.GetInputStream(item)))
        {
            Console.WriteLine(s.ReadToEnd());
        }
    }
}
Console.Read();

PS: starting .NET 4.5, there is support for ZIP files built in. See the ZipArchive class. PS:从.NET 4.5开始,支持内置的ZIP文件。请参阅ZipArchive类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM