だから私は次のコードを持っています:
WebClient webClient = new WebClient();
Stream data = webClient.OpenRead("http://awesomeurl.com/file.zip");
UnzipFromStream(data, @"c:/dir/");
そして、.DownloadProgressChanged で WebClient を使用する場合と同じように、そのストリームのデータをプログレス バーに表示したいと思います。
しかし、それを機能させる方法を理解できないようです...
注意してください:私はICSharpCode.SharpZipLibを使用しています
更新 #1.1
わかりました、次のコードを機能させることができました (ダウンロードして解凍し、完全なメッセージが表示されます)。ただし、進行状況バーは更新されません。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// first, we need to get the exact size (in bytes) of the file we are downloading
Uri url = new Uri(filename);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
// gets the size of the file in bytes
Int64 iSize = response.ContentLength;
// keeps track of the total bytes downloaded so we can update the progress bar
Int64 iRunningByteTotal = 0;
string outFolder = folder;
// use the webclient object to download the file
using (System.Net.WebClient webClient = new WebClient())
{
// open the file at the remote URL for reading
using (System.IO.Stream zipStream = webClient.OpenRead(filename))
{
ZipInputStream zipInputStream = new ZipInputStream(zipStream);
ZipEntry zipEntry = zipInputStream.GetNextEntry();
while (zipEntry != null)
{
String entryFileName = zipEntry.Name;
// to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
// Optionally match entrynames against a selection list here to skip as desired.
// The unpacked length is available in the zipEntry.Size property.
// Manipulate the output filename here as desired.
String fullZipToPath = Path.Combine(outFolder, entryFileName);
string directoryName = Path.GetDirectoryName(fullZipToPath);
if (directoryName.Length > 0)
Directory.CreateDirectory(directoryName);
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
// of the file, but does not waste memory.
// The "using" will close the stream even if an exception occurs.
using (FileStream streamWriter = File.Create(fullZipToPath))
{
int iByteSize = 0;
byte[] buffer = new byte[iSize]; // 4K is optimum
StreamUtils.Copy(zipInputStream, streamWriter, buffer);
while ((iByteSize = zipStream.Read(buffer, 0, buffer.Length)) > 0)
{
iRunningByteTotal += iByteSize;
// calculate the progress out of a base "100"
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)iSize;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
backgroundWorker1.ReportProgress(iProgressPercentage);
}
}
zipEntry = zipInputStream.GetNextEntry();
}
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("File download complete");
}