code_junkie

Computing progress (bar) using GZipStream

Nov 13th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;
  2. using (Stream ftpStream = response.GetResponseStream())
  3. using (GZipStream unzipped = new GZipStream(ftpStream, CompressionMode.Decompress))
  4. using (StreamReader linereader = new StreamReader(unzipped))
  5. {
  6. String l;
  7. while ((l = linereader.ReadLine()) != null)
  8. {
  9. ...
  10. }
  11. }
  12.  
  13. public class ProgressStream : Stream
  14. {
  15. public long BytesRead { get; set; }
  16. Stream _baseStream;
  17. public ProgressStream(Stream s)
  18. {
  19. _baseStream = s;
  20. }
  21. public override bool CanRead
  22. {
  23. get { return _baseStream.CanRead; }
  24. }
  25. public override bool CanSeek
  26. {
  27. get { return false; }
  28. }
  29. public override bool CanWrite
  30. {
  31. get { return false; }
  32. }
  33. public override void Flush()
  34. {
  35. _baseStream.Flush();
  36. }
  37. public override long Length
  38. {
  39. get { throw new NotImplementedException(); }
  40. }
  41. public override long Position
  42. {
  43. get
  44. {
  45. throw new NotImplementedException();
  46. }
  47. set
  48. {
  49. throw new NotImplementedException();
  50. }
  51. }
  52. public override int Read(byte[] buffer, int offset, int count)
  53. {
  54. int rc = _baseStream.Read(buffer, offset, count);
  55. BytesRead += rc;
  56. return rc;
  57. }
  58. public override long Seek(long offset, SeekOrigin origin)
  59. {
  60. throw new NotImplementedException();
  61. }
  62. public override void SetLength(long value)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. public override void Write(byte[] buffer, int offset, int count)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. }
  71.  
  72. // usage
  73. FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;
  74. using (Stream ftpStream = response.GetResponseStream())
  75. using (ProgressStream progressStream = new ProgressStream(ftpstream))
  76. using (GZipStream unzipped = new GZipStream(progressStream, CompressionMode.Decompress))
  77. using (StreamReader linereader = new StreamReader(unzipped))
  78. {
  79. String l;
  80. while ((l = linereader.ReadLine()) != null)
  81. {
  82. progressStream.BytesRead(); // does contain the # of bytes read from FTP so far.
  83. }
  84. }
  85.  
  86. public static readonly byte[] symbols = new byte[8 * 1024];
  87.  
  88. public static void Decompress(FileInfo inFile, FileInfo outFile)
  89. {
  90. using (var inStream = inFile.OpenRead())
  91. {
  92. using (var zipStream = new GZipStream(inStream, CompressionMode.Decompress))
  93. {
  94. using (var outStream = outFile.OpenWrite())
  95. {
  96. var total = 0;
  97. do
  98. {
  99. var async = zipStream.BeginRead(symbols, 0, symbols.Length, null, null);
  100. total = zipStream.EndRead(async);
  101. if (total != 0)
  102. {
  103. // Report progress. Read total bytes (8K) from the zipped file.
  104. outStream.Write(symbols, 0, total);
  105. }
  106. } while (total != 0);
  107. }
  108. }
  109. }
  110. }
  111.  
  112. public static readonly byte[] symbols = new byte[8 * 1024];
  113.  
  114. public static void Decompress(FileInfo inFile, FileInfo outFile, double size, Action<double> progress)
  115. {
  116. var percents = new List<double>(100);
  117.  
  118. using (var inStream = inFile.OpenRead())
  119. {
  120. using (var zipStream = new GZipStream(inStream, CompressionMode.Decompress))
  121. {
  122. using (var outStream = outFile.OpenWrite())
  123. {
  124. var current = 0;
  125.  
  126. var total = 0;
  127. while ((total = zipStream.Read(symbols, 0, symbols.Length)) != 0)
  128. {
  129. outStream.Write(symbols, 0, total);
  130. current += total;
  131.  
  132. var p = Math.Round(((double)current / size), 2) * 100;
  133. if (!percents.Contains(p))
  134. {
  135. if (progress != null)
  136. {
  137. progress(p);
  138. }
  139. percents.Add(p);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
Add Comment
Please, Sign In to add comment