private static void UnzipFile(FileInfo zipFIle, string outFolder, UnzipProgressCallback progressCallback) { var events = new FastZipEvents(); events.Progress += UnzipProgressHandler; var fastZip = new FastZip(events); try { fastZip.ExtractZip(zipFIle.OpenRead(), outFolder, FastZip.Overwrite.Always, null, null, null, true, true); } catch (Exception e) { throw new DiskCleanupException(e.Message, e); } zipFIle.Delete(); void UnzipProgressHandler(object sender, ProgressEventArgs e) { progressCallback?.Invoke(e.Name, e.PercentComplete); } }
public void Download(DownloadProgressCallback downloadProgressCallback = default, UnzipProgressCallback unzipProgressCallback = default) { if (Interlocked.CompareExchange(ref _syncPoint, 1, 0) != 0) { throw new InvalidOperationException("A download or cleanup operation is already in progress."); } var webRequest = (HttpWebRequest)WebRequest.Create(DownloadUrl); try { using (var response = (HttpWebResponse)webRequest.GetResponse()) using (var responseStream = response.GetResponseStream() ?? throw new DiskCleanupException($"Error downloading CCleaner from {DownloadUrl}.")) using (var fileStream = _zip.OpenWrite()) { var downloaded = 0L; var buffer = new byte[8192]; int read; while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { fileStream.Write(buffer, 0, read); downloaded += read; downloadProgressCallback?.Invoke(response.ContentLength, downloaded); } } UnzipFile(_zip, Path.Combine(_zip.Directory.FullName, "CCleanerPortable"), unzipProgressCallback); } finally { Interlocked.Decrement(ref _syncPoint); } }
public IAsyncResult BeginDownload(DownloadProgressCallback downloadProgressCallback, UnzipProgressCallback unzipProgressCallback, AsyncCallback callback, object state) { _downloadDelegate = Download; return(_downloadDelegate.BeginInvoke(downloadProgressCallback, unzipProgressCallback, callback, state)); }