在BCB5中使用zlib中出现的怪事!该如何解决
在BCB5中使用zlib中出现的怪事!
最近发现,在BCB中使用zlib,在压缩文件过程中,大概率地出现如下错误:不能打开文件tmp_tmp_,就是有时会,有时不会,但我一加入如下语句:Sleep(500),就都不会出现错误了!真是怪事!谁能解释原因?
bool __fastcall TForm1::CompressFileNew(const AnsiString SourceFile, const AnsiString DestinationFile,TCompressionLevel CompressLevel)
{
TFileStream *infile, *outfile, *tmpfile ;
TCompressionStream *compr;
int i,l ;
String s ;
char *c;
//if (Files->Count > 0 )
outfile =new TFileStream(DestinationFile,fmCreate);
try
{
l = 1;//Files->Count;
outfile->Write(&l,sizeof(l));
infile=new TFileStream(SourceFile,fmOpenRead);
try
{
s = ExtractFileName(SourceFile);//Files->Strings[i]);
l =s.Length() ;
c=new char[l+1];
strcpy(c,s.c_str());
outfile->Write(&l,sizeof(l));
outfile->Write(c,l);
delete []c;
l = infile->Size;
outfile->Write(&l,sizeof(l));
tmpfile =new TFileStream("tmp_tmp_",fmCreate);
compr =new TCompressionStream(CompressLevel,tmpfile);//Zlib::clMax,tmpfile);
try
{
compr->CopyFrom(infile,l);
}
__finally
{
delete compr;
delete tmpfile;
}
Sleep(500); //加入这句后,就不会出现错误了!
tmpfile = new TFileStream("tmp_tmp_",fmOpenRead);
try
{
outfile->CopyFrom(tmpfile,0);
}
__finally
{
delete tmpfile;
}
}
__finally
{
delete infile;
}
}
__finally
{
delete outfile;
}
DeleteFile("tmp_tmp_");
return true;
}
------解决方案--------------------
可能是没有压缩完成呗。
------解决方案--------------------
呵呵,也经常碰到这种事情,Sleep函数将挂起当前线程的执行,等侯指定的时间间隔,所以此时sleep是挂起主线程,等待子线程进行压缩操作
------解决方案--------------------
------解决方案--------------------
在windwos环境下,这个问题经常出现,delete tmpfile;调用API,感觉是异步操作,就像WinExec一样,很多API会自己开线程.所以我加Sleep已是习已为常的事了
------解决方案--------------------
一般为内存错误。或者打开文件没关闭。。。。等等
最近发现,在BCB中使用zlib,在压缩文件过程中,大概率地出现如下错误:不能打开文件tmp_tmp_,就是有时会,有时不会,但我一加入如下语句:Sleep(500),就都不会出现错误了!真是怪事!谁能解释原因?
bool __fastcall TForm1::CompressFileNew(const AnsiString SourceFile, const AnsiString DestinationFile,TCompressionLevel CompressLevel)
{
TFileStream *infile, *outfile, *tmpfile ;
TCompressionStream *compr;
int i,l ;
String s ;
char *c;
//if (Files->Count > 0 )
outfile =new TFileStream(DestinationFile,fmCreate);
try
{
l = 1;//Files->Count;
outfile->Write(&l,sizeof(l));
infile=new TFileStream(SourceFile,fmOpenRead);
try
{
s = ExtractFileName(SourceFile);//Files->Strings[i]);
l =s.Length() ;
c=new char[l+1];
strcpy(c,s.c_str());
outfile->Write(&l,sizeof(l));
outfile->Write(c,l);
delete []c;
l = infile->Size;
outfile->Write(&l,sizeof(l));
tmpfile =new TFileStream("tmp_tmp_",fmCreate);
compr =new TCompressionStream(CompressLevel,tmpfile);//Zlib::clMax,tmpfile);
try
{
compr->CopyFrom(infile,l);
}
__finally
{
delete compr;
delete tmpfile;
}
Sleep(500); //加入这句后,就不会出现错误了!
tmpfile = new TFileStream("tmp_tmp_",fmOpenRead);
try
{
outfile->CopyFrom(tmpfile,0);
}
__finally
{
delete tmpfile;
}
}
__finally
{
delete infile;
}
}
__finally
{
delete outfile;
}
DeleteFile("tmp_tmp_");
return true;
}
------解决方案--------------------
可能是没有压缩完成呗。
------解决方案--------------------
呵呵,也经常碰到这种事情,Sleep函数将挂起当前线程的执行,等侯指定的时间间隔,所以此时sleep是挂起主线程,等待子线程进行压缩操作
------解决方案--------------------
------解决方案--------------------
在windwos环境下,这个问题经常出现,delete tmpfile;调用API,感觉是异步操作,就像WinExec一样,很多API会自己开线程.所以我加Sleep已是习已为常的事了
------解决方案--------------------
一般为内存错误。或者打开文件没关闭。。。。等等