2017-10-11 98 views
1

我正在從URL下載zip文件,並試圖手動提取它只是爲了檢查它已經通過正確顯示它顯示它的空着,不讓我。從URL下載Zip文件時,顯示ZIP文件爲空時嘗試手動提取

try { 
    using (var client = new WebClient()) 
    { 
     client.DownloadFile("url", "C:/1.zip"); 
    } 
} catch(Exception e) { 
    Debug.WriteLine(e + "DDDD"); 
} 

此外,我將如何編程提取此所以我可以進入該文件的內容,提取更多的事情。最簡單的方法是什麼?

+0

你從哪裏下載?您自己設計的公共資源或自定義應用程序? – DiskJunky

+1

@DiskJunky這是一個公共資源,但我不能共享URL,因爲它只是內部使用。 – Sam

+0

對於解壓縮部分:https://stackoverflow.com/questions/836736/unzip-files-programmatically-in-net?rq=1 – Goufalite

回答

0

通過Firefox中的HTTP Headers檢查URL。在給出zip文件之前,發現這個URL需要某種API。該URL有作爲參數傳遞的參數。

我安裝RestSharp則做到了這一點:

var client = new RestClient(URL); 
var request = new RestRequest("&user=bam&pass=boom", Method.GET); 
var queryResult = client.Execute(request); 
string zipPath = C:/Temp + zippy.zip"; 
client.DownloadData(request).SaveAs(zipPath); 
0

嘗試使用異步方法和與之一起使用的事件。 事情是這樣的:

void Foo() { 
    var webClient = new WebClient(); 
    var totalBytes = 0l; 
    var destFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "platform-tools-latest-windows.zip")); 
    webClient.DownloadProgressChanged += (s, e) => Debug.WriteLine($"Download progress changed: { e.ProgressPercentage }% ({ e.BytesReceived }/{ (totalBytes = e.TotalBytesToReceive) })"); 
    webClient.DownloadFileCompleted += (s, e) => { 
     destFile.Refresh(); 
     if (destFile.Length != totalBytes) { 
      // Handle error 
     } else { 
      // Do nothing? 
     } 
    }; 
    webClient.DownloadFileAsync(new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip"), destFile.FullName); 

} 

給一個嘗試,看看它是否與您的郵遞工作

編輯: 如果上面的代碼不能正常工作,有一些更多的可能性值得嘗試。

1:嘗試附加while (webClient.IsBusy);上述方法結束時,強制運行的線程等待,直到Web客戶端下載完

2:嘗試第一下載的原始數據(byte[]),然後沖洗緩衝到文件。

注意:只能做這個小(呃)文件!

public void DownloadFoo() { 
     var webClient = new WebClient(); 
     var totalBytes = 0l; 
     var destFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "platform-tools-latest-windows.zip")); 
     webClient.DownloadProgressChanged += (s, e) => Debug.WriteLine($"Download progress changed: { e.ProgressPercentage }% ({ e.BytesReceived }/{ (totalBytes = e.TotalBytesToReceive) })"); 

     using (webClient) { 
      var buffer = webClient.DownloadData(new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip")); 

      using (var oStream = destFile.Open(FileMode.Truncate)) { 
       oStream.Write(buffer, 0, buffer.Length); 
       oStream.Flush(true); // true => flushToDisk 
      } 
     } 

     // webClient is automatically disposed of; method will return cleanly 

    } 
+0

它仍然無法手動打開它顯示爲空。這是因爲它需要被節目提取? – Sam

+0

上面的代碼片段包含一個有效的鏈接;你有沒有試過執行上面的代碼 - 就這樣 - 然後打開生成的文件?你能打開文件嗎? – SimonC

+0

剛剛嘗試過,使用此網址https://dl.google.com/android/repository/platform-tools-latest-windows.zip仍然不允許我打開它 – Sam

0

您可以使用下面的代碼提取您的zip文件。

System.IO.Compression.ZipFile.ExtractToDirectory(@"C:/1.zip",@"c:\example\extract"); 

不要忘記從裝配中添加System.IO.Compression.FileSystem

+0

使用System.IO.Compression.FileSystem添加此項; 顯示命名空間FileSystem不存在 – Sam

+1

您需要添加對所述命名空間/程序集的引用,然後才能使用它。 – SimonC

+0

@Sam你必須在我的答案中提及參考 –