c# Stream progress just like .DownloadProgressChanged

别说谁变了你拦得住时间么 提交于 2019-12-13 01:36:14

问题


So i have the following code:

WebClient webClient = new WebClient();
Stream data = webClient.OpenRead("http://awesomeurl.com/file.zip");
UnzipFromStream(data, @"c:/dir/");

And i would like to have the data of that stream in a progress bar, just like if you would use WebClient with .DownloadProgressChanged

I cant however seem to figure out how to make it work...

Please note: i am using ICSharpCode.SharpZipLib

UPDATE #1.1

Ok, i managed to get the following code to work (it downloads and unzips + the complete msg is shown), however the progress bar is not updating:

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");
    }

来源:https://stackoverflow.com/questions/19097324/c-sharp-stream-progress-just-like-downloadprogresschanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!