本人最近在写一个软件里面用到了GZIPSTREAM压缩数据,为了可以响应用户界面的操作我使用了异步来操作.以下是具体代码请高手过目问题也在代码下给出
private Boolean compressDateByFile(String inFilePath,String outFilePath)
{
int bufferSize = 2048;
this.isComplete = false;
Boolean compressSuccess = false;//标记异步压缩完成
Byte[] buffer = null;
try
{
buffer = new Byte[bufferSize];
FileStream inFS = new FileStream(inFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, true);
this.totalSize=inFS.Length;
FileStream outFS = new FileStream(outFilePath, FileMode.Create, FileAccess.Write, FileShare.Write, bufferSize, true);
GZipStream zipStream = new GZipStream(outFS, CompressionMode.Compress);
AsyncCallback readCallback = null;
AsyncCallback writeCallback = null;
writeCallback = delegate(IAsyncResult asyncResult)
{
zipStream.EndWrite(asyncResult);
int newSize=int.Parse(asyncResult.AsyncState.ToString());
if (newSize != 0)
{
this.curSize += newSize;
this.percent = calcPercent(this.curSize, this.totalSize);
}
else
{
this.isComplete = true;
zipStream.Close();
}
};
readCallback = delegate(IAsyncResult asyncResult)
{
int readByteCount = inFS.EndRead(asyncResult);
if (readByteCount > 0)
{
//没有到结尾
inFS.BeginRead(buffer, 0, buffer.Length, readCallback, null);
}
else
{
//到结尾了
inFS.Close();
}
zipStream.BeginWrite(buffer, 0, buffer.Length, writeCallback, readByteCount);
};
IAsyncResult async=inFS.BeginRead(buffer, 0, buffer.Length, readCallback, null);
compressSuccess = true;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
return compressSuccess;
}
我的想法就是异步读异步写, 异步读取到BUFFER缓存然后异步写入压缩流中.然后在读.再写.....这样直到读完为止..可是出现异常了说只能执行一个异步...不知道哪里出问题了,请高手们帮帮我吧或者给我一个更好的解决方法,我在这里谢谢各位了!
private Boolean compressDateByFile(String inFilePath,String outFilePath)
{
int bufferSize = 2048;
this.isComplete = false;
Boolean compressSuccess = false;//标记异步压缩完成
Byte[] buffer = null;
try
{
buffer = new Byte[bufferSize];
FileStream inFS = new FileStream(inFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, true);
this.totalSize=inFS.Length;
FileStream outFS = new FileStream(outFilePath, FileMode.Create, FileAccess.Write, FileShare.Write, bufferSize, true);
GZipStream zipStream = new GZipStream(outFS, CompressionMode.Compress);
AsyncCallback readCallback = null;
AsyncCallback writeCallback = null;
writeCallback = delegate(IAsyncResult asyncResult)
{
zipStream.EndWrite(asyncResult);
int newSize=int.Parse(asyncResult.AsyncState.ToString());
if (newSize != 0)
{
this.curSize += newSize;
this.percent = calcPercent(this.curSize, this.totalSize);
}
else
{
this.isComplete = true;
zipStream.Close();
}
};
readCallback = delegate(IAsyncResult asyncResult)
{
int readByteCount = inFS.EndRead(asyncResult);
if (readByteCount > 0)
{
//没有到结尾
inFS.BeginRead(buffer, 0, buffer.Length, readCallback, null);
}
else
{
//到结尾了
inFS.Close();
}
zipStream.BeginWrite(buffer, 0, buffer.Length, writeCallback, readByteCount);
};
IAsyncResult async=inFS.BeginRead(buffer, 0, buffer.Length, readCallback, null);
compressSuccess = true;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
return compressSuccess;
}
我的想法就是异步读异步写, 异步读取到BUFFER缓存然后异步写入压缩流中.然后在读.再写.....这样直到读完为止..可是出现异常了说只能执行一个异步...不知道哪里出问题了,请高手们帮帮我吧或者给我一个更好的解决方法,我在这里谢谢各位了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货