我正在从一些慢速源(如FTP服务器)读取.gz文件,并且正在处理收到的数据。看起来像这样:
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)
{
...
}
}
我的问题是显示准确的进度条。事先我可以获得压缩的.gz文件大小,但我不知道内容将被解压缩多大。 逐行读取文件我很清楚我读了多少未压缩的字节,但我不知道这与压缩文件大小有什么关系。
那么,有没有办法从GZipStream获取文件指针前进到压缩文件的距离?我只需要当前位置,我在读取文件之前可以获取的gz文件大小。
答案 0 :(得分:3)
您可以在其间插入一个流,计算GZipStream已读取的字节数。
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.
}
}
答案 1 :(得分:0)
我建议你看看下面的代码:
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);
}
}
}
}
答案 2 :(得分:0)
我重新访问了我的代码,并且我已经执行了一些测试。恕我直言是对的。但是我认为可以只读取压缩流的标题(大小?)并找出生成的文件大小。 (WinRar“知道”解压缩的文件大小是什么,而不解压缩整个zip存档。它从存档的标题中读取此信息。)如果找到生成的文件大小,此代码将帮助您报告精确的进度。
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);
}
}
}
}
}
}
我希望这会有所帮助。
答案 3 :(得分:0)
作为解压缩进度的代理,您可以尝试使用以下方法从基础流中获取文件的下载进度信息:
var percentageProgress = ftpStream.Position / (double)ftpWebResponse.ContentLength;
或
var percentageProgress = ftpStream.Position / (double)ftpStream.Length;
它适用于FileStream,它应该在GetResponseStream()上工作,前提是它实现了Position属性,FTP服务器返回有关下载文件长度的信息:{{ 3}}