LZ看下对你是否有帮助
procedure TForm1.Button5Click(Sender: TObject);
var
ms,mt:TMemoryStream;
fn:string;
SourceStream:TCompressionStream;
Count:Integer;
begin
if opendialog2.Execute and FileExists(opendialog2.FileName) then
begin
fn:=opendialog2.FileName;
ms:=TMemoryStream.Create;
ms.LoadFromFile(fn);
{ Count:=ms.Size;
mt:=TMemoryStream.Create;
//构建压缩流
SourceStream:=TCompressionStream.Create(clMax,mt);
try
ms.SaveToStream(SourceStream);
SourceStream.Free;
ms.Clear;
ms.CopyFrom(mt,0);
Count:=ms.Size; }
with adq1 do
begin
adq1.Insert;
// (FieldByName('avi') as TBlobField).LoadFromStream(ms);
(FieldByName('avi') as TBlobField).LoadFromStream(zstream(ms));
post;
end;
{ finally
ms.Free;
mt.Free;
end; }
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
ms:TMemoryStream;
// SourceStream:TDecompressionStream;
Count:Integer;
path:string;
Buffer
ointer;
begin
path:=ExtractFilePath(Application.ExeName);
ms:=TMemoryStream.Create;
try
with adq1 do
begin
adq1.Edit;
(FieldByName('avi') as TBlobField).SaveToStream(ms);
adq1.Cancel;
{// ms.Position := 0;
ms.ReadBuffer(Count, Sizeof(Count));
GetMem(Buffer,Count);
SourceStream := TDecompressionStream.Create(ms);
try
SourceStream.readBuffer(Buffer^,Count);
//将数据流保存至ms流中
ms.Clear;
ms.writeBuffer(Buffer^,Count);
ms.Position := 0;//复位流指针
Count:=ms.Size;
}
ms:=UnZStream(ms);
ms.SaveToFile(path+'456.avi');
// SourceStream.Free;
// except
// end;
end;
finally
ms.Free;
end;
end;
function ZStream(var CompressedStream:TMemoryStream):TMemoryStream;
var
SourceStream:TCompressionStream;
DestStream:TMemoryStream;
Count:Integer;
Begin
//获得数据流的原始尺寸
Count:=CompressedStream.Size;
DestStream:=TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clMax,DestStream);
Try
//SourceStream中保存着原始的数据流
CompressedStream.SaveToStream(SourceStream);
//将原始数据流进行压缩,DestStream中保存着压缩后的数据流
SourceStream.Free;
with CompressedStream do begin
Clear;
//写入原始数据流尺寸
WriteBuffer(Count,SizeOf(Count));
//写入经过压缩的数据流
CopyFrom(DestStream,0);
//恢复流指针
Position:=0;
end;
finally
DestStream.Free;
end;
Result:=CompressedStream;
end;
function UnZStream(var res:TMemoryStream):TMemoryStream;
//解压缩文件数据流
var
SourceStream:TDecompressionStream;
Buffer
Char;
Count:Integer;
src:TMemoryStream;
Begin
res.Position:=0;
//从被压缩的数据流中读出原始数据流尺寸
res.ReadBuffer(Count,SizeOf(Count));
//根据数据流尺寸大小为将要读入的原始数据流分配内存块
GetMem(Buffer,Count);
SourceStream := TDecompressionStream.Create(res);
Try
//将被压缩的数据流解压缩,然后存入Buffer内存块中
SourceStream.readBuffer(Buffer^,Count);
//将数据流保存至res流中
res.Clear;
res.writeBuffer(Buffer^,Count);
res.Position:=0;//复位流指针
finally
FreeMem(Buffer);
end;
Result:=res;
end;