Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;
- using (Stream ftpStream = response.GetResponseStream())
- using (GZipStream unzipped = new GZipStream(ftpStream, CompressionMode.Decompress))
- using (StreamReader linereader = new StreamReader(unzipped))
- {
- String l;
- while ((l = linereader.ReadLine()) != null)
- {
- ...
- }
- }
- public class ProgressStream : Stream
- {
- public long BytesRead { get; set; }
- Stream _baseStream;
- public ProgressStream(Stream s)
- {
- _baseStream = s;
- }
- public override bool CanRead
- {
- get { return _baseStream.CanRead; }
- }
- public override bool CanSeek
- {
- get { return false; }
- }
- public override bool CanWrite
- {
- get { return false; }
- }
- public override void Flush()
- {
- _baseStream.Flush();
- }
- public override long Length
- {
- get { throw new NotImplementedException(); }
- }
- public override long Position
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- int rc = _baseStream.Read(buffer, offset, count);
- BytesRead += rc;
- return rc;
- }
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotImplementedException();
- }
- public override void SetLength(long value)
- {
- throw new NotImplementedException();
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- throw new NotImplementedException();
- }
- }
- // usage
- FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;
- using (Stream ftpStream = response.GetResponseStream())
- using (ProgressStream progressStream = new ProgressStream(ftpstream))
- using (GZipStream unzipped = new GZipStream(progressStream, CompressionMode.Decompress))
- using (StreamReader linereader = new StreamReader(unzipped))
- {
- String l;
- while ((l = linereader.ReadLine()) != null)
- {
- progressStream.BytesRead(); // does contain the # of bytes read from FTP so far.
- }
- }
- public static readonly byte[] symbols = new byte[8 * 1024];
- public static void Decompress(FileInfo inFile, FileInfo outFile)
- {
- using (var inStream = inFile.OpenRead())
- {
- using (var zipStream = new GZipStream(inStream, CompressionMode.Decompress))
- {
- using (var outStream = outFile.OpenWrite())
- {
- var total = 0;
- do
- {
- var async = zipStream.BeginRead(symbols, 0, symbols.Length, null, null);
- total = zipStream.EndRead(async);
- if (total != 0)
- {
- // Report progress. Read total bytes (8K) from the zipped file.
- outStream.Write(symbols, 0, total);
- }
- } while (total != 0);
- }
- }
- }
- }
- public static readonly byte[] symbols = new byte[8 * 1024];
- public static void Decompress(FileInfo inFile, FileInfo outFile, double size, Action<double> progress)
- {
- var percents = new List<double>(100);
- using (var inStream = inFile.OpenRead())
- {
- using (var zipStream = new GZipStream(inStream, CompressionMode.Decompress))
- {
- using (var outStream = outFile.OpenWrite())
- {
- var current = 0;
- var total = 0;
- while ((total = zipStream.Read(symbols, 0, symbols.Length)) != 0)
- {
- outStream.Write(symbols, 0, total);
- current += total;
- var p = Math.Round(((double)current / size), 2) * 100;
- if (!percents.Contains(p))
- {
- if (progress != null)
- {
- progress(p);
- }
- percents.Add(p);
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment